0

My architecture is as follows;

  1. Kubernetes (GKE)
  2. nginx reverse proxy deployment fronted by "LoadBalancer" service
  3. Python + Flask WSGI app

This works perfectly as long as there is only ONE Flask pod. As soon as the replica count is increased beyond 1 then sessions completely break down since now the session set in one request is forgotten in the next (request will hit different pods every time instead of the one that remembers the session).

I want to implement sticky sessions somehow. When I google how to solve this I find two types of answers, both of which does not help me;

  1. Put ip_hash directive into nginx config. This does not work because I am relying on the name resolution built into kubernetes to reach the flask app from nginx (I just refer to it by a single name "app". for ip_hash to work, each downstream server needs to be mentioned by name in the nginx config)
  2. Configure nginx ingress controller to use sticky sessions. This does not work because.... I don't use ingress at all. I use a custom nginx (community edition) running in a pod fronted by a LoadBalancer service entry.

What options exist for sticky sessions in my setup?

Do I need to switch my architecture to ingress to have sticky sessions?

Can't I somehow decorate requests with the pod id and route based on that in nginx?

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • Implement #2, get inside the ingress pod and run a `nginx -T`, this will dump the complete config. Then you can use the parts required for the sticky session in your custom nginx pod – Tarun Lalwani Feb 21 '22 at 03:44
  • If you have flexibility to your architecture, why can't you think of implementing the distributed sessions in Flask itself ,using modules like Flask-Session with MemCache/Redis/SQLALchemy as your backend ? So you don't need to worry how your cluster scales... just a thought! – Kris Feb 21 '22 at 11:03
  • @Kris that would probably work, but isn't that unneeded added complexity? Another deployment to watch over and care about. – Mr. Developerdude Feb 21 '22 at 21:57
  • @LennartRolland Yes, it would add one more responsibility. Probably it depends on the tradeoff equation as well, like the, volume of session data, app footprint, performance SLAs, who handles infrastructure , ease of debugging etc. – Kris Feb 22 '22 at 05:40
  • @LennartRolland Were you able to get your query resolved ? I believe the point suggested by Kris should work. If yes, post the answer and accept it. – Ramesh kollisetty Feb 28 '22 at 09:40
  • @Rameshkollisetty I am not working on this full time and so I will post it when I damn well want to :D – Mr. Developerdude Mar 01 '22 at 14:12

0 Answers0