<p>I'm using Kubernetes replicas to run 'run-to-completion' tasks. </p>

<p>I currently increase the number of replicas when we have a work item to complete on a queue and the container immediately consumes the item. As the desired replicas has never changed, Kubernetes kicks off a new replica which then finds that there's nothing to do and so terminates immediately (and repeat). If I scale down the replicas before a container has finished (i.e. as soon as a queue item is consumed), one of the replicas that are doing work terminates prematurely.</p>

<p>Is there a way to reduce the replicas and not enforce termination?</p>

Matt Brown
<p>You can add the <code>termination grace period</code> for all pods so when container terminates it's wait for some time.</p>

<p>you can also add the SIG term in lifecycle also that particular pod donot take next item from queue.</p>

<pre><code>lifecycle:
          preStop:
            exec:
              # SIGTERM triggers a quick exit; gracefully terminate instead
              command: ["/usr/sbin/nginx","-s","quit"]
</code></pre>

<p>you can also have look at this : <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-terminating-with-grace" rel="nofollow noreferrer">https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-terminating-with-grace</a></p>
