Sub topic 9
Init containers
Init containers run before the main containers of a Pod start. This chapter assumes you can read a Pod manifest, know the Pod phases, and understand that containers in a Pod can share an emptyDir volume.
What init containers do
An init container is a container listed under spec.initContainers. The kubelet (the node agent) runs init containers one at a time, in the order you list them. Each one must exit with code 0 before the next starts. Only after all of them succeed does the kubelet start the containers in spec.containers.
Typical uses:
- Prepare files in a shared volume, such as generating configuration or content.
- Wait for a dependency, such as a database, to accept connections.
- Run setup tools that you do not want in the main image.
Init containers can use a different image from the main containers. This keeps tools like curl or git out of your application image.
A worked example
This Pod has one init container that writes a file into an emptyDir volume. The nginx container mounts the same volume and serves the file.
apiVersion: v1
kind: Pod
metadata:
name: web-init
labels:
app: web
spec:
initContainers:
- name: prepare-content
image: busybox:1.36
command:
- sh
- -c
- echo '<h1>Hello from init</h1>' > /work/index.html
volumeMounts:
- name: html
mountPath: /work
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 2
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
volumes:
- name: html
emptyDir: {}The volume html is mounted at /work in the init container and at /usr/share/nginx/html in nginx. The file written by the first container is what the second one serves.
Apply it and watch the status:
kubectl apply -f pod.yaml
kubectl get pod web-init -wWhile the init container runs, the STATUS column shows Init:0/1. After it finishes, the Pod moves to PodInitializing and then Running.
Behavior to know
- Failures and restarts. If an init container fails and
restartPolicyisAlwaysorOnFailure, the kubelet retries it with backoff. WithNever, the whole Pod is marked as failed. - No probes. Init containers do not support readiness, liveness, or startup probes in the way app containers do. They simply run to completion.
- Pod phase. The Pod stays
Pendingwhile init containers run. It is not Ready until they finish and the app containers pass their readiness probes. - Resources. The Pod's effective request is the larger of the biggest single init container request and the sum of the app container requests. Init containers run one at a time, so they do not add up.
- Idempotence. Init containers may run again if the Pod's sandbox is recreated, so write them to be safe to repeat.
Debugging a stuck init container
If a Pod sits in Init:Error or Init:CrashLoopBackOff, read the init container's logs by name:
kubectl describe pod web-init
kubectl logs web-init -c prepare-contentA common error is a "wait for service" loop that never ends because the dependency name is wrong. The Pod then stays in Init:0/1 indefinitely, and the logs output shows the loop.
Init containers versus sidecars
Use an init container for work that must finish before the app starts. Use a regular second container (a sidecar) for work that runs alongside the app for the Pod's whole life, such as the log reader from the previous chapter.
Exercise
A Pod has two init containers, A and then B, and one app container. Init container A fails repeatedly with exit code 1, and restartPolicy is Always. Does B run, does the app container run, and what do you see in kubectl get pod?
Show answer
Neither B nor the app container runs, because each init container must exit 0 before the next step begins. The kubelet keeps retrying A with backoff. kubectl get pod shows a status such as Init:Error and then Init:CrashLoopBackOff. Use kubectl logs <pod> -c A to see why it fails.
Exercise
An init container requests 500m CPU. The only app container requests 200m CPU. What CPU request does the scheduler use for the Pod?
Show answer
500m. The effective request is the larger of the highest init container request (500m) and the sum of the app container requests (200m), because init containers run one at a time, before the app containers.