Pod Lifecycle, Container Lifecycle, Hooks and restartPolicy

Simple Pod — Sleep 6 Seconds

nano myLifecyclePod-1.yaml

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The Pod is running && sleep 6']
kubectl create -f myLifecyclePod-1.yaml 

pod/myapp-pod created
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 2s
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 4s
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 7s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 10s
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 09:04:28 +0200
Finished: Tue, 08 Jan 2019 09:04:34 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f myLifecyclePod-1.yaml --force --grace-period=0warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "myapp-pod" force deleted

Simple Pod — Exit 1 ( Error ) restartPolicy: Never

nano myLifecyclePod-2.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && exit 1']
restartPolicy: Never
kubectl create -f myLifecyclePod-2.yaml 

pod/myapp-pod created
Status:             Failed
myapp-container:
State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 09:14:20 +0200
Finished: Tue, 08 Jan 2019 09:14:20 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f myLifecyclePod-2.yaml --force --grace-period=0warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "myapp-pod" force deleted

Simple Pod — Peek into Early Status

kubectl create -f myLifecyclePod-1.yaml 

pod/myapp-pod created
Status:             Running
myapp-container:
State: Running
Started: Tue, 08 Jan 2019 09:17:51 +0200
Ready: True
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 09:17:51 +0200
Finished: Tue, 08 Jan 2019 09:17:57 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Status:             Failed
myapp-container:
State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 09:14:20 +0200
Finished: Tue, 08 Jan 2019 09:14:20 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f myLifecyclePod-1.yaml --force --grace-period=0pod "myapp-pod" force deleted

Simple Pod — Exit 1 ( Error ) restartPolicy: Always

nano myLifecyclePod-3.yaml 

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && exit 1']
restartPolicy: Always
kubectl create -f myLifecyclePod-3.yaml 

pod/myapp-pod created
NAME        READY   STATUS   RESTARTS   AGE
myapp-pod 0/1 Error 1 4s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 1 9s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 1 15s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Error 2 19s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Error 2 28s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 2 36s
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Error 3 51s
Status:             Running
myapp-container:
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 09:34:51 +0200
Finished: Tue, 08 Jan 2019 09:34:51 +0200
Ready: False
Restart Count: 3
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Status:             Running
myapp-container:
State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 09:35:32 +0200
Finished: Tue, 08 Jan 2019 09:35:32 +0200
Last State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 09:34:51 +0200
Finished: Tue, 08 Jan 2019 09:34:51 +0200
Ready: False
Restart Count: 4
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f myLifecyclePod-3.yaml --force --grace-period=0pod "myapp-pod" force deleted

restartPolicy: Always : Pod Sleep 1 Second

Restart policy

A PodSpec has a restartPolicy field with possible values Always, OnFailure, and Never.

The default value is Always. restartPolicy applies to all Containers in the Pod

nano myLifecyclePod-4.yaml

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && sleep 1']
restartPolicy: Always
kubectl create -f myLifecyclePod-4.yaml 

pod/myapp-pod created
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 3s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 1 6s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 1 9s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 1 13s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 1 15s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 2 20s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 2 23s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 2 28s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 CrashLoopBackOff 2 35s
kubectl delete -f myLifecyclePod-4.yaml --force --grace-period=0pod "myapp-pod" force deleted

restartPolicy: OnFailure : Pod sleep 1 Second

nano myLifecyclePod-5.yaml

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && sleep 1']
restartPolicy: OnFailure
kubectl create -f myLifecyclePod-5.yaml

pod/myapp-pod created
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 3s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 6s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 10s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 16s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 21s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 30s
kubectl delete -f myLifecyclePod-5.yaml --force --grace-period=0pod "myapp-pod" force deleted

restartPolicy: Never : Pod Sleep 1 Second

nano myLifecyclePod-6.yaml

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && sleep 1']
restartPolicy: Never
kubectl create -f myLifecyclePod-6.yaml

pod/myapp-pod created
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 3s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 9s
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 18s
kubectl delete -f myLifecyclePod-6.yaml --force --grace-period=0pod "myapp-pod" force deleted

restartPolicy: Never : Pod Exits with Error

nano myLifecyclePod-7.yaml

apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo The Pod is running && exit 1']
restartPolicy: Never
kubectl create -f myLifecyclePod-7.yaml

pod/myapp-pod created
kubectl get po
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Error 0 8s
Status:             Failed
myapp-container:
State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 08 Jan 2019 10:00:34 +0200
Finished: Tue, 08 Jan 2019 10:00:34 +0200
Ready: False
Restart Count: 0
kubectl delete -f myLifecyclePod-7.yaml --force --grace-period=0pod "myapp-pod" force deleted

Container Lifecycle Hooks

Simplest Case: Working Pod

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running && sleep 5']

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:09:21 +0200
Finished: Tue, 08 Jan 2019 11:09:26 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

postStart Sleep 10 Seconds

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running && sleep 5']

lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "sleep 10"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:12:22 +0200
Finished: Tue, 08 Jan 2019 11:12:27 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Warning FailedPostStartHook 2s kubelet, minikube Exec lifecycle hook ([/bin/sh -c sleep 10]) for Container "myapp-container" in Pod "myapp-pod_default(812c05ce-1325-11e9-91d6-0800270102d2)" failed - error: command '/bin/sh -c sleep 10' exited with 137: , message: ""
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

postStart Echo to Termination-log

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running && sleep 5']

lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo In postStart > /dev/termination-log"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed

Message: In postStart
Exit Code: 0
Started: Tue, 08 Jan 2019 11:17:25 +0200
Finished: Tue, 08 Jan 2019 11:17:30 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

preStop Echo to /dev/termination-log

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running && sleep 5']

lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "echo In preStop > /dev/termination-log"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:24:10 +0200
Finished: Tue, 08 Jan 2019 11:24:15 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

preStop Sleeps 10 Seconds

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running && sleep 5']

lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 10"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status:             Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:27:30 +0200
Finished: Tue, 08 Jan 2019 11:27:35 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

Multiple preStop Commands

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running']
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 1 ; echo In preStop > /dev/termination-log"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status: Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:34:39 +0200
Finished: Tue, 08 Jan 2019 11:34:39 +0200
Ready: False
Restart Count: 0
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

Multiple postStart and preStop Commands

nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running']
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "sleep 1 ; echo In postStart > /dev/termination-log"]
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 1 ; echo In preStop > /dev/termination-log"]

restartPolicy: Never
kubectl create -f mypostStartPod.yaml

pod/myapp-pod created
Status: Succeeded
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Tue, 08 Jan 2019 11:40:29 +0200
Finished: Tue, 08 Jan 2019 11:40:29 +0200
Ready: False
Restart Count: 0
Warning FailedPostStartHook 3s kubelet, minikube Exec lifecycle hook ([/bin/sh -c sleep 1 ; echo In postStart > /dev/termination-log]) for Container "myapp-container" in Pod "myapp-pod_default(6e795f76-1329-11e9-91d6-0800270102d2)" failed - error: command '/bin/sh -c sleep 1 ; echo In postStart > /dev/termination-log' exited with 126: , message: "cannot exec in a stopped state: unknown\r\n" Warning FailedPreStopHook 3s kubelet, minikube Exec lifecycle hook ([/bin/sh -c sleep 1 ; echo In preStop > /dev/termination-log]) for Container "myapp-container" in Pod "myapp-pod_default(6e795f76-1329-11e9-91d6-0800270102d2)" failed - error: command '/bin/sh -c sleep 1 ; echo In preStop > /dev/termination-log' exited with 126: , message: "cannot exec in a stopped state: unknown\r\n"
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

Multiple postStart and preStop Commands — Sleep 10 Pod

command: ['sh', '-c', 'echo The Pod is running ; sleep 10']
nano mypostStartPod.yamlapiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container

image: busybox
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo The Pod is running ; sleep 10']
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "sleep 1 ; echo In postStart > /dev/termination-log"]
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 1 ; echo In preStop > /dev/termination-log"]

restartPolicy: Never
Message:  In postStart  
Started: Tue, 08 Jan 2019 14:31:21 +0200
Finished: Tue, 08 Jan 2019 14:31:31 +0200
kubectl delete -f mypostStartPod.yaml --force --grace-period=0pod "myapp-pod" force deleted

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store