0
0
Kubernetesdevops~10 mins

Init containers in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Init containers
Pod starts
Init Container 1 runs
Init Container 1 completes?
NoWait
Yes
Init Container 2 runs
Init Container 2 completes?
NoWait
Yes
Main containers start
Pod running
Init containers run one by one before main containers start. Each must finish successfully before the next begins.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  initContainers:
  - name: init-setup
    image: busybox
    command: ['sh', '-c', 'echo Init container running; sleep 3']
  containers:
  - name: main-app
    image: nginx
This pod runs an init container that prints a message and waits 3 seconds before starting the main nginx container.
Process Table
StepContainerActionStatusNext Step
1init-setupStart init containerRunningWait for completion
2init-setupComplete command 'echo Init container running; sleep 3'SucceededStart main-app container
3main-appStart main container nginxRunningPod is running
4main-appContainer running normallyRunningPod stays running
💡 Init container completed successfully, main container started, pod is running
Status Tracker
ContainerInitial StateAfter Step 1After Step 2Final State
init-setupNot startedRunningSucceededCompleted
main-appNot startedNot startedRunningRunning
Key Moments - 2 Insights
Why does the main container not start immediately?
The execution_table shows the init container must complete successfully (Step 2) before the main container starts (Step 3). This ensures setup tasks finish first.
What happens if an init container fails?
If an init container fails, the pod will keep retrying that init container and will not start the main containers, as shown by the 'Wait for completion' action in Step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status of 'init-setup' at Step 2?
ASucceeded
BRunning
CFailed
DNot started
💡 Hint
Check the 'Status' column for Step 2 in the execution_table.
At which step does the main container start running?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'Start main container nginx' in the 'Action' column.
If the init container took longer to complete, which step would be delayed?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Main container starts only after init container succeeds (Step 3).
Concept Snapshot
Init containers run before main containers in a pod.
They run sequentially and must complete successfully.
Main containers start only after all init containers finish.
Used for setup tasks like config or waiting for services.
If an init container fails, pod startup pauses until fixed.
Full Transcript
Init containers are special containers in Kubernetes pods that run before the main containers start. They run one after another, and each must finish successfully before the next begins. This ensures setup tasks like configuration or waiting for resources complete first. In the example, the init container runs a simple command and waits 3 seconds. Only after it finishes does the main nginx container start. If an init container fails, the pod will keep retrying it and will not start the main containers until it succeeds.