<p>I have a small Kubernetes on prem cluster (Rancher 2.3.6) consisting of three nodes. 
The deployments inside the cluster are provisioned dynamically by an external application and always have their replica count set to 1, because these are stateful applications and high availability is not needed.</p>

<p>The applications are exposed to the internet by NodePort services with a random port and ExternalTrafficPolicy set to Cluster. So if the user requests one of the three nodes, the k8s proxy will route and s-NAT the request to the correct node with the application pod. </p>

<p>To this point, everything works fine.</p>

<p>The problem started when we added Applications that rely on the requests source IP. Since the s-NAT replaces the request IP with an internal IP this applications don't work properly.</p>

<p>I know, that setting the services ExternalTrafficPolicy to local will disabke s-natting. But this will also break the architecture, because not every pod has an instance of the application running.</p>

<p>Is there a way to preserve the original client IP and still make use of the internal routing, so i won't have to worry about on which node the request will land?</p>

<p>It depends on how the traffic gets into your cluster. But let's break it down a little bit:</p>
<p>Generally, there are two strategies on how to handle source ip preservation:</p>
<ul>
<li>SNAT (packet IP)</li>
<li>proxy/header (passing the original IP in an additional header)</li>
</ul>
<h4>1) SNAT</h4>
<p>By default packets to <em>NodePort</em> and <em>LoadBalancer</em> are SourceNAT'd (to the node's IP that received the request), while packets send to <em>ClusterIP</em> are <strong>not</strong> SourceNAT'd.</p>
<p>As you metioned already, there is a way to turn off SNAT for <em>NodePort</em> and <em>LoadBalancer</em> Services by setting <code>service.spec.externalTrafficPolicy: Local</code> which preserves the original source IP address, but with the undesired effect that kube-proxy only proxies proxy requests to local endpoints, and does not forward traffic to other nodes.</p>
<h4>2) Header + Proxy IP preservation</h4>
<p><em><strong>a) Nginx Ingress Controller and L7 LoadBalancer</strong></em></p>
<ul>
<li>When using L7 LoadBalancers which are sending a <code>X-Forwarded-For</code> header, Nginx by default evaluates the header containing the source ip if we have set the LB CIDR/Address in the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#proxy-real-ip-cidr" rel="nofollow noreferrer"><code>proxy-real-ip-cidr</code></a></li>
<li>you might need to set <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#use-forwarded-headers" rel="nofollow noreferrer"><code>use-forwarded-headers</code></a> explicitly to make nginx forward the header information</li>
<li>additionally you might want to enable <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#enable-real-ip" rel="nofollow noreferrer"><code>enable-real-ip</code></a> so the realip_module replaces the real ip that has ben set in the <code>X-Forwarded-For</code> header provided by the trusted LB specified in <code>proxy-real-ip-cidr</code>.</li>
</ul>
<p><em><strong>b) Proxy Protocol and L4 LoadBalancer</strong></em></p>
<ul>
<li>With enabled <code>use-proxy-protocol: &quot;true&quot;</code> the header is <strong>not</strong> evaulated and the connection details will be send before forwarding the TCP actual connection. The LBs must support this.</li>
</ul>
