Sub topic 11
Choosing the right pattern
This chapter assumes you can write Pod manifests, know what a Deployment and a Service are, and have used kubectl to apply resources and read logs. It also assumes you have seen init containers and native sidecars in earlier chapters. Here you will decide which one to use for a given job, or whether the job belongs in a separate Pod.
Three questions that decide the pattern
Every helper task fits one of three shapes. Ask these questions in order.
- Must the task finish before the app starts, and does it run only once per Pod start? Use an init container.
- Must the task run for as long as the app runs, sharing the Pod's network or volumes? Use a sidecar. On a current cluster, make it a native sidecar: an init container with
restartPolicy: Always. - Can the task run on its own schedule, scale on its own, or serve more than one Pod? Use a separate Pod, usually behind a Service.
Comparing the patterns
| Question | Init container | Native sidecar | Regular extra container | Separate Pod |
|---|---|---|---|---|
| When does it run? | Before the app, then exits | Starts before the app, keeps running | Starts with the app | Independent of the app |
| Startup order guaranteed? | Yes | Yes | No | No |
| Shares network and volumes? | Yes | Yes | Yes | No |
| Restarted if it exits? | Retried until it succeeds (depending on the Pod's `restartPolicy`) | Yes, always | Per Pod `restartPolicy` | Per its own controller |
| Scales with the app? | Yes, one per replica | Yes, one per replica | Yes, one per replica | No, scaled separately |
| Failure affects the app? | App never starts | Pod is not ready until its startup probe passes | App keeps running | Only through the network |
Matching tasks to patterns
Some typical tasks and where they belong:
- Wait for a database to accept connections. Init container. The check ends and the app then starts.
- Run a schema migration. Usually an init container for small apps. With many replicas, each Pod would run the migration. A Kubernetes Job is safer, because it runs once per release.
- Download or render config into a volume. Init container writing to an
emptyDirvolume. - Ship logs from a shared volume. Native sidecar. It must be running while the app writes.
- Proxy outbound traffic, such as a service mesh proxy. Native sidecar. It must be ready before the app sends its first request, and it should outlive the app during shutdown.
- Refresh a certificate or config file periodically. Native sidecar writing to a shared volume.
- A cache or message queue used by many Pods. Separate Pod. Sharing it through a sidecar would create one copy per replica.
The example below shows a migration that is small enough for an init container, followed by an app that depends on it:
apiVersion: v1
kind: Pod
metadata:
name: app-with-prep
spec:
initContainers:
- name: prepare-data
image: busybox:1.36
command: ['sh', '-c', 'echo "ready" > /work/status.txt']
volumeMounts:
- name: work
mountPath: /work
containers:
- name: app
image: busybox:1.36
command: ['sh', '-c', 'cat /work/status.txt; sleep 3600']
volumeMounts:
- name: work
mountPath: /work
volumes:
- name: work
emptyDir: {}When a separate Pod is the better answer
Containers in one Pod are scheduled together, scaled together, and updated together. That coupling is the point of the Pod, and it is also the cost. Move the helper into its own Pod when any of these is true:
- The helper needs a different replica count than the app. A single cache for ten app replicas is one example.
- The helper has a different release cycle. Changing it should not restart the app.
- The helper needs different resources or node placement, such as a GPU.
- Several different apps use the helper.
Common mistakes
- Using a regular second container for a task that must start first. Containers in
containersstart with no guaranteed order. Use an init container or native sidecar. - Using an init container for a long-running helper. An init container without
restartPolicy: Alwaysmust exit, or the app never starts. - Putting one-time cluster work in every Pod. Migrations in init containers run once per replica, per Pod start. Use a Job if the work must happen once.
- Using a native sidecar on an old cluster. Native sidecars need a Kubernetes version that supports them. Check your cluster's version before relying on them.
Exercise
A team runs a Deployment with 20 replicas. Each Pod needs a TLS certificate file in a shared volume before the app starts, and the file must be refreshed every hour while the app runs. Which pattern or patterns would you use, and why?
Show answer
Use an init container to fetch the first certificate and write it to an emptyDir volume, so the app never starts without it. Use a native sidecar (an init container with restartPolicy: Always) to refresh the file every hour. Alternatively, a single native sidecar can do both jobs if it writes the first certificate before it reports ready, using a startup probe. A separate Pod does not fit, because the file must be in each Pod's own volume.
Exercise
A database schema migration runs in an init container of a 10-replica Deployment. What problem can this cause, and what is a better design?
Show answer
Each of the 10 Pods runs the migration whenever it starts, so migrations can run concurrently or repeatedly and may conflict. Run the migration in a Kubernetes Job once per release, and have the Deployment wait for it, or use an init container only to wait until the schema is ready.