<p>I'm working on Python application which is deployed as &quot;Deployment&quot; on k8s. The application scales up based on SQS messages number.
When messages count is zero, the pods are terminated without completing processing.
How to allow a pod to continue processing till finish running request processing based on memory and cpu usage?</p>

Nesrin
<p>To allow a pod to continue processing till finish running request processing you can use below suggestions:</p>
<p>1.Use prestop and graceful shutdown to delay the termination of pod, Resulting:</p>
<ul>
<li>Application will wait a few seconds and then it stops accepting new connections.</li>
<li>An application will wait until all requests are complete. And close all idle keepalive connections.</li>
</ul>
<p>You can find more information about it in below blogs:</p>
<ul>
<li><a href="https://blog.palark.com/graceful-shutdown-in-kubernetes-is-not-always-trivial/" rel="nofollow noreferrer">Blog - Graceful shutdown</a> by Ilya Andreev with NGINX example.</li>
<li><a href="https://ubuntu.com/blog/avoiding-dropped-connections-in-nginx-containers-with-stopsignal-sigquit" rel="nofollow noreferrer">Blog Graceful shutdown</a> by Robin Winslow.</li>
</ul>
<p>2.You can use HPA to scale the deployment based on memory and cpu usage, so that one pod will remain active to take requests. You can find info related to scaling deployment via CPU and Memory in this <a href="https://granulate.io/blog/kubernetes-autoscaling-the-hpa/#:%7E:text=of%20the%20cluster.-,HPA%20Example%3A%20Scaling%20a%20Deployment%20via%20CPU%20and%20Memory%20Metrics,-The%20following%20is" rel="nofollow noreferrer">blog - HPA</a> authored by Naom Salinger.</p>
<p>3.In addition, you can also use <a href="https://www.alibabacloud.com/help/en/container-service-for-kubernetes/latest/horizontal-expansion-of-containers-based-on-rabbitmq-indicator" rel="nofollow noreferrer">Horizontal pod autoscaling based on the metrics of Message Queue for RabbitMQ</a>.</p>
