<p>Is there a way to get a trigger to shutdown, so we can close all connections gracefully before shutdown and don't proceed any actions after that probe and keeping the probe ready to kill.</p>

<p>This including flushing logs, keeping any state of the application saved before handing over to the new pod and many more use cases.</p>

Kannaiyan
<p>You have 2 options:</p>

<ol>
<li><p>Containers (PID 1) receive SIGTERM before the container (and the pod) is removed. You can trap SIGTERM and act on it.</p></li>
<li><p>You can use the <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#hook-details" rel="nofollow noreferrer">preStop lifecycle hook</a></p></li>
</ol>

<p>Important implementation details can be found here: <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods</a></p>

<h2>httpGet example</h2>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: prestop-pod
spec:
  terminationGracePeriodSeconds: 5
  containers:
    - name: nginx
      image: nginx
      lifecycle:
        preStop:
          httpGet:
            # only port is reqired
            port: 80
            path: "?preStop"
            # scheme: HTTP
            # host: ...
            # httpHeaders:
            #   name: ...
            #   value: ...
</code></pre>

<p>After <code>kubectl apply -f</code> on this file, run <code>kubectl log -f prestop-pod</code> while executing <code>kubectl delete pod prestop-pod</code> on another terminal. You should see something like:</p>

<pre><code>$ kubectl apply -f prestop.yaml 
pod/prestop-pod created
$ kubectl logs -f prestop-pod
10.244.0.1 - - [21/Mar/2019:09:15:20 +0000] "GET /?preStop HTTP/1.1" 200 612 "-" "Go-http-client/1.1" "-"
</code></pre>
