Sub topic 5
Practical init container patterns
This chapter assumes you can write a Pod manifest, know that containers in a Pod can mount the same volume, and have seen initContainers in a spec. It shows the patterns that init containers are used for in real clusters. It also covers the mistakes that come with each one.
What init containers are good at
An init container runs to completion before any app container starts. If it fails, the app never starts. That makes it a good place for work that must finish first and that you don't want inside the app image. Most real uses fall into four groups.
| Pattern | What the init container does | Why not do it in the app |
|---|---|---|
| Wait for a dependency | Polls DNS or a port until a Service is reachable | Keeps retry logic out of the app image |
| Prepare data | Downloads, renders, or copies files into a shared volume | Lets the app image stay small and generic |
| Fix volume ownership | Runs `chown` or `chmod` as root on a mounted volume | Lets the app run as a non-root user |
| Run one-time setup | Runs schema migrations or registers the Pod somewhere | Keeps extra tools and credentials out of the app image |
Pattern 1: wait for a dependency
This init container loops until the DNS name of the db Service resolves. The web Pod stays in Init:0/1 until then.
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: 80A DNS check only proves that the Service exists. It does not prove that the database accepts connections. If you need that, test the port instead, for example with nc -z db 5432. Your app should still retry its own connections, because the database can go away later, after the init container has finished.
Pattern 2: prepare data for the app
The init container below writes a file into an emptyDir volume. The nginx container mounts the same volume at its web root and serves the file.
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 two containers mount the volume at different paths. The volume is the same, so the files are the same. In practice the echo is replaced by wget, git clone, or a templating tool. The point is that the tool lives only in the init container image, so the app image does not need it.
Pattern 3: fix permissions and run one-time setup
When a volume is owned by root and the app runs as a non-root user, the app cannot write to it. A short init container running as root can run chown on the mount. Run it with the same volumeMounts entry the app uses, then let the app start as its normal user. The same approach suits migrations and registration steps. Give the init container the credentials and tools it needs, and give the app container nothing extra.
Inspect an init container
Use these commands to see where a Pod is stuck and what the init container printed.
kubectl get pod web
kubectl describe pod web
kubectl logs web -c wait-for-dbkubectl get shows Init:0/1 while the first of one init container is still running. kubectl describe lists events and each init container's state. kubectl logs needs -c with the init container's name, because the default is the app container.
Common mistakes
- Using a long-running process in an init container. The Pod never becomes ready. If you need a process that keeps running, use a sidecar instead.
- Waiting forever with no signal. A loop with no log line makes a stuck Pod hard to diagnose. Print a message on every attempt, as the example above does.
- Putting heavy tools in the app image when an init container could carry them.
- Assuming the dependency stays up. The init container only checks at startup.
Exercise
A Pod has an init container that runs until nslookup db; do sleep 2; done. The Pod has shown Init:0/1 for ten minutes. Name two kubectl commands you would run first, and what each tells you.
Show answer
Run kubectl describe pod <name> to see the init container's state and recent events, such as image pull errors or restarts. Run kubectl logs <name> -c <init-container-name> to see the "waiting" output and nslookup errors. Together they show whether the init container is looping because the db Service does not exist, is in the wrong namespace, or has no DNS record.