<p>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)</p>

<p>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.</p>

<p>I'm using the command <code>kubectl apply -f $MY_FILE</code> 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. </p>

<p>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 ?</p>

<p>Thank you</p>

<p>You can use preStop hooks to complete ongoing task before the pod is terminated.</p>

<p>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 <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="nofollow noreferrer">Termination of Pods</a>.</p>

<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/#define-poststart-and-prestop-handlers" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/#define-poststart-and-prestop-handlers</a></p>
