Creating Services

Services allow communication between your applications in your cluster.

In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them. A service allows communication between pods in your cluster.

The example below creates a MySQL deployment along with a service to access it.

Deployment::create('mysql')
    ->setContainerPort(3306)
    ->setContainerImage('mysql')
    ->setEnvironmentVariable(new EnvironmentVariable('MYSQL_ROOT_PASSWORD', fromEnvironmentVariableName: 'DB_PASSWORD'))
    ->setEnvironmentVariable(new EnvironmentVariable('MYSQL_DATABASE', fromEnvironmentVariableName: 'DB_DATABASE'))
    ->setEnvironmentVariable(new EnvironmentVariable('MYSQL_USER', fromEnvironmentVariableName: 'DB_USERNAME'))
    ->setEnvironmentVariable(new EnvironmentVariable('MYSQL_PASSWORD', fromEnvironmentVariableName: 'DB_PASSWORD'));

Service::create('mysql')
    ->setTargetPort(3306)
    ->setServicePort(3306);

Networking

Set Target Port

The target port is for forwarding traffic, traffic will be forwarded onto the container specified in your Deployment.

Set Service Port

The service port is for receiving traffic, an example would be from your Laravel application.

Load Balancer

You can create a service to act as a load balancer, calling this method will create a load balancer on the Cloud provider you use or update it if needed.

Load Balancers can take a while to provision so you may want to check the status by logging into your provider.

Environment

Set Namespace

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

The default namespace is "default".

Last updated