Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Kubernetes, how do I implement session affinity using an Ingress?

Tags:

kubernetes

I'd like to implement a sticky-session Ingress controller. Cookies or IP hashing would both be fine; I'm happy as long as the same client is generally routed to the same pod.

What I'm stuck on: it seems like the Kubernetes service model means my connections are going to be proxied randomly no matter what. I can configure my Ingress controller with session affinity, but as soon as the the connection gets past the that and hits a service, kube-proxy is just going to route me randomly. There's the sessionAffinity: ClientIP flag on services, but that doesn't help me -- the Client IP will always be the internal IP of the Ingress pod.

Am I missing something? Is this possible given Kubernetes' current architecture?

like image 351
iameli Avatar asked Nov 19 '15 00:11

iameli


2 Answers

An ingress controller can completely bypass kube-proxy. The haproxy controller for example, does this and goes straight to endpoints. However it doesn't use the Ingress in the typical sense.

You could do the same with the nginx controller, all you need to lookup endpoints and insert them instead of the DNS name it currently uses (i.e swap this line for a pointer to an upstream that contains the endpoints).

like image 156
Prashanth B Avatar answered Oct 11 '22 18:10

Prashanth B


I evaluated the haproxy controller but could not get it running reliably with session affinity. After some research I discovered Nginx Ingress Controller which since version 0.61 also includes the nginx-sticky-module-ng module and is now running reliably since a couple of days in our test environment. I created a Gist that sets up the required Kubernetes pieces since some important configuration is a bit hard to locate in the existing documentation. Good luck!

like image 31
Till Kuhn Avatar answered Oct 11 '22 17:10

Till Kuhn