How to get client ip in Kubernetes hosted application?

10/4/2020

I have an application made of a few microservices. UI has is served via Nginx and accesses the API via reverse proxy. This is a local Kubernetes deployed with Rancher.

UI service is NodePort so I can access it outside of the cluster and API can be either NodePort or ClusterIP. In API microservice there is an IP logger that always shows the IP of the Node on which the UI is deployed, no matter what I'm doing.

So far I did try 'externalTrafficPolicy: Local' for the NodePort services in combination with setting Nginx headers (X-Forwarded-For and X-Forwarded-Proto) for reverse proxying. Nothing seems to work.

Is it possible to get client IP in the application hosted in Kubernetes?

Kubernetes Version: v1.17.3

Rancher v2.3.4

-- eugeneK
kubernetes
nginx
rancher

1 Answer

10/4/2020

You need to use externalTrafficPolicy: Local and then look at the actual REMOTE_ADDRESS or whatever your web framework calls it.

-- coderanger
Source: StackOverflow