Running Scalable Lightweight Session aware Python Flask application on Kubernetes

Vinit Kapoor
3 min readFeb 6, 2022
Deploying Flask App on Kubernetes

Objective

Deploy and run scalable lightweight Flask application on Kubernetes. The application is expected to do user session management for logged in users.

Challenge

  • A user management functionality requires a state information to be remembered about the logged in user.
  • Deploying a scalable Flask app requires that we should be able to scale up the app by increasing replicas of it as per the amount of traffic at any time. This further requires load balancer to be in place to distribute the traffic between the different replicas of the app.
  • What are the design considerations to put together for such an app? And what challenges we need to address to deploy such an app on Kubernetes.

Solution

The main design consideration for such an app is how the state information of the user session is managed. Follow are the two options to address this:

  • Stateful approach
  • Stateless approach

Load balancing

  • To scale the app with multiple instances, we require Load balancer to be present which can distribute the traffic between instances of the app. We can have an external load balancer such as the one provided by cloud service providers. There is also an internal load balancing functionality available by default from Cluster IP service of Kubernetes.

Flask app design

  • User management in flask app can be done using Flask-login plugin.

Kubernetes Objects

  • Deployment: App can be deployed as a “Deployment” type of Kubernetes object.
  • Load balancer Service: A service of type “Load Balancer” is created which in turns provides NodePort and Cluster IP services. Normally external Load Balancer is provided by cloud service providers.

Stateful App approach

  • In this option, an app stores the user’s session information with its internal main memory.
  • It requires Load balancer to be session aware which is achieved by using sticky sessions. This can be configured with most of the external load balancers.

Challenge: The challenges with this approach are:

  • It requires ClusterIP service of Kubernetes to be session aware. However, ClusterIP service only supports ClientIP based session awareness which may not work because of NAT and other aspects (ClientIP Load Balancing).
  • It the app would have been deployed outside of Kubernetes, then Load Balancer sticky sessions would have solved this issue, however on Kubernetes ClusterIP does not supports sticky sessions on cookies.

Conclusion: Hence stateful app is not a workable solution even with Load balancer sticky sessions when deployed on Kubernetes.

Light weight Flask App Deployment on Kubernetes

Stateless App approach

In this option, user’s session information is stored outside of the app:

Potential options to store the session information are (Reference)

  • — Client side sessions
  • — Server side sessions

Client side session management

  • In this approach, the session information is stored in a Cookie.
  • We can use “sessions” global object provided by Flask framework to store session information.
  • The contents of this object are transferred as contents of “session” cookie as part of this functionality.

Server side session management

  • Session information is stored in central cache such as Redis or external database. However for very lightweight Flask application, you can prefer to go with client side session management as the complexity remains low.

Conclusion: You can design a light weight stateless Flask app using client side session management which is scalable on Kubernetes.

Optimisations: We may not even require an external Load Balancer to be created, as ClusterIP service along with NodePort could be sufficient to do the load distribution.

--

--