Prepare Files with an Init Container · CKAD
Create a file before the application starts and share it through a volume.
Learn with progress and quizzesFirst, understand the concepts.
Initialization order
A regular init container must finish successfully before startup proceeds. If initialization fails, inspect its logs before blaming the application.
Shared emptyDir
Mount one volume at different paths so the app can read files written during initialization. An emptyDir belongs to the Pod lifetime.
Container-specific logs
Use logs -c to select a container. A regular init container and a continuously running sidecar have different lifecycles and roles.
Why it matters
A regular init container can prepare a file before the application starts. Both containers must mount the same volume to share its data.
Work through the reasoning
Verify ordering
Regular init containers must finish successfully before the next stage. If the app is waiting, inspect initContainerStatuses and init logs rather than only app logs.
Connect writer and reader
Mount paths may differ while referencing the same volume name. Check the relative file path and whether ownership and permissions permit the app to read it.
Learn by doing.
Use kubectl, a personal practice Kubernetes cluster, and a POSIX shell. Each lab uses its own namespace. Use example values only in Secrets. Cleanup removes the entire practice namespace.
Have an init container write index.html and let nginx serve it. This lab uses a regular init container and does not require the native sidecar feature.
kubectl config current-context
kubectl create namespace study-ckad-init --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -n study-ckad-init -f - <<'YAML'
apiVersion: v1
kind: Pod
metadata:
name: site
spec:
initContainers:
- name: prepare
image: busybox:1.36
command: ["sh", "-c", "echo init-ready > /work/index.html; echo prepared"]
volumeMounts:
- {name: shared, mountPath: /work}
containers:
- name: web
image: nginx:stable-alpine
volumeMounts:
- {name: shared, mountPath: /usr/share/nginx/html, readOnly: true}
volumes:
- name: shared
emptyDir: {}
YAML
kubectl wait --for=condition=Ready pod/site -n study-ckad-init --timeout=120s
kubectl logs site -n study-ckad-init -c prepare
kubectl exec site -n study-ckad-init -c web -- wget -qO- http://127.0.0.1How to check your result
Expect prepared in the init logs and init-ready in the HTTP response from the app. For a Pod stuck in Init, read prepare logs and Events. Deleting the Pod also removes its emptyDir data.
Clean up practice resources
Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.
kubectl delete namespace study-ckad-initApply what you learned
Initialization succeeded but the app cannot find the file. What should you compare?
Reveal the reasoning after trying
Compare volumeMount names, relative file locations, and the app’s read path. Confirm shared volume identity and permissions. Init success alone does not prove the app reads the right location.
Ready to check your understanding?
What happens while a regular init container has not completed successfully?
- The app always starts first
- The app waits for initialization to complete
- The Service performs initialization
- The volume automatically becomes a PV
Reveal the reasoning after trying
The app waits for initialization to complete
Regular init containers must complete successfully in sequence before app containers start. Distinguish them from continuously running sidecars.
Official documentation · Prepare Files with an Init Container