<p>I have an application that I deploy on Kubernetes. </p>

<p>This application has 4 replicas and I'm doing a rolling update on each deployment.</p>

<p>This application has a graceful shutdown which can take tens of minutes (it has to wait for running tasks to finish).</p>

<p>My problem is that during updates, I have over-capacity since all the older version pods are stuck at "Terminating" status while all the new pods are created.</p>

<p>During the updates, I end up running with 8 containers and it is something I'm trying to avoid.</p>

<p>I tried to set <code>maxSurge</code> to 0, but this setting doesn't take into consideration the "Terminating" pods, so the load on my servers during the deployment is too high.</p>

<p>The behaviour I'm trying to get is that new pods will only get created after the old version pods finished successfully, so at all times I'm not exceeding the number of replicas I set.</p>

<p>I wonder if there is a way to achieve such behaviour.</p>

RamenCoder
<p>What I ended up doing is creating a <code>StatefulSet</code> with <code>podManagementPolicy: Parallel</code> and <code>updateStrategy</code> to <code>OnDelete</code>.</p>

<p>I also set <code>terminationGracePeriodSeconds</code> to the maximum time it takes for a pod to terminate.</p>

<p>As a part of my deployment process, I apply the new <code>StatefulSet</code> with the new image and then delete all the running pods.</p>

<p>This way all the pods are entering <code>Terminating</code> state and whenever a pod finished its task and terminated a new pod with the new image will replace it. </p>

<p>This way I'm able to keep a static number of replicas during the whole deployment process.</p>
