Question: I have a situation with `ip_hash` on nginx for sticky session where it always direct the traffic to one server only. The nginx is running behind a Google Cloud load balancer, from nginx it hits my app's GC Kubernetes load balancer before actually going to one of the pod.

I have a thought that all users' IP was masked by GC load balancer so that nginx recognises them as one source.

I was told that we can add user's original IP to nginx's header so that the load balancer able to recognise them as different users. How can I approach one? Or is there another solution?

Answer: 
If you are using Network load balancing (with target pools) the load balancer keeps the IP. What happens is that Kubernetes is changing the [source IP](https://kubernetes.io/docs/tutorials/services/source-ip/#source-ip-for-services-with-typeloadbalancer) with the cluster/node IPs.

Kubernetes has a feature to preserve the client source IP. You can check in the docs for [how to preserve the client source IP in the services with Type=LoadBalancer](https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip) (Network load balancing).
Configure **[session affinity](https://cloud.google.com/compute/docs/load-balancing/http/#session_affinity)** during the initial configuration of a **[target pool](https://cloud.google.com/compute/docs/load-balancing/network/target-pools)** by setting it to one of the following **non-default values** in order to provide the desired level of *sticky session*:

> 
> `CLIENT_IP`
> 
> 
> 2-tuple hashing, which uses the source and destination IPs. All 
>  connections from a client will end up on the same instance regardless of
>  protocol as long as the instance stays healthy.
> 
> 
> `CLIENT_IP_PROTO`
> 
> 
> 3-tuple hashing, which uses the source and destination IPs and the
>  protocol. All connections from a client will end up on the same
>  instance as long as they use the same protocol and the instance stays
>  healthy.
> 
> 
> 

[Lakshman Diwaakar](https://stackoverflow.com/users/3477322/lakshman-diwaakar) refers to session affinity configuration as being a non-modifiable attribute set *"during the creation of"* a target pool. His review of Cloud Compute load-balancing functionalities available might be interesting for you to review: [Network load balancer Vs HTTP(s) Load Balancer](https://stackoverflow.com/questions/41535226/what-are-the-differences-between-network-and-https-load-balancer-in-gcp/41535227#answer-41535227).