My customer accountSales contactWebmailOVHcloud Blog

Welcome to OVHcloud.

Log in to order, manage your products and services, and track your orders

Log in
OVH Guides

Getting the source IP behind the LoadBalancer

Find out how to get the source IP behind the LoadBalancer on OVHcloud Managed Kubernetes

Last updated 20 May, 2020.

Before you begin

This tutorial presupposes that you already have a working OVHcloud Managed Kubernetes cluster, and you have deployed there an application using the OVHcloud Managed Kubernetes LoadBalancer. If you want to know more on those topics, please look at the using the OVHcloud Managed Kubernetes LoadBalancer documentation.

The problem

When you deploy your HTTP services in NodePort mode, you directly recover the request's Remote Address from the server (for example using $_SERVER['REMOTE_ADDR'] on PHP or $ENV{'REMOTE_ADDR'} in Perl). This address (usually in IP:port format) corresponds to the original requestor or the last proxy between them and your cluster.

When deploying the services in LoadBalancer mode, things are a bit different, our Load Balancer acts like a proxy, and the Remote Address will give you the IP address of the Load Balancer. How can you get the source IP of the request in this case?

This tutorial describe how to deploy a LoadBalancer service on OVHcloud Managed Kubernetes and preserve the source IP.

Getting the request's source IP behind the LoadBalancer

The easiest way to deploy services behind the Load Balancer while keeping the source IP is to place your services under an Ingress, itself behind the LoadBalancer.

The Ingress is exposed to the outside of the cluster either via LoadBalancer, and it routes incoming traffic to your services according to configured rules. And additional advantage of this setting is the cost: you can have lots of services behind a single LoadBalancer.

In this tutorial we are using the most basic Ingress Controller: NGINX Ingress Controller, where an NGINX server take the role of reverse proxy.

1. Installing the NGINX Ingress Controller

The official way to install the NGINX Ingress Controller is using a mandatory manifest file:

kubectl apply -f

It creates the namespace, serviceaccount, role and all the other Kubernetes objects needed for the Ingress Controller, and then it deploys the controller:

$ kubectl apply -f
namespace/ingress-nginx created
kind: Service
serviceaccount/ingress-nginx created
# Please edit the object below. Lines beginning with a '#' will be ignored,
configmap/ingress-nginx-controller created created created created created
service/ingress-nginx-controller-admission created
service/ingress-nginx-controller created
deployment.apps/ingress-nginx-controller created created created created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created created created
serviceaccount/ingress-nginx-admission created

2. Deploying an Ingress behind the LoadBalancer

Now we are deploying the Ingress that will be the entry point for your services, and placing it behind the LoadBalancer. Our Ingress definition uses the externalTrafficPolicy property with Local value, that preserves the client source IP.

Here you have the manifest for this Ingress:

kind: Service
apiVersion: v1
  name: ingress-lb
  namespace: ingress-nginx
  annotations: "v1"
  selector: ingress-nginx
  externalTrafficPolicy: Local
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  - name: https
    protocol: TCP
    port: 443
    targetPort: 443
  type: LoadBalancer

Copy it in a ingress-lb.yaml file, and deploy it:

kubectl apply -f ingress-lb.yaml

You have now an Ingress behind the LoadBalancer:

$ kubectl apply -f ingress-lb.yaml
service/ingress-lb created

You can use kubectl to get the state of the service and recover the Load Balancer's IP:

kubectl get service -n ingress-nginx ingress-lb

You should see your newly created Ingress service:

$ kubectl get service -n ingress-nginx ingress-lb
NAME         TYPE           CLUSTER-IP    EXTERNAL-IP                          PORT(S)                      AGE
ingress-lb   LoadBalancer    80:30113/TCP,443:30051/TCP   3m8s

As the LoadBalancer creation is asynchronous, and the provisioning of the Load Balancer can take several minutes, you can get a <pending> at EXTERNAL-IP while the Load Balancer is setting up. In this case, please wait some minutes and try again.

3. Patching the Ingress Controller

Now you need to patch the Ingress controller to support the proxy protocol.

Get the list of the egress load balancer IPs:

kubectl get svc ingress-lb -n ingress-nginx -o jsonpath="{\.k8s\.ovh\.net/egress-ips}"

You should see something like this:

$ kubectl get svc ingress-lb -n ingress-nginx -o jsonpath="{\.k8s\.ovh\.net/egress-ips}",bbb.bbb.bbb.bbb/32,ccc.ccc.ccc.ccc/32,ddd.ddd.ddd.ddd/32,eee.eee.eee.eee/32,fff.fff.fff.fff/32

Copy the next YAML snippet in a patch-ingress-configmap.yml file and modify the set-real-ip-from parameter accordingly:

  use-proxy-protocol: "true"
  real-ip-header: "proxy_protocol"
  set-real-ip-from: ",bbb.bbb.bbb.bbb/32,ccc.ccc.ccc.ccc/32,ddd.ddd.ddd.ddd/32,eee.eee.eee.eee/32,fff.fff.fff.fff/32"

And apply it in your cluster:

kubectl -n ingress-nginx patch configmap ingress-nginx-controller -p "$(cat patch-ingress-configmap.yml)"

After applying the patch, you need to restart the Ingress Controller:

kubectl rollout restart deploy/ingress-nginx-controller -n ingress-nginx

You should see the configuration being patched and the controller pod deleted (and recreated):

$ kubectl -n ingress-nginx patch configmap nginx-configuration -p "$(cat patch-ingress-configmap.yml)"
configmap/nginx-configuration patched
$ kubectl rollout restart deploy/ingress-nginx-controller -n ingress-nginx
deployment.apps/ingress-nginx-controller restarted

4. Testing

We can now deploy a simple echo service to verify that everything is working. The service will use the mendhak/http-https-echo image, a very useful HTTPS echo Docker container for web debugging.

First, copy the next manifest to a echo.yaml file:

apiVersion: v1
kind: Namespace
  name: echo


apiVersion: apps/v1
kind: Deployment
  name: echo-deployment
  namespace: echo
    app: echo
  replicas: 1
      app: echo
        app: echo
      - name: echo
        image: mendhak/http-https-echo
        - containerPort: 80
        - containerPort: 443


apiVersion: v1
kind: Service
  name: echo-service
  namespace: echo
    app: echo
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP


apiVersion: extensions/v1beta1
kind: Ingress
  name: echo-ingress
  namespace: echo
    serviceName: echo-service
    servicePort: 80

And deploy it on your cluster:

kubectl apply -f echo.yaml
$ kubectl apply -f echo.yaml
namespace/echo created
deployment.apps/echo-deployment created
service/echo-service created
ingress.extensions/echo-ingress created

Now you can test it using the LoadBalancer URL:


And you should get the HTTP parameters of your request, including the right source IP in the x-real-ip header:

  "path": "/",
  "headers": {
    "host": "",
    "x-request-id": "2126b343bc837ecbd07eca904c33daa3",
    "x-real-ip": "XXX.XXX.XXX.XXX",
    "x-forwarded-for": "XXX.XXX.XXX.XXX",
    "x-forwarded-host": "",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-original-uri": "/",
    "x-scheme": "http",
    "user-agent": "curl/7.58.0",
    "accept": "*/*"
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "",
  "ip": "::ffff:",
  "ips": [],
  "protocol": "http",
  "query": {},
  "subdomains": [
  "xhr": false,
  "os": {
    "hostname": "echo-deployment-6b6fdc96cf-hwqw6"

What if I want to use another Ingress Controller

The precedent method should work in a similar way for any Ingress Controller. We will soon update this tutorial with more detailed information on other Ingress Controllers, specifically Traefik.

Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.

These guides might also interest you...