With Kubernetes Is there a way to wait for a pod to finish its ongoing tasks before updating it?

4/7/2020

I'm managing a application inside kubernetes, I have a front end (nginx, flask) and a backend (celery) Long running tasks are sent to the backend using a middle-ware (rabbitmq)

My issue here is that i can receive long running tasks at anytime, and i don't want it to disturb my plan of upgrading the version of my application.

I'm using the command kubectl apply -f $MY_FILE to deploy/update my application. But if i do it when a celery po is busy, the pod will be terminated, and i'll be losing the task.

I tried using the readiness probe, but the pods are still being terminated. My question is, is there a way for kube to target only 'free' pods, and wait for the busy on to finish ?

Thank you

-- Titouan THEOPHILE
celery
deployment
kubernetes

2 Answers

4/7/2020

One way is to create another deployment with the new image, expose it as a service. Pass on any new requests ONLY to this new deployment/service.

Meanwhile, the old deployment/service can still continue processing the existing requests and not take any new ones. Once all the requests are processed the old deployment/service can be deleted.

The only problem with this approach, roughly double the resources are required for some duration as old/new deployment/service run in parallel.

Something like a A/B testing. FYI ... Istio makes is easy with traffic management.

-- Praveen Sripati
Source: StackOverflow

4/7/2020

You can use preStop hooks to complete ongoing task before the pod is terminated.

Kubernetes sends the preStop event immediately before the Container is terminated. Kubernetes’ management of the Container blocks until the preStop handler completes, unless the Pod’s grace period expires.For more details, see Termination of Pods.

https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/#define-poststart-and-prestop-handlers

-- Arghya Sadhu
Source: StackOverflow