Post

Kubernetes - Init Containers in a Pod

Lab assignment for creating a init container in a pod using kubectl

Kubernetes - Init Containers in a Pod

Prerequisites

  • Kubernetes
  • kubectl

Assignment

  • Lets start by creating a pod which will have a init container which will echo a countdown from 120 to 0 every second.

1️⃣ Create a K8 manifest yaml to deploy a pod with two containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat <<EOF > init-container-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  initContainers:
  - name: init-countdown
    image: busybox
    command: ['sh', '-c', 'for i in \$(seq 90 -1 0); do echo init-countdown: \$i; sleep 1; done']

  containers:
  - name: main-container
    image: busybox
    command: ['sh', '-c', 'while true; do count=\$((count + 1)); echo main-container: sleeping for 30 seconds - iteration \$count; sleep 30; done']
EOF

2️⃣ Create a pod using the manifest file

1
2
3
```sh
kubectl apply -f ./init-container-demo.yaml
```

3️⃣ Verify that the pod was created and check the number of containers under Ready column

1
2
3
```sh
kubectl get pods -o wide
```

4️⃣ Lets watch the logs from init container first and then the logs from the main container.

1
2
3
```sh
until kubectl logs pod/mypod -c init-countdown --follow --pod-running-timeout=5m; do sleep 1; done; until kubectl logs pod/mypod -c main-container --follow --pod-running-timeout=5m; do sleep 1; done
```

5️⃣ Exit the main container using Ctrl+c and check if the init container pod has exited.

1
2
3
```sh
kubectl get pods -o wide
```

CleanUp

1
2
kubectl delete -f init-container-demo.yaml --now
rm init-container-demo.yaml
This post is licensed under CC BY 4.0 by the author.