This guide provides a step-by-step lab on Kubernetes init containers, covering pod configurations, updates, and troubleshooting.
This guide walks you through a step-by-step lab on Kubernetes init containers. You will learn how to inspect pod configurations, differentiate between regular and init containers, update pod specifications to include an init container, and troubleshoot a failing pod.
Identifying Pods and Their Container Configurations
Begin by listing all pods in your cluster. In this lab, there are three pods: red, green, and blue.
Copy
Ask AI
controlplane ~ ☸️ k get podsNAME READY STATUS RESTARTS AGEred 1/1 Running 0 23sgreen 2/2 Running 0 23sblue 1/1 Running 0 23scontrolplane ~ ~
From the output, note that the pod named green is running two containers. However, to identify if a pod includes an init container, further inspection is required.
Retrieve a detailed description of the pods to uncover specific container configurations:
Copy
Ask AI
controlplane ~ ⚡ k get podsNAME READY STATUS RESTARTS AGEred 1/1 Running 0 23sgreen 2/2 Running 0 23sblue 1/1 Running 0 23scontrolplane ~ ⚡
Using the kubectl describe pod command, you can view sections such as command, state, mounts, conditions, volumes, and events. Regular containers are listed under the “Containers” section, while init containers appear separately under “Init Containers”.For example, reviewing the blue pod displays the following snippet:
In this example, the blue pod’s init container uses the BusyBox image and executes a sleep command for 5 seconds before completing successfully (exit code 0). In contrast, the red and green pods only have regular containers listed under the “Containers” section.
A new pod named purple is deployed with one container and two init containers. First, verify the pod’s status:
Copy
Ask AI
k get pod purple
Copy
Ask AI
NAME READY STATUS RESTARTS AGEpurple 0/1 Init:0/2 0 11s
The status “Init:0/2” indicates that two init containers are configured. Inspecting the pod reveals:
Copy
Ask AI
Containers: purple-container: Command: sh -c echo The app is running! && sleep 3600 State: Waiting Reason: PodInitializing...Init Containers: warm-up-1: Command: sh -c sleep 600 State: Running Started: Sun, 17 Apr 2022 18:55:00 +0000 Ready: False warm-up-2: Command: sh -c sleep 1200 State: Waiting Reason: PodInitializing Ready: False
The main purple application container will not start until both init containers complete their execution. In this example, the total wait time is 30 minutes (10 minutes + 20 minutes).
Next, update the pod specification for the red pod to include an init container that uses the BusyBox image to sleep for 20 seconds. First, check the current state of the red pod:
Copy
Ask AI
k get pod red
Copy
Ask AI
NAME READY STATUS RESTARTS AGEred 1/1 Running 0 5m52s
After saving the changes, the pod will terminate and recreate with the updated specification. If the update is rejected because the pod does not accept live updates, force the replacement.
If live edits are rejected, save the modified YAML to a file and force the replacement with:
Copy
Ask AI
k replace --force -f <file-with-updated-pod-spec.yaml>
After replacing the pod, verify that the logs from the init container no longer contain errors:
Copy
Ask AI
k logs orange -c init-myservice
Then check the pod status:
Copy
Ask AI
k get pods orange
Copy
Ask AI
NAME READY STATUS RESTARTS AGEorange 1/1 Running 0 27s
Now the init container terminates cleanly (exit code 0) and the main application container starts running normally.This concludes the Kubernetes init containers lab. In this lesson, you learned to inspect pod configurations, differentiate between regular and init containers, update a pod to include an init container, and troubleshoot configuration errors due to command typos.For more information on Kubernetes concepts, check out these resources: