An init container is a special container that runs before the main application containers in a Pod. Init containers are defined in spec.initContainers and are designed to perform setup tasks that must complete successfully before the application starts.
Key properties:
Run sequentially in the order they appear in the YAML
Each init container must exit with code 0 before the next one starts
If any init container fails, Kubernetes restarts the Pod according to restartPolicy
Main containers do not start until all init containers succeed
Exam Trap: Init containers do not support livenessProbe, readinessProbe, or startupProbe. If you see these fields in an init container spec, the Pod will fail validation. Source: CKA Day 11
Common Use Cases
Use Case
Example
Dependency waiting
Poll a database or API until it is reachable
Configuration generation
Render templates from ConfigMaps/Secrets into files
Permission fixes
chmod or chown on shared volumes before the main app starts
Schema migrations
Run database migrations before application startup
Secrets decryption
Decrypt secrets from a KMS into a shared volume
Network setup
Configure iptables rules or service mesh sidecar injection
wait-for-db polls the database port until it is open
run-migrations applies database schema changes
Only then does the app container start
Resource Considerations
Init containers affect Pod scheduling because Kubernetes uses the highest resource request/limit across all init and regular containers to determine the Pod’s total resource requirement.
Sum of all regular container requests = runtime requirement
Best practice: Keep init containers lightweight. A heavy init container can cause unnecessary scheduling pressure (large node requirements) even though it only runs briefly. Source: CKA Day 11
Troubleshooting Init Containers
Check Pod Status
kubectl get pod init-demo# STATUS: Init:0/2 → 0 of 2 init containers have completed# STATUS: Init:1/2 → 1 of 2 completed, waiting on the second# STATUS: Init:Error → The current init container failed
Read Init Container Logs
# Get logs from a specific init containerkubectl logs init-demo -c wait-for-db# If the pod has restarted, get logs from the previous instancekubectl logs init-demo -c wait-for-db --previous
Describe for Events
kubectl describe pod init-demo
Look for:
Init Containers section showing each init container’s state
Events showing Created container, Started container, or Back-off restarting failed container
Common Failure Modes
Failure
Cause
Fix
Init:CrashLoopBackOff
Init container exits non-zero
Fix the script/command; add retries or timeouts
Init:0/1 forever
Waiting condition never satisfied
Verify dependencies are running and reachable
ImagePullBackOff on init
Wrong image name or registry auth
Check image name, tags, and pull secrets
Permission denied
Init container runs as non-root but needs root
Use securityContext.runAsUser: 0 if appropriate
CKA Exam Patterns
# Quickly check init container statuskubectl get pod my-pod -o jsonpath='{range .status.initContainerStatuses[*]}{.name}{"\t"}{.state}{"\n"}{end}'# Create a Pod with an init container from an existing deploymentkubectl run my-pod --image=nginx --restart=Never --dry-run=client -o yaml > pod.yaml# Then manually add spec.initContainers section
Exam Tip: If a Pod is stuck and you suspect an init container, always check kubectl describe pod first. The Init Containers section shows exactly which init container is running or failed. Source: CKA Day 11
Practical Practice
Exam-style hands-on tasks for this topic. Complete each task before reviewing the solution. Time yourself — CKA tasks average 5–7 minutes.
Task 1: Init Container Creating Shared Data
You are asked to create a Job named init-data-job where an init container writes a file /shared/data.txt with content “hello”, and the main container reads and outputs that file.
Requirements: Use an emptyDir volume mounted at /shared in both containers.
Verification:kubectl logs job/init-data-job shows “hello”.
Solution:
Task 2: Debug an Init Container Failure
A Pod has an init container stuck in Init:Error. Find the failing init container, inspect its logs, and fix the command so it exits 0.
Requirements: Do not delete the Pod. Identify the bad command from logs and edit the Pod manifest.
Verification:kubectl get pod <pod> reaches Running status.
Solution:
kubectl get pod <pod> -o jsonpath='{.status.initContainerStatuses[*].state}'kubectl logs <pod> -c <init-container-name># Fix the command in the manifest and re-apply with --force
Task 3: Init Container Validating ConfigMap
You are asked to create a Pod where the init container validates that a ConfigMap named app-config exists in the current namespace before the main app container starts.
Requirements: The init container uses kubectl (image bitnami/kubectl) to query the ConfigMap.
Verification:kubectl get pod config-check reaches Running only after the ConfigMap exists.
Solution: