Creating Deployments

Deployments are where your application is defined including scaling and resource limits.

You are browsing the latest version for the release v0.0.3.

Deployments are defined by using the class Larakube\Cluster\Deployment. You define your deployments in the kube/services.php file.

The below example creates a deployment for our Laravel application.

<?php

use Larakube\Cluster\Deployment;
use Larakube\Cluster\EnvironmentVariable;
use Larakube\Cluster\Service;

Deployment::create('laravel')
    ->setContainerPort(80)
    ->setReplicaCount(2)
    ->setDockerFilePath('Dockerfile')
    ->setEnvironmentVariable(new EnvironmentVariable('DB_HOST', 'mysql'))
    ->setEnvironmentVariable(new EnvironmentVariable('DB_USERNAME', 'root', fromEnvironmentVariableName: 'DB_USERNAME'))
    ->setEnvironmentVariable(new EnvironmentVariable('DB_PASSWORD', fromEnvironmentVariableName: 'DB_PASSWORD'));

Container

Set Dockerfile

To tell your service that it needs to build from a Dockerfile you can use the method below:

Deployment::create('laravel')->setDockerFilePath('docker/Dockerfile');

This method takes one argument which is the relative path to the service directory of where the Dockerfile is located.

Example

If your Laravel application service is located at kube/services/laravel and your Dockerfile is located at kube/services/laravel/docker/Dockerfile then you only need to pass the value docker/Dockerfile to the method.

Deployment::create('laravel')->setDockerFilePath('docker/Dockerfile');

Set Container Image

You can specify an image and tag for deployment pods to pull from.

Deployment::create('laravel')->setContainerImage('mysql', 'latest');

Environment

Set Namespace

You may want to explicitly set the namespace for your service in Kubernetes, you can achieve this with the following method.

Deployment::create('laravel')->setNamespace('production');

The default namespace is "default".

Environment Variables

You can easily set environment variables for your deployments, they can be hard coded values or come from the current environment, for example when you are deploying from a CI provider.

Deployment::create('laravel')->setEnvironmentVariable(
    new EnvironmentVariable('DB_HOST', 'mysql')
);

Take a look at the EnvironmentVariable class for method reference.

Scaling

Set number of Replicas

You can set the number of replicas a deployment should have by using the method below.

Deployment::create('laravel')->setReplicaCount(2);

The default number of replicas is 1.

Networking

Set Container Port

The container port should be the same port you expose in your Dockerfile and/or the port your application listens on for traffic.

If you wish to change the port you can use the method below.

Deployment::create('laravel')->setContainerPort(3306);

The default container port is 80.

Last updated