<p>I have a multi-container pod in k8s, let's call them A and B. When stopping the pod, A must stop before B because A needs B until it's off.</p>
<p>To do that, I registered a <code>preStop</code> hook on A so A can gracefully stop before B.</p>
<p>However I'm not sure this is a good solution, because I miss some information I can't find in k8s documentation:</p>
<p>What happens when a multi-container pod is stopped?</p>
<ul>
<li>All containers <code>preStop</code> hooks are called, then when they are all over all containers receive <code>SIGTERM</code>, or</li>
<li>In parallel, all containers receive <code>preStop</code> if they have one or directly <code>SIGTERM</code> if they don't?</li>
</ul>
<p>In the second case, <code>preStop</code> is useless for what I want to do as B will be instantly killed.</p>

<p>Typically, during pod deletion, the container runtime sends a TERM signal to the main process in each container.</p>
<p>According to <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination" rel="nofollow noreferrer">the official documentation</a>:</p>
<blockquote>
<ol>
<li><p>If one of the Pod's containers has defined a  <code>preStop</code>  <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks" rel="nofollow noreferrer">hook</a>,
the kubelet runs that hook inside of the container.</p>
</li>
<li><p>The kubelet triggers the container runtime to send a TERM signal to process 1 inside each container.</p>
</li>
</ol>
</blockquote>
<p>This numeration can confuse - looks like TERM signal will be sent only after <code>preStop</code>  hook will be finished.
I decided to check the order of work with a simple example below.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  restartPolicy: Never
  volumes:
  - name: config
    configMap:
      name: nginx-conf
  containers:
  - name: container-1
    image: nginx
    lifecycle:
      preStop:
        exec:
          command: [&quot;/bin/sleep&quot;,&quot;15&quot;]
    ports:
    - containerPort: 80
  - name: container-2
    image: nginx
    ports:
    - containerPort: 81
    volumeMounts:
    - name: config
      mountPath: /etc/nginx/conf.d
  terminationGracePeriodSeconds: 30
</code></pre>
<p>Container-1 has <code>preStop</code>  hook for 15 seconds delay.
I've connected to both containers to see behavior during pod deletion.</p>
<p><strong>Result</strong></p>
<p>After pod deletion:</p>
<ol>
<li><p>Container-1 worked for 15 seconds, before the connection was lost</p>
</li>
<li><p>Container-2 immediately lost connection</p>
</li>
</ol>
<p><strong>Conclusion</strong></p>
<p>If the container has a <code>preStop</code>  hook, it will try to execute it. Only then it will receive TERM signal. <em>The main condition in this case: the grace period has not expired.</em></p>
<p>If the container doesn't have a  <code>preStop</code>  hook, it will receive TERM signal immediately after the command to remove the pod. Thus, it will not wait while<code>preStop</code>  hook will be executed for another container.</p>
<blockquote>
<p><strong>Note:</strong> The containers in the Pod receive the TERM signal at different times and in an arbitrary order. If the order of shutdowns
matters, consider using a <code>preStop</code> hook to synchronize.</p>
</blockquote>
