Background
I have web application which deployed to deployment with multiple pods. The deployment is exposed to the internet with kubernetes service with external IP.
The external IP exposed to the world via Cloudflare:
Client ---> Cloudflare ---> k8 service ---> pod
This web application needs to be defined with sticky sessions. So I patched my service with sessionAffinity: ClientIP, like this:
kubectl patch service MYSERVICE -p '{"spec":{"sessionAffinity":"ClientIP"}}'
I checked it on development environment and found that the session affinity is not working well.
Investigation
I looked for the problem with the sticky sessions. Then I found that Cloudflare caller IP can be changed from time to time - randomly. This will redirect all the users to another pod - exactly what Sticky Session should solve.
So, the problem is that my Loadbalancer service redirect traffic according to Cloudflare IP and it random.
Possible solutions
- I found that it may be possible to loadbalance traffic according to Cookie. Founded this source. But it using advanced Kubernetes components, like BackendService and Ingress, that needs to be well-defined. Do you have more simple solution?
- Cloudflare attaching the real client IP to the request in headers. Is this possible to define the loadbalancing to look on this header and redirect the traffic according to it's value?