# Assign a Static IP Address to a Kubernetes Service

In <code class="expression">space.vars.ionos\_cloud</code> Managed Kubernetes, `LoadBalancer` service creation does not provision an external load balancer. Instead, <code class="expression">space.vars.ionos\_cloud</code> 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, <code class="expression">space.vars.ionos\_cloud</code> 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`, <code class="expression">space.vars.ionos\_cloud</code> 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 <code class="expression">space.vars.ionos\_cloud</code> `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 <code class="expression">space.vars.ionos\_cloud</code> 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`, <code class="expression">space.vars.ionos\_cloud</code> 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 <code class="expression">space.vars.ionos\_cloud</code>:

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

* <code class="expression">space.vars.ionos\_cloud</code> reserves a static IP address for each `LoadBalancer` service.
* You can either pre-reserve an IP address or let <code class="expression">space.vars.ionos\_cloud</code> 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ionos.com/cloud/containers/managed-kubernetes/use-cases/assign-static-ip-address.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
