<p>Does Kubernetes support connection draining?</p>

<p>For example, my deployment rolls out a new version of my web app container. 
In connection draining mode Kubernetes should spin up a new container from the new image and route all new traffic coming to my service to this new instance. The old instance should remain alive long enough to send a response for existing connections.</p>

<p>Kubernetes <strong>does</strong> support connection draining, but how it happens is controlled by the Pods, and is called <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="noreferrer">graceful termination</a>.</p>

<h2>Graceful Termination</h2>

<p>Let's take an example of a set of Pods serving traffic through a Service. This is a simplified example, the full details can be found in the <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="noreferrer">documentation</a>.</p>

<ol>
<li>The system (or a user) notifies the API that the Pod needs to stop.</li>
<li>The Pod is set to the <code>Terminating</code> state. This removes it from a Service serving traffic. Existing connections are maintained, but new connections should stop as soon as the load balancers recognize the change.</li>
<li>The system sends SIGTERM to all containers in the Pod.</li>
<li>The system waits <code>terminationGracePeriodSeconds</code> (default 30s), or until the Pod completes on it's own.</li>
<li>If containers in the Pod are still running, they are sent SIGKILL and terminated immediately. At this point the Pod is forcefully terminated if it is still running.</li>
</ol>

<p>This not only covers the simple termination case, but the exact same process is used in rolling update deployments, each Pod is terminated in the exact same way and is given the opportunity to clean up.</p>

<h2>Using Graceful Termination For Connection Draining</h2>

<p><strong>If you do not handle SIGTERM in your app, your Pods will immediately terminate</strong>, since the default action of SIGTERM is to terminate the process immediately, and the grace period is not used since the Pod exits on its own.</p>

<p>If you need "connection draining", this is the basic way you would implement it in Kubernetes:</p>

<ol>
<li>Handle the SIGTERM signal, and clean up your connections in a way that your application decides. This may simply be "do nothing" to allow in-flight connections to clear out. Long running connections may be terminated in a way that is (more) friendly to client applications.</li>
<li>Set the <code>terminationGracePeriodSeconds</code> long enough for your Pod to clean up after itself.</li>
</ol>
