Skip to main content
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.
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.

Detailed Pod Descriptions

Retrieve a detailed description of the pods to uncover specific container configurations:
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.

Inspecting Specific Pods

The “red” Pod

The red pod runs a single container. Its configuration is shown below when describing the pod:
Since no “Init Containers” section is present, this pod does not include an init container.

The “blue” Pod

The blue pod, on the other hand, includes at least one init container:
In this pod, the init container (named “init-myservice”) successfully executed the sleep command before the main container started.

Deploying a New Pod: “purple”

A new pod named purple is deployed with one container and two init containers. First, verify the pod’s status:
The status “Init:0/2” indicates that two init containers are configured. Inspecting the pod reveals:
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).

Updating a Pod to Add an Init Container

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:
Then, edit the pod using the YAML snippet below:
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.

Troubleshooting a Failing Pod: “orange”

A new pod named orange was deployed but is immediately encountering an init container crash loop. First, check its status:
Inspect the pod description to diagnose the issue. The init container “init-myservice” has an error:
The command “sleepee” is misspelled, causing the init container to fail. To verify, check the init container logs:
The logs confirm the error:
Ensure that commands are correctly spelled in your container specifications because minor typos can prevent your pod from initializing properly.
To resolve the issue, update the pod configuration for orange with the corrected sleep command using the YAML snippet below:
If live edits are rejected, save the modified YAML to a file and force the replacement with:
After replacing the pod, verify that the logs from the init container no longer contain errors:
Then check the pod status:
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:

Watch Video