<p>I need to get client IP in Kubernetes, but I can only get the Flannel internal IP <code>10.244.X.1</code> (in nginx via <code>$remote_addr</code>, <code>$http_x_forwarded_for</code> is always <code>-</code>).</p>

<p>The Network topo is:<br>
client browser -> k8s cluster node ip -> k8s service (A) -> the pod (which desire the client ip) -> other services</p>

<p>I have tried <code>externalTrafficPolicy</code> in <code>A</code>, doesn't work. Is it only work on cloud provider, or behind a LB? <a href="https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip" rel="nofollow noreferrer">[1]</a>
<br>
I also tried ingress-nginx <a href="https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#source-ip-address" rel="nofollow noreferrer">[2]</a>, get the same result.</p>

<p>My Environment:</p>

<pre><code> Kernel Version:             3.10.0-1062.4.1.el7.x86_64
 OS Image:                   CentOS Linux 7 (Core)
 Operating System:           linux
 Architecture:               amd64
 Container Runtime Version:  docker://18.6.2
 Kubelet Version:            v1.16.2
 Kube-Proxy Version:         v1.16.2
 Network Add-on:             Flannel
</code></pre>

<p>I have read some similar questions <a href="https://stackoverflow.com/questions/32112922/how-to-read-client-ip-addresses-from-http-requests-behind-kubernetes-services">[3]</a> <a href="https://stackoverflow.com/questions/32759517/how-to-get-the-real-ip-in-the-request-in-the-pod-in-kubernetes">[4]</a> <a href="https://stackoverflow.com/questions/58149992/how-to-get-clients-public-ip-on-the-pod">[5]</a> , looks answers are imply to cloud provider or LB.</p>

<p>Kubernetes services work in one of three proxy modes: Userspace, iptables, and IPVS. I'm not familiar with IPVS, but in both Userspace and iptables modes, the client IP will be altered for NodePort and Load Balancer services [<a href="https://kubernetes.io/docs/concepts/services-networking/service/#ips-and-vips" rel="nofollow noreferrer">docs</a>].</p>

<p>Due to this proxying behaviour, the client IP will necessarily change, and that data can only be preserved if the client and server are speaking a "higher layer" protocol and there's a "higher layer" proxy that can capture the original client IP and inject it into the request before forwarding it on. If client and server are talking HTTP, then there are standard headers that an IaaS L7/HTTP(S) load balancer or nginx can inject for you, however if you're using nginx-ingress then the problem is that is sitting behind a NodePort or LoadBalancer service, so by the time it gets to the actual nginx process the client IP has been altered. You would need to have nginx running outside the cluster to make this work.</p>

<p>If you're running this on a public cloud, I think most cloud's L7 LB solutions inject these <code>X-Forwarded-For</code> or <code>Forwarded</code> headers out of the box for you. Alternatively, consider designing your server such that it doesn't need the client IP -- if client IP is being used to authenticate a client or authorize a request, there are other more cloud-native patterns that could work instead.</p>
