<p>I need to let the container to run 5 minutes after the <code>kubectl</code> ' termination. It needs to do some work before it's destroyed. It seems that kubernetes contains exactly what I need:</p>

<pre><code>terminationGracePeriodSeconds: 300
</code></pre>

<p>so I defined it within my yaml. I've updated running <code>RCs</code>, delete current pods so new ones were created and now I can see that a pod contains exactly this setting via <code>get pod xyz -o=yaml</code>.</p>

<p>Unfortunately, when I tried to do <code>rolling-update</code>, the original pod was killed after exactly 1 minute, not after 5 minutes. I does ssh to the target machine and I could see that docker termineted the container after this time.</p>

<p>I tried to do some investigation how the feature works. I finally found the documentation to <code>kubectl delete</code> where there is a notion about graceful termination period:</p>

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

<blockquote>
  <p>By default, all deletes are graceful within 30 seconds. The kubectl delete command supports the --grace-period= option which allows a user to override the default and specify their own value. The value 0 indicates that delete should be immediate, and removes the pod in the API immediately so a new pod can be created with the same name. On the node pods that are set to terminate immediately will still be given a small grace period before being force killed</p>
</blockquote>

<p>So I took one pod, nginx, and try to delete it with <code>grace-period=30</code>. It turned out, that original pod was immediately delete and <code>get pods</code> showed that new one was being started.</p>

<p>So no 30 seconds. What am I doing wrong? It seems that all pods kubernetes does not take these values into account.
Note that I'm using kubernetes v1.2.2</p>

<p>I also found this issue <a href="https://github.com/kubernetes/kubernetes/issues/24695" rel="noreferrer">https://github.com/kubernetes/kubernetes/issues/24695</a> where the reporter had same problem and he solved it in the same fashion. So e.g. 300 seconds is not too much for kubernetes.</p>

<p>You can use the <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks" rel="nofollow noreferrer">container lifecycle hook</a> <code>preStop</code> to have your pod sleep before terminating. This hook will be executed prior to <code>kubectl</code> sending <code>SIGTERM</code> to your container.</p>
<p>Example configuration from <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/" rel="nofollow noreferrer">the docs</a>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      preStop:
        exec:
          command: [&quot;/bin/sleep&quot;,&quot;300&quot;]
</code></pre>
