In this page, you can find an explanation of how to get the source IP of your app in your Kubernetes cluster.
Table of contents
Deploy web application
To show you how we could get the source IP we took small app that echoes back the source IP of requests. To deploy it use command:
kubectl run source-ip-app --image=k8s.gcr.io/echoserver:1.4
Creation service with Type LoadBalancer
1) Create Service with Type
LoadBalancer and annotation
loadbalancer.openstack.org/x-forwarded-for: "true" using this
apiVersion: v1 kind: Service metadata: annotations: loadbalancer.openstack.org/x-forwarded-for: "true" labels: run: source-ip-app name: source-ip-app-svc spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: run: source-ip-app type: LoadBalancer
Use next command to create it:
kubectl apply -f app-svc.yaml
Now get external ip of your service using command:
kubectl get svc
Getting Source IP
To get Source IP you need use command
curl with external IP that you has been received in previous step.
In our case it’s:
The output is:
CLIENT VALUES: client_address=10.100.1.1 command=GET real path=/ query=nil request_version=1.1 request_uri=http://188.8.131.52:8080/ SERVER VALUES: server_version=nginx: 1.10.0 - lua: 10001 HEADERS RECEIVED: accept=*/* host=184.108.40.206 user-agent=curl/7.58.0 x-forwarded-for=220.127.116.11
NoteWith a proxy that terminates the client connection and opens a new connection to your nodes/endpoints. In such cases the source IP will always be that of the cloud LB, not that of the client.
In our case it’s an IP from line:
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.