<p>I want to implement graceful shutdown in a Kubernetes Pod. I know I need to listen for SIGTERM, which indicates the start of the shutdown procedure. But what exactly do I do when I receive it?</p>

<p>At the very least I must wait for all running request to finish before exiting. But can the pod still receive new requests after receiving the SIGTERM? (It's exposed using a service.) I can't find any explicit documentation on this.</p>

<p><a href="http://kubernetes.io/docs/user-guide/pods/#termination-of-pods" rel="noreferrer">The docs</a> state:</p>

<blockquote>
  <p>Pod is removed from endpoints list for service, and are no longer considered part of the set of running pods for replication controllers. Pods that shutdown slowly can continue to serve traffic as load balancers (like the service proxy) remove them from their rotations.</p>
</blockquote>

<p>So that seems to imply that new requests can still come in. So how long should I continue to expect new requests before graceful termination? Do I simply ignore the SIGTERM, continue to serve requests as usual and wait for the eventual SIGKILL?</p>

<p>I suppose ensuring future readiness checks fail and then waiting longer than the period with which they occur before terminating might work?</p>

<p>I'm on Kubernetes 1.2.5, if that makes any difference, and am talking about rolling updates in particular, but also scaling replication controllers down generally.</p>

<p>I recently faced similar problem, I used simple preStop hook, which introduces some delay(sleep) between start of termination and receiving SIGTERM to underlying process</p>

<pre><code>lifecycle:
        preStop:
          exec:
            command:
              - "sleep"
              - "60"
</code></pre>

<p>This delay helps,</p>

<ol>
<li><p>Load balancer to remove(sync) the pod being terminated</p></li>
<li><p>Gives chance to terminating pod to complete requests received before termination</p></li>
<li><p>Fulfill the requests received by terminating pod between termination and load balancer update(sync)</p></li>
</ol>

<p>PreStop can be made more intelligent for pod with unpredictable time of serving</p>
