> For the complete documentation index, see [llms.txt](https://benosborne.gitbook.io/larakube/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://benosborne.gitbook.io/larakube/services/creating-services.md).

# Creating Services

Services allow communication between your applications in your cluster.

{% hint style="success" %}
You are browsing the latest version for the release [v0.0.3](https://github.com/larakube/larakube/releases/tag/0.0.3).
{% endhint %}

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.

```php
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](/larakube/services/creating-deployments.md).

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

### Set Service Port

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

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

### 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.

```php
Service::create('laravel')->loadBalancer();
```

## Environment

### Set Namespace

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

```php
Service::create('mysql')->setNamespace('production');
```

{% hint style="info" %}
The default namespace is "default".
{% endhint %}
