# Assign a Static IP Address to a Kubernetes Service

In IONOS Cloud Managed Kubernetes, `LoadBalancer` service creation does not provision an external load balancer. Instead, IONOS Cloud automatically reserves a static public IP address and assigns it as a secondary IP address to one of your worker nodes. This node acts as an ingress node, accepting incoming traffic and forwarding it to the correct pod using `kube-proxy`.

## Functionality of `LoadBalancer` services in IONOS Cloud Managed Kubernetes

1. When a `LoadBalancer` service is created, IONOS Cloud either assigns an existing pre-reserved static IP address or automatically reserves a new one.
2. This IP address is attached as a secondary IP address to one of the worker nodes in the cluster.
3. The designated ingress node handles incoming traffic.
4. If the target pod runs on a different node, `kube-proxy` routes the traffic to the correct destination.

## Reserve a static IP address

You can obtain a static IP address in either of the following ways:

* **Pre-reserving a static IP address:** You can manually reserve an IP address beforehand and reference it in your service manifest. This ensures that the IP address remains assigned even if the service is deleted.
* **Automatic Reservation:** If you do not specify a `loadBalancerIP`, IONOS Cloud dynamically reserves an IP address. However, this IP address is released when the service is deleted.

## Example: Deploy an echo server with a static IP address

To demonstrate the behavior of the IONOS Cloud `LoadBalancer` service, we will deploy an echo server that returns request details useful for verifying traffic routing.

{% stepper %}
{% step %}
**Deploy the echo server:** The following deployment runs two instances of the echo server to demonstrate traffic forwarding:

```bash
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-server
spec:
  replicas: 2
  selector:
    matchLabels:
      app: echo-server
  template:
    metadata:
      labels:
        app: echo-server
    spec:
      containers:
        - name: echo-server
          image: ealen/echo-server
          ports:
            - containerPort: 80
```

{% endstep %}

{% step %}
**Create the `LoadBalancer` service:** Use either of the following options to create a `LoadBalancer` service:

<details>

<summary><strong>Option 1: Use a pre-reserved static IP address</strong></summary>

If you have manually reserved a static IP address in your IONOS Cloud account, you can assign it explicitly in the service manifest using the following code:<br>

```

apiVersion: v1
kind: Service
metadata:
  name: echo-service
spec:
  type: LoadBalancer
  loadBalancerIP:RESERVED-IP (Remember to mention your pre-reserved static IP address.)
  selector:
    app: echo-server
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
```

</details>

<details>

<summary><strong>Option 2: Automatically assign a static IP address</strong></summary>

If you do not specify a `loadBalancerIP`, IONOS Cloud dynamically reserves an IP address. When a static IP address is assigned dynamically, it will be released once the service is deleted.<br>

```

apiVersion: v1
kind: Service
metadata:
  name: echo-service
spec:
  type: LoadBalancer
  selector:
    app: echo-server
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
```

</details>
{% endstep %}

{% step %}
\*\*Verify the Assigned Static IP Address:\*\* Check the assigned external IP address after service creation:

```bash
kubectl get service echo-service
```

{% hint style="success" %}
**Result:** The following is an example output where `85.215.190.220` is the static IP address assigned by IONOS Cloud:

```bash
NAME          TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE
echo-service  LoadBalancer   10.96.0.1      85.215.190.220     80:31234/TCP   5m
```

{% endhint %}
{% endstep %}

{% step %}
\*\*Test the Echo Server:\*\* You can send a request to the assigned IP address to verify traffic routing:

```bash
curl http://85.215.190.220 
```

{% hint style="success" %}
**Result:** The following is an example response:

```bash
{
  "path": "/",
  "method": "GET",
  "headers": {
  
    "Host": "85.215.190.220",
    "User-Agent": "curl/7.68.0",
    "Accept": "*/*" }
}
```

{% endhint %}
{% endstep %}
{% endstepper %}

## Summary

This setup ensures a stable external IP address for applications without requiring an additional load balancer.

* IONOS Cloud reserves a static IP address for each `LoadBalancer` service.
* You can either pre-reserve an IP address or let IONOS Cloud assign one automatically.
* The static IP address is attached to an ingress node and used to route traffic via `kube-proxy`.
* If you do not specify a `loadBalancerIP`, the IP address is automatically released when you delete the service.
