Kubernetes Init Containers

Basic Demo: Init Containers That Just Sleeps (Too Slow)

nano myInitPod-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 app is running! && sleep 3600']
initContainers:
- name: my-init-container-1
image: busybox
command: ['sh', '-c', 'echo my-init-container-1 start; sleep 2;echo my-init-container-1 complete;']
- name: my-init-container-2
image: busybox
command: ['sh', '-c', 'echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;']
kubectl create -f myInitPod-1.yaml 

pod/myapp-pod created
kubectl get po  
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 3s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 6s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 9s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 13s
kubectl get po  
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:1/2 0 22s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 PodInitializing 0 24s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 PodInitializing 0 29s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 PodInitializing 0 30s

NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 33s
kubectl logs pod/myapp-pod -c my-init-container-1 --timestamps=true2019-01-05T05:59:11.009868369Z my-init-container-1 start
2019-01-05T05:59:13.013553431Z my-init-container-1 complete
kubectl logs pod/myapp-pod -c my-init-container-2 --timestamps=true2019-01-05T05:59:18.41524046Z my-init-container-2 start
2019-01-05T05:59:20.417166592Z my-init-container-2 complete
kubectl logs pod/myapp-pod --timestamps=true2019-01-05T05:59:29.795805962Z The app is running!
kubectl describe pod/myapp-pod Status:             Running
my-init-container-1:
echo my-init-container-1 start; sleep 2;echo my-init-container-1 complete;
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 05 Jan 2019 07:59:11 +0200
Finished: Sat, 05 Jan 2019 07:59:13 +0200
Ready: True
my-init-container-2:
echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 05 Jan 2019 07:59:18 +0200
Finished: Sat, 05 Jan 2019 07:59:20 +0200
Ready: True
myapp-container:
State: Running
Started: Sat, 05 Jan 2019 07:59:29 +0200
Ready: True
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 82s default-scheduler Successfully assigned default/myapp-pod to minikube
Normal Pulling 81s kubelet, minikube pulling image "busybox"
Normal Pulled 70s kubelet, minikube Successfully pulled image "busybox"
Normal Created 70s kubelet, minikube Created container
Normal Started 69s kubelet, minikube Started container
Normal Pulling 67s kubelet, minikube pulling image "busybox"
Normal Pulled 62s kubelet, minikube Successfully pulled image "busybox"
Normal Created 62s kubelet, minikube Created container
Normal Started 62s kubelet, minikube Started container
Normal Pulling 60s kubelet, minikube pulling image "busybox"
Normal Pulled 51s kubelet, minikube Successfully pulled image "busybox"
Normal Created 51s kubelet, minikube Created container
Normal Started 51s kubelet, minikube Started container
my-init-container-1:
echo my-init-container-1 start; sleep 2;echo my-init-container-1 complete;
State: Terminated
Reason: Completed
Exit Code: 0
myapp-container:
State: Running
Started: Sat, 05 Jan 2019 07:59:29 +0200
Ready: True
myapp-container:
State: Running
Started: Sat, 05 Jan 2019 07:59:29 +0200
Ready: True
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True

Why This Demo Took 30 Seconds?

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 82s default-scheduler Successfully assigned default/myapp-pod to minikube
Normal Pulling 81s kubelet, minikube pulling image "busybox"
Normal Pulled 70s kubelet, minikube Successfully pulled image "busybox"
Normal Created 70s kubelet, minikube Created container
Normal Started 69s kubelet, minikube Started container
Normal Pulling 67s kubelet, minikube pulling image "busybox"
Normal Pulled 62s kubelet, minikube Successfully pulled image "busybox"
Normal Created 62s kubelet, minikube Created container
Normal Started 62s kubelet, minikube Started container
Normal Pulling 60s kubelet, minikube pulling image "busybox"
Normal Pulled 51s kubelet, minikube Successfully pulled image "busybox"
Normal Created 51s kubelet, minikube Created container
Normal Started 51s kubelet, minikube Started container

Basic Demo: Init Containers That Just Sleeps

kubectl delete pod/myapp-pod 

pod "myapp-pod" deleted
nano myInitPod-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 app is running! && sleep 3600']
terminationGracePeriodSeconds: 0
initContainers:
- name: my-init-container-1
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-1 start; sleep 2;echo my-init-container-1 complete;']
- name: my-init-container-2
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;']
kubectl create -f myInitPod-2.yaml 

pod/myapp-pod created
kubectl describe pod/myapp-pod 

Name: myapp-pod
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: minikube/10.0.2.15
Start Time: Sat, 05 Jan 2019 08:33:03 +0200
Labels: app=myapp
Annotations: <none>
Status: Running
IP: 172.17.0.5
Init Containers:
my-init-container-1:
Container ID: docker://5175ac45ec3f9664d511db083f1b15e817cc4c9ac28be31f6ae356583cf1efcb
Image: busybox
Image ID: docker-pullable://busybox@sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0
Port: <none>
Host Port: <none>
Command:
sh
-c
echo my-init-container-1 start; sleep 2;echo my-init-container-1 complete;
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 05 Jan 2019 08:33:04 +0200
Finished: Sat, 05 Jan 2019 08:33:06 +0200
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gs2wt (ro)
my-init-container-2:
Container ID: docker://0b20b8bbc582663fdd8c5050e96268497095406b024f9c5c84e518e6818b8e16
Image: busybox
Image ID: docker-pullable://busybox@sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0
Port: <none>
Host Port: <none>
Command:
sh
-c
echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 05 Jan 2019 08:33:07 +0200
Finished: Sat, 05 Jan 2019 08:33:09 +0200
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gs2wt (ro)
Containers:
myapp-container:
Container ID: docker://ad93ff9d3033f694a93b1656768f6ac58e62278cc608c3b5d33ac793fc3779cb
Image: busybox
Image ID: docker-pullable://busybox@sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0
Port: <none>
Host Port: <none>
Command:
sh
-c
echo The app is running! && sleep 3600
State: Running
Started: Sat, 05 Jan 2019 08:33:10 +0200
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gs2wt (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-gs2wt:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gs2wt
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 42s default-scheduler Successfully assigned default/myapp-pod to minikube
Normal Pulled 42s kubelet, minikube Container image "busybox" already present on machine
Normal Created 41s kubelet, minikube Created container
Normal Started 41s kubelet, minikube Started container
Normal Pulled 38s kubelet, minikube Container image "busybox" already present on machine
Normal Created 38s kubelet, minikube Created container
Normal Started 38s kubelet, minikube Started container
Normal Pulled 35s kubelet, minikube Container image "busybox" already present on machine
Normal Created 35s kubelet, minikube Created container
Normal Started 35s kubelet, minikube Started container
kubectl delete -f myInitPod-2.yaml

pod "myapp-pod" force deleted
kubectl get po 

No resources found.

Basic Demo: Init Containers That Crash Forever

command: ['sh', '-c', 'echo my-init-container-1 start; exit 1 ;echo my-init-container-1 complete;']nano myInitPod-3.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 app is running! && sleep 3600']
terminationGracePeriodSeconds: 0
initContainers:
- name: my-init-container-1
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-1 start; exit 1 ;echo my-init-container-1 complete;']
- name: my-init-container-2
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;']
kubectl get poNAME        READY   STATUS                  RESTARTS   AGE
myapp-pod 0/1 Init:CrashLoopBackOff 1 5s
kubectl describe pod/myapp-pod Events:
Type Reason Age From Message
Normal Scheduled 21s default-scheduler Successfully assigned default/myapp-pod to minikube
Normal Pulled 3s (x3 over 20s) kubelet, minikube Container image "busybox" already present on machine
Normal Created 3s (x3 over 20s) kubelet, minikube Created container
Normal Started 3s (x3 over 20s) kubelet, minikube Started container
kubectl get poNAME        READY   STATUS                  RESTARTS   AGE
myapp-pod 0/1 Init:CrashLoopBackOff 3 89s
kubectl delete -f myInitPod-3.yaml --force --grace-period=0 

pod "myapp-pod" force deleted
kubectl get po No resources found.

Basic Demo: Init Containers That Crash Just Once

nano myInitPod-4.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 app is running! && sleep 3600']
terminationGracePeriodSeconds: 0
restartPolicy: Never

initContainers:
- name: my-init-container-1
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-1 start; exit 1 ;echo my-init-container-1 complete;']
- name: my-init-container-2
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;']
kubectl create -f myInitPod-4.yaml

pod/myapp-pod created
kubectl get poNAME        READY   STATUS       RESTARTS   AGE
myapp-pod 0/1 Init:Error 0 3s
NAME        READY   STATUS       RESTARTS   AGEmyapp-pod   0/1     Init:Error   0          28s
kubectl describe pod/myapp-pod Status:             Failed
my-init-container-1:
echo my-init-container-1 start; exit 1 ;echo my-init-container-1 complete;
State: Terminated
Reason: Error
Exit Code: 1
Started: Sat, 05 Jan 2019 09:08:06 +0200
Finished: Sat, 05 Jan 2019 09:08:06 +0200
Ready: False
my-init-container-2:
echo my-init-container-2 start; sleep 2;echo my-init-container-2 complete;
State: Waiting
Reason: PodInitializing
Ready: False
myapp-container:
State: Waiting
Reason: PodInitializing
Ready: False
Conditions:
Type Status
Initialized False
Ready False
ContainersReady False
PodScheduled True
Events:
Type Reason Age From Message
Normal Scheduled 16s default-scheduler Successfully assigned default/myapp-pod to minikube
Normal Pulled 16s kubelet, minikube Container image "busybox" already present on machine
Normal Created 16s kubelet, minikube Created container
Normal Started 15s kubelet, minikube Started container
my-init-container-1:
echo my-init-container-1 start; exit 1 ;echo my-init-container-1 complete;
State: Terminated
Reason: Error
Exit Code: 1
kubectl delete -f myInitPod-4.yaml --force --grace-period=0pod "myapp-pod" force deleted
kubectl get poNo resources found.

Realistic Production Init Container Simulation

nano myInitPod-7.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 app is running! && sleep 10']

restartPolicy: Never

initContainers:
- name: my-init-container-1
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-1 start; sleep 3 ;echo my-init-container-1 complete;']
- name: my-init-container-2
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-2 start; sleep 3;echo my-init-container-2 complete;']
- name: my-init-container-3
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-3 start; sleep 3;echo my-init-container-3 complete;']
- name: my-init-container-4
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c', 'echo my-init-container-4 start; sleep 3;echo my-init-container-4 complete;']
kubectl create -f myInitPod-7.yaml 

pod/myapp-pod created
kubectl get po 

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/4 0 3s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:1/4 0 6s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:2/4 0 9s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:2/4 0 11s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:3/4 0 15s
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 19s

NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 22s
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 26s

NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Completed 0 30s
kubectl delete -f myInitPod-7.yamlpod "myapp-pod" deleted
kubectl get poNo resources found.
myapp-container:
State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 05 Jan 2019 10:37:06 +0200
Finished: Sat, 05 Jan 2019 10:37:16 +0200
Ready: False

Clean Up

--

--

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