Init containers and sidecar containers
Containers that run before your app, native sidecars that start first, run alongside, stop last and never block a Job, and how both change a Pod's requests.
Init containers and sidecar containers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Init containers run one at a time, in order, before any app container starts. Each must exit 0; a failure restarts it (or fails the Pod under restartPolicy Never).
- A native sidecar is an init container with restartPolicy: Always. It starts in order, keeps running, gets restarted if it dies, and can have probes.
- Sidecars start before the app containers and stop after them, in reverse order, so a proxy or log shipper sees the app's whole life.
- Sidecars do not keep a Job's Pod alive: the Pod completes when its regular containers finish, which ended years of workarounds. Native sidecars are GA since 1.33.
- The scheduler counts a Pod's CPU and memory as the larger of its biggest init step (plus sidecars started before it) and all sidecars plus all app containers.
Order matters, and Pods had none
A Pod’s regular containers start at roughly the same time, in no guaranteed order, and the Pod is done only when all of them stop. That is fine for one app. It breaks down quickly for helpers:
- A database migration must finish before the app starts.
- A service-mesh proxy must be up before the app makes its first network call, and must stay up until the app has drained its last request.
- A log shipper must keep reading after the app writes its last line, yet must not stop a batch Job from finishing.
For years people bolted on workarounds: shell loops waiting for a port, preStop sleeps, and Job containers that killed the sidecar through a shared file. Kubernetes now has two built-in answers: init containers for things that run first and finish, and native sidecar containers for things that run first and keep running.
Init containers
Init containers are listed under spec.initContainers and run one at a time, in order, before any regular container starts. Each must exit 0 before the next begins. If one fails, the kubelet restarts it with back-off, unless the Pod’s restartPolicy is Never, in which case the Pod fails. kubectl get pods shows progress as Init:0/2, Init:1/2, or Init:Error and Init:CrashLoopBackOff when something goes wrong.
Typical jobs: run migrations, wait for a dependency, render a config file into a shared emptyDir, fetch a secret, or set kernel parameters with a privileged image the app should never have. Because they can use their own image, the app image stays small and free of tools it doesn’t need. Init containers cannot have liveness, readiness or startup probes; they either finish or they don’t. If the Pod’s sandbox is recreated, for example after a node reboot, the init containers run again, so make them safe to repeat.
Native sidecars: restartable init containers
A sidecar is an init container with restartPolicy: Always. That one field changes its behaviour completely:
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
initContainers:
- name: migrate # regular init container: runs to completion first
image: ghcr.io/example/api:3.0.0
command: ["/app/migrate", "--up"]
resources: {requests: {cpu: "2"}}
- name: proxy # native sidecar: starts, then keeps running
image: envoyproxy/envoy:v1.36.0
restartPolicy: Always
resources: {requests: {cpu: 500m}}
containers:
- name: api
image: ghcr.io/example/api:3.0.0
resources: {requests: {cpu: "1"}}
- Startup. It starts in its place in the init sequence, but the kubelet moves on as soon as it has started, and passed its
startupProbeif it has one, instead of waiting for it to exit. Later init containers and all app containers can rely on it. - Running. It keeps running for the life of the Pod, is restarted if it exits, and may have liveness, readiness and startup probes like any app container.
- Shutdown. When the Pod terminates, the app containers are stopped first, then the sidecars in reverse order of declaration. A proxy is still there while the app drains.
- Completion. Sidecars do not count towards completion. When a Job Pod’s regular containers finish, the kubelet ends the sidecars and the Pod succeeds.
Picture a theatre stage crew. The crew arrives before the actors, stays through the show, and leaves only after the actors have taken their bows; nobody waits for the crew to “finish” before calling the show over.
Sidecars arrived as alpha in 1.28, became beta and on by default in 1.29, and went GA in 1.33. In the v1.37.3 source the SidecarContainers feature gate has already been removed, as GA gates are.
How sidecars change a Pod’s requests
The scheduler needs one number for a Pod’s CPU and memory. Init containers run one at a time and sidecars keep running, so the rule is: the Pod’s effective request is the larger of
- the biggest init step, where each init container’s request is added to the sidecars started before it, and
- the sum of all sidecars plus all regular containers.
The lab shows the ordering matters. On a fake node with 4 CPUs, the Pod above, with migrate listed before proxy, was charged 2 CPU: max(2, 0.5 + 1). The same Pod with proxy listed first was charged more, because the proxy is already running while the migration runs: max(2 + 0.5, 0.5 + 1) = 2.5. With both on the node, the second didn’t fit:
Warning FailedScheduling default-scheduler 0/1 nodes are available: 1 Insufficient cpu. ...
After the first Pod was deleted, kubectl describe node showed the sidecar-first Pod scheduled with 2500m (62%) of CPU requested. Put long-lived sidecars after heavy init steps when they don’t need to run during them.
When to use which
| Need | Use |
|---|---|
| Do a step once, before the app, then stop | Init container |
| A helper that must be up before the app and outlive it (proxy, log shipper, credential refresher) | Native sidecar |
| A second process that is genuinely part of the app, equal partner | Regular container |
Service meshes and log agents are the main users of native sidecars; Istio and others can inject proxies this way. If you still see sidecars as regular containers with preStop sleeps, that is pre-1.29 thinking.
About the video
The embed is Todd Neal and Sergey Kanzhelev’s KubeCon talk “Sidecar Containers Are Built-in to Kubernetes: What, How, and Why Now?” (CNCF, November 2023), from the start to 25:02, just before the Q&A. They worked on the sidecar KEP. Its chapters cover the sidecar pattern, the historical problems, reliability and how the design evolved, the implementation, termination ordering, best practices and caveats, and future directions. It was recorded when the feature was alpha in 1.28, so anything said about feature gates or alpha status is out of date: sidecars are GA since 1.33. It does not cover init containers in general, which the lesson does. English was judged from the English title, abstract and caption track; there is no audio-track metadata. Kubesimplify’s video in deeper demonstrates the feature in a Deployment and in a Job.
What was run: both Pods and the scheduler’s resource accounting (2 CPU against 2500m, and the Insufficient cpu event) came from a local v1.37.3 control plane whose node runs no containers, on 2026-11-28. Startup, restart and shutdown ordering need a kubelet, so they are written from the sidecar and init container docs and KEP-753.
Where this fits
Controllers find their Pods, and Services find their backends, by labels. Next: labels, selectors, annotations and namespaces.
Check yourself
Why put a database migration in an init container instead of at the top of the app's entrypoint script?
It runs to completion before any app container starts, can use a different image with tools the app image lacks, and its failure shows clearly as Init:Error or Init:CrashLoopBackOff. The app container stays simple and starts only once the schema is ready.
A Job's Pod has a log-shipping sidecar written as a second regular container. The main container finishes but the Job never completes. Why, and how do native sidecars fix it?
A Pod only completes when all its regular containers exit, and the log shipper never does. Declared as an init container with restartPolicy: Always, it becomes a sidecar; the kubelet ends it once the regular containers have finished, so the Pod and the Job complete.
Your app crashes at startup because the Envoy sidecar isn't ready yet. What should you change?
Make Envoy a native sidecar listed before anything that needs it, and give it a startupProbe. The kubelet starts init containers in order and doesn't move past a sidecar until it has started, and the probe passed if one is defined, so the app starts only after the proxy is up.
An init container requests 2 CPU, a sidecar listed before it requests 500m, and the app requests 1 CPU. What request does the scheduler use for the Pod?
2.5 CPU. While the init container runs, the earlier sidecar is already running, so that step needs 2 + 0.5. Afterwards the sidecar plus the app need 1.5. The scheduler takes the larger, 2.5. Listed after the init container, the same sidecar gives max(2, 1.5) = 2.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.