Sub topic 3
Init containers: setup that runs before your app
This chapter assumes you can write Pod manifests in YAML, use kubectl apply, kubectl get, and kubectl logs, and know what a Deployment and a Service are. You should also know what a container image is.
What an init container is
An init container is a container that runs inside a Pod before the app containers start. It runs to completion and then exits. Kubernetes does not start any app container until every init container has finished successfully.
Init containers are defined in spec.initContainers, next to spec.containers. They use the same container fields: image, command, env, volume mounts, and so on. They differ from app containers in two ways:
- They must exit with code 0 before the app starts.
- They do not support lifecycle probes such as
readinessProbein the classic form, because they are not meant to keep running.
Each init container can use its own image. This is the main benefit. Setup tools such as curl, git, or database clients can live in the init container's image, so your app image stays small.
Example: wait for a dependency
The first pattern is to block startup until another service is reachable. This Pod does not start nginx until the DNS name of the db Service resolves.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
initContainers:
- name: wait-for-db
image: busybox:1.36
command: ['sh', '-c', 'until nslookup db.default.svc.cluster.local; do echo waiting for db; sleep 2; done']
containers:
- name: app
image: nginx:1.27
ports:
- containerPort: 80The shell loop runs nslookup every two seconds. When the lookup succeeds, the loop ends, the command exits with code 0, and Kubernetes starts app.
Apply it and watch the status:
kubectl get pod web
kubectl describe pod web
kubectl logs web -c wait-for-dbWhile the init container runs, kubectl get pod web shows Init:0/1. That means zero of one init containers have completed. Once the db Service exists, the status moves to PodInitializing and then Running. The -c flag on kubectl logs picks the container, which is required here because the init container is not the default.
Example: prepare data for the app
The second pattern is to produce files that the app serves or reads. The init container and the app container share an emptyDir volume. An emptyDir is a scratch directory that is created when the Pod starts and removed when the Pod is deleted.
apiVersion: v1
kind: Pod
metadata:
name: web-with-content
spec:
initContainers:
- name: fetch-content
image: busybox:1.36
command: ['sh', '-c', 'echo "<h1>Prepared by an init container</h1>" > /work/index.html']
volumeMounts:
- name: content
mountPath: /work
containers:
- name: nginx
image: nginx:1.27
volumeMounts:
- name: content
mountPath: /usr/share/nginx/html
volumes:
- name: content
emptyDir: {}The same volume, content, is mounted at different paths in each container. The init container writes index.html to /work. The nginx container sees that file at /usr/share/nginx/html, which is where nginx serves pages from. In a real setup the init container might download the content, render a config template, or check out a Git repository.
How the pieces fit together
With the default restartPolicy: Always on the Pod, a failed init container is retried. Repeated failures lead to a back-off delay and the status Init:CrashLoopBackOff. The next chapter covers ordering and restart rules in detail.
Common mistakes
- Forgetting
-cwith logs.kubectl logs webasks for the app container, which has not started yet. Name the init container. - Writing to the wrong path. If the init container writes to a directory that is not on a shared volume, the app never sees the file. Both containers must mount the same volume.
- An init command that never exits. A loop with no exit condition keeps the Pod in
Init:0/1forever. Make sure the loop ends when the condition is met. - Using
latesttags. Pin image versions, as the examples do, so setup behaves the same on every run.
Exercise
A Pod has one init container and one app container. kubectl get pod shows Init:0/1 for several minutes, and kubectl logs mypod returns an error saying the container is waiting to start. Which command shows the output of the init container named setup, and what does Init:0/1 mean?
Show answer
Run kubectl logs mypod -c setup. Init:0/1 means that 0 of the Pod's 1 init containers have completed, so the app container has not started yet.