Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Production ready Python apps on Kubernetes

I have been deploying apps to Kubernetes for the last 2 years. And in my org, all our apps(especially stateless) are running in Kubernetes. I still have a fundamental question, just because very recently we found some issues with respect to our few python apps.

Initially when we deployed, our python apps(Written in Flask and Django), we ran it using python app.py. It's known that, because of GIL, python really doesn't have support for system threads, and it will only serve one request at a time, but in case the one request is CPU heavy, it will not be able to process further requests. This is causing sometimes the health API to not work. We have observed that, at this moment, if there is a single request which is not IO and doing some operation, we will hold the CPU and cannot process another request in parallel. And since it's only doing fewer operations, we have observed there is no increase in the CPU utilization also. This has an impact on how HorizontalPodAutoscaler works, its unable to scale the pods.

Because of this, we started using uWSGI in our pods. So basically uWSGI can run multiple pods under the hood and handle multiple requests in parallel, and automatically spin new processes on demand. But here comes another problem, that we have seen, uwsgi is lacking speed in auto-scaling the process tocorrected serve the request and its causing HTTP 503 errors, Because of this we are unable to serve our few APIs in 100% availability.

At the same time our all other apps, written in nodejs, java and golang, is giving 100% availability.

I am looking at what is the best way by which I can run a python app in 100%(99.99) availability in Kubernetes, with the following

  1. Having health API and liveness API served by the app
  2. An app running in Kubernetes
  3. If possible without uwsgi(Single process per pod is the fundamental docker concept)
  4. If with uwsgi, are there any specific config we can apply for k8s env
like image 617
Ysak Avatar asked Oct 24 '19 05:10

Ysak


People also ask

Is Kubernetes production ready?

While you could argue that a Kubernetes cluster is production-ready the moment it is ready to serve traffic, there is a commonly agreed set of minimum requirements. We'll explore these below. Naturally, you need to prioritize security, ensuring attack surfaces are kept to a minimum.

Can we use Python in Kubernetes?

Whether you want to run a simple Python application or a complex one, Kubernetes can quickly and efficiently help you deploy and scale your applications, seamlessly roll out new features while limiting resources to only required resources.

Is Kubernetes production ready open source platform for?

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.


2 Answers

We use Twisted's WSGI server with 30 threads and it's been solid for our Django application. Keeps to a single process per pod model which more closely matches Kubernetes' expectations, as you mentioned. Yes, the GIL means only one of those 30 threads can be running Python code at time, but as with most webapps, most of those threads are blocked on I/O (usually waiting for a response from the database) the vast majority of the time. Then run multiple replicas on top of that both for redundancy and to give you true concurrency at whatever level you need (we usually use 4-8 depending on the site traffic, some big ones are up to 16).

like image 122
coderanger Avatar answered Oct 16 '22 22:10

coderanger


I have exactly the same problem with a python deployment running the Flask application. Most api calls are handled in a matter of seconds, but there are some cpu intensive requests that acquire GIL for 2 minutes.... The pod keep accepting requests, ignores the configured timeouts, ignores a closed connection by the user; then after 1 minute of liveness probes failing, the pod is restarted by kubelet.

So 1 fat request can dramatically drop the availability.

I see two different solutions:

  1. have a separate deployment that will host only long running api calls; configure ingress to route requests between these two deployments;
  2. using multiprocessing handle liveness/readyness probes in a main process, every other request must be handled in the child process;

There are pros and cons for each solution, maybe I will need a combination of both. Also if I need a steady flow of prometheus metrics, I might need to create a proxy server on the application layer (1 more container on the same pod). Also need to configure ingress to have a single upstream connection to python pods, so that long running request will be queued, whereas short ones will be processed concurrently (yep, python, concurrency, good joke). Not sure tho it will scale well with HPA.

So yeah, running production ready python rest api server on kubernetes is not a piece of cake. Go and java have a much better ecosystem for microservice applications.

PS here is a good article that shows that there is no need to run your app in kubernetes with WSGI https://techblog.appnexus.com/beyond-hello-world-modern-asynchronous-python-in-kubernetes-f2c4ecd4a38d

PPS Im considering to use prometheus exporter for flask. Looks better than running a python client in a separate thread; https://github.com/rycus86/prometheus_flask_exporter

like image 2
dududko Avatar answered Oct 16 '22 23:10

dududko