<p>Are there any hooks available for Pod lifecycle events? Specifically, I want to run a command to upload logs on pod restart. </p>

Mohit Gupta
<p><strong>Edit: PreStop hook doesn't work for container restart - please see rest of answer below</strong> </p>

<p>As standing in <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/" rel="nofollow noreferrer">documentation</a> there are <code>PreStop</code> and <code>PostStart</code> events and you can attach to them.</p>

<p>Example from docs:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler &gt; /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
</code></pre>

<p>Edit:
So I checked with following POC if that preStop hook is executed on container crash and conclusion is: <strong>NOT</strong></p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    volumeMounts:
    - mountPath: /data
      name: test-volume
    image: nginx
    command: ["/bin/sh"]
    args: ["-c", "sleep 5; exit 1"]
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler &gt; /data/postStart"]
      preStop:
        exec:
          command: ["/bin/sh","-c","echo preStop handler! &gt; /data/preStop"]

  volumes:
  - name: test-volume
    hostPath:
      path: /data
      type: Directory
</code></pre>

<p>As solution for you I would recommend to override command section for you container this way:</p>

<pre><code>command: ["/bin/sh"]
args: ["-c", "your-application-executable; your-logs-upload"]
</code></pre>

<p>so your-logs-upload executable will be executed after your-application-executable crash/end</p>
