Python Flask Kubernetes Deployment with Liveliness Probe

9/28/2018

How can we deploy a flask application on Kubernetes which also has a probePath to check the liveliness of the application?

Most of the tutorials/examples which I bumped into do not take care of it and just launch the app with application.run() inside the docker image.

I feel that the problem with this approach is that if some request is taking too much time to load on a pod and in that time, probePath URL is called by Kubernetes on the pod, it will timeout. Kubernetes will hence, kill the pod assuming it is non-responsive.

Am I missing something?

-- Arpan Gupta
docker
flask
kubernetes
python

1 Answer

9/28/2018

I found a solution after some research.

UWSGI is able to help in this case with a cheaper mode.

To enable cheaper mode add the cheaper = N option to the uWSGI configuration file, where N is the minimum number of workers uWSGI can run. The cheaper value must be lower than the maximum number of configured workers (workers or processes option).

set cheaper algorithm to use, if not set default will be used

cheaper-algo = spare

minimum number of workers to keep at all times

cheaper = 2

number of workers to spawn at startup

cheaper-initial = 5

maximum number of workers that can be spawned

workers = 10

how many workers should be spawned at a time

cheaper-step = 1

This configuration will tell uWSGI to run up to 10 workers under load. If the app is idle uWSGI will stop workers but it will always leave at least 2 of them running. With cheaper-initial you can control how many workers should be spawned at startup. If your average load requires more than minimum number of workers you can have them spawned right away and then “cheaped” (killed off) if load is low enough. When the cheaper algorithm decides that it needs more workers it will spawn cheaper-step of them. This is useful if you have a high maximum number of workers – in the event of a sudden load spike it would otherwise take a lot of time to spawn enough workers one by one.

Note: Worker is notified by uWSGI before being cheaped. Worker should finish until timeout is reached (this is configured by worker-reload-mercy configuration parameter). Otherwise, uWSGI kills the worker. Killing worker in the middle of serving request can cause errors or partial responses to the client.

Refer to this link for more information: https://uwsgi-docs.readthedocs.io/en/latest/Cheaper.html

-- Arpan Gupta
Source: StackOverflow