How to Configure Init Containers in a Kubernetes Pod
You are a Kubernetes administrator working on a test application for devopscube. The application Pod has been provided with its main application container, but it requires two initialization steps to be completed before the application starts.
A Pod manifest is available in init.yaml.
apiVersion: v1
kind: Pod
metadata:
name: devopscube-app
spec:
containers:
- command:
- sh
- -c
- echo Welcome to the CKA course && sleep 1000
image: busybox
name: devopscube-app-containerModify the manifest to add the required initialization containers and deploy the updated Pod.
Your task is to configure the Pod devopscube-app with two init containers using the busybox image:
prepare-configsetup-dependencies
Each init container must execute its initialization task, wait for 30 seconds, and print a message identifying the container before completing.
The existing application container must continue to run after both initialization containers complete.
Modify the manifest
Update init.yaml to add two init containers. The init containers run first and complete their tasks before the main application container starts.
Deploy the Pod
Apply the manifest to create the Pod with this command:
$ kubectl apply -f init.yaml
pod/devopscube-app createdkubectl apply --dry-run=client -f init.yamlCheck Init Container Execution
Watch the Pod to see the init containers complete before the main container starts. The status will change from Init:0/2 ,Init:1/2 ,PodInitializing .Running.
$ kubectl get pod devopscube-app -w
NAME READY STATUS RESTARTS AGE
devopscube-app 0/1 Init:0/2 0 3s
devopscube-app 0/1 Init:0/2 0 3s
devopscube-app 0/1 Init:1/2 0 34s
devopscube-app 0/1 Init:1/2 0 37s
devopscube-app 0/1 PodInitializing 0 67s
devopscube-app 1/1 Running 0 70sThe exact intermediate output can vary slightly depending on timing.
What does Init:1/2 mean?
It means there are 2 init containers, and 1 has completed. Kubernetes is now processing the second init container.
Once both init containers complete, the Pod becomes 1/1 Running
This means the main application container has started.
Verify Init Container Status
Use the following command to check the status of both init containers:
$ kubectl get pod devopscube-app \
-o jsonpath='{range .status.initContainerStatuses[*]}{.name}{" => "}{.state.terminated.reason}{"\n"}{end
}'
prepare-config => Completed
setup-dependencies => CompletedThis confirms that both initialization containers successfully completed.
Check Init Container Logs
Check the logs of each init container to verify that they completed their tasks successfully.
$ kubectl logs devopscube-app -c prepare-config
Hello prepare-configGet the second:
$ kubectl logs devopscube-app -c setup-dependencies
Hello setup-dependenciesHow Init Containers Work
An init container runs before the application's normal containers.
For this Pod, prepare-config runs first. After it successfully finishes, setup-dependencies runs. Only after both init containers complete successfully does Kubernetes start the main application container.
If an init container fails, Kubernetes waits for it to succeed before proceeding.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the CKA Init Containers Practice exam.
1. Know the Field Name
Init containers are defined under spec.initContainers, while normal application containers are defined under spec.containers. Knowing the correct field is important when configuring init containers in a Pod manifest.
2. Init Containers Must Complete
Unlike normal application containers, init containers must complete successfully before the main application containers can start.
For example, sleep 30; echo "Hello prepare-config" runs its commands and then exits successfully, allowing the Pod to continue starting.
3. kubectl describe Is Extremely Useful
For CKA troubleshooting, kubectl describe pod techiescamp-app provides detailed information about the Pod, including init container states, container states, events, image-pull problems, startup failures, and scheduling issues.