Init containers
Prepare data before the application starts
A bookstore reader needs a small inventory file before it can display a count. An ordinary init container performs preparation and exits successfully before the next initialization step begins. Unlike a long-running helper, it is not intended to keep serving after the application starts. Use initialization for a finite task, not as a substitute for application health checks.
Write and read a shared file
The example uses a Pod named catalog-init. A temporary volume named work is mounted into both containers; the preparation container can write, while the reader gets a read-only mount. Both scripts use the same path inside their own container. Expected application output is books=3. The image deliberately cannot be pulled: this is a full static manifest for review, not a deployable lab. A reviewed Python image, isolated namespace and approved cluster would be needed for execution.
apiVersion: v1kind: Podmetadata: name: catalog-init namespace: pattern-studyspec: restartPolicy: Never activeDeadlineSeconds: 120 terminationGracePeriodSeconds: 15 automountServiceAccountToken: false securityContext: runAsNonRoot: true runAsUser: 10001 runAsGroup: 10001 fsGroup: 10001 seccompProfile: type: RuntimeDefault volumes: - name: work emptyDir: sizeLimit: 8Mi initContainers: - name: prepare image: example.invalid/bytemind/python:offline command: - python3 - -c args: - 'from pathlib import Path
Path(''/work/catalog.txt'').write_text(''books=3\n'', encoding=''utf-8'')' securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL resources: requests: cpu: 200m memory: 64Mi limits: cpu: 400m memory: 128Mi volumeMounts: - name: work mountPath: /work readOnly: false containers: - name: reader image: example.invalid/bytemind/python:offline command: - python3 - -c args: - 'from pathlib import Path
print(Path(''/work/catalog.txt'').read_text(encoding=''utf-8'').strip())' securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL resources: requests: cpu: 100m memory: 32Mi limits: cpu: 200m memory: 64Mi volumeMounts: - name: work mountPath: /work readOnly: trueFollow the startup sequence
First Kubernetes establishes the Pod environment and mounts the volumes. Then prepare writes the file and exits with code zero. Only then may reader start. With more ordinary init containers, each waits for its predecessor to complete successfully. A failure prevents subsequent steps from starting. Under the traditional Pod-level restart policies used here, Never makes a failing init cause Pod failure; OnFailure and Always allow retries (Always treats ordinary init failure like OnFailure). An init may run again in some restart or replacement scenarios, so writing a fixed file is safer than appending another record every time. Ordinary init containers do not use startup, readiness or liveness probes, or lifecycle hooks.
Estimate resources during preparation
For this ordinary-init-only example, the CPU request is the larger of the preparation request (200m) and the application request (100m), hence 200m. The memory request is 64Mi rather than 32Mi. With multiple sequential ordinary init containers, take the largest request separately for each resource, then compare with the sum for concurrently running application containers. This example excludes Pod overhead and Pod-level resource overrides. Requests are scheduling inputs, not measured usage. An init request can make a Pod unschedulable even when its application would fit.
Diagnose a blocked initialization
A synthetic inspection snapshot shows prepare has exited with code 1, and reader has not started. Inspect the failed initialization step before blaming reader. In an explicitly approved lab with a dedicated kubeconfig and context, these read-only commands would identify its state and logs. They were not run here. On a replacement Pod the emptyDir data is not retained; container restarts within the same Pod can retain it. This is not a storage backup.
kubectl --context bytemind-lab -n pattern-study describe pod catalog-initkubectl --context bytemind-lab -n pattern-study logs catalog-init -c preparePractice
Use init-cases.json to decide whether the application can start under each recorded state. Explain why repeated preparation should be safe and why a missing file is not fixed by adding an application readiness probe. Next, study helpers that continue running during the application lifetime. Further reading: https://v1-34.docs.kubernetes.io/docs/concepts/workloads/pods/init-containers/ and https://kubernetes.io/docs/reference/kubernetes-api/core/pod-v1/.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Init containers — knowledge check
Apply the chapter concepts using synthetic offline scenarios.
What must happen before the reader starts in catalog-init?
prepare fails with exit code 1 under Pod restartPolicy Never. What follows?
Two ordinary init containers request 300m and 150m; app containers request 100m and 120m. What is the effective CPU request with no overhead?
A preparation script can rerun. Which output approach is safer?
A replacement Pod starts. What happens to the previous Pod emptyDir contents?