CKAD · 18 November 2026 · 10 min read

Kubernetes sidecar containers: native sidecars, use cases and YAML

A Kubernetes sidecar is a helper container in your app's Pod. How native sidecars start before the app and stop after it, a real git-sync example in YAML, the Docker Compose equivalent, and how to debug them.

A Kubernetes sidecar is a helper container that runs alongside your application container in the same Pod. It shares the Pod's network and can share its volumes, so it can ship the app's logs, proxy its traffic, refresh its credentials or sync files for it, without any change to the application itself. Since Kubernetes 1.33, sidecars are a stable, built-in feature: a native sidecar is an init container with restartPolicy: Always, which starts before the app and stops after it.

TL;DR

  • A sidecar is a helper container in the same Pod as your app: same network (localhost), shared volumes.
  • Native sidecar = an entry in spec.initContainers with restartPolicy: Always (on by default since Kubernetes 1.29, stable since 1.33).
  • It starts before the app, runs for the life of the Pod, stops after the app, and does not block a Job from completing.
  • "Starts before" is not "ready before": only a startupProbe on the sidecar makes the app wait for it.
  • Forget restartPolicy: Always and the Pod hangs at Init:0/1 forever.
  • Docker has no Pods: Compose can run the same two containers on one host, without the sidecar lifecycle.
  • Exam: CKAD Application Design and Build (20%); a broken sidecar is a CKA Troubleshooting task.

This is part 3 of the Kubernetes Pod Patterns series: what sidecars are for, how native sidecars behave, a real one (git-sync keeping nginx's content current), the Docker Compose equivalent, debugging, and when not to use one.

What a sidecar is for

The idea is to keep one job per container. The application does its work; the sidecar adds something around it. The Kubernetes docs list logging, monitoring, security and data synchronization as typical jobs. Common examples:

  • Log shipping: the app writes log files to a shared volume and the sidecar sends them to a central log store.
  • Proxies and service meshes: a proxy such as Envoy handles TLS, retries and metrics for the app's traffic. Service meshes inject this sidecar for you, usually through a mutating admission webhook.
  • Configuration and secrets: a sidecar fetches or refreshes certificates, tokens or config files and writes them where the app can read them. Since Kubernetes 1.37, Pod certificates (stable) let a signer issue and refresh a Pod's certificates without a sidecar.
  • File sync: a sidecar pulls content from Git or object storage into a volume that a web server serves. This is the example below.
  • Adapters and ambassadors: a sidecar reshapes the app's output for another system, or proxies the app's connections out. They have their own parts in this series.

A Pod's containers share one network namespace, so the sidecar reaches the app on localhost; files are shared through a volume, usually an emptyDir, mounted into both.

Native sidecars: restartPolicy: Always

Before native support, a sidecar was simply a second entry in spec.containers. That still works, but Kubernetes could not tell the helper from the app, which caused three problems: the sidecar could start after the app, it could be stopped before the app during shutdown, and in a Job a sidecar that never exits kept the Pod running after the real work was done. Service meshes felt this most: traffic was lost if the proxy started late or stopped early. The proposal to fix it was opened in 2018 and took seven years to reach stable.

A native sidecar is listed under spec.initContainers with restartPolicy: Always on the container. The feature was alpha in 1.28, on by default from 1.29 and stable in 1.33.

A native sidecar's life in the Pod, drawn as a timeline. First an ordinary init container, setup, runs and exits 0, marked with a tick. Then the git-sync sidecar starts; once its startupProbe passes, it keeps running and is restarted if it exits. The nginx app starts after the sidecar has started and runs beside it. When the Pod is deleted, the app stops first, marked 1, and the sidecar stops after it, marked 2.

A native sidecar:

  • Starts before the app containers, in its place in the init container order. Kubernetes does not wait for it to exit; once it has started (and passed its startup probe, if it has one) the next init container or the app begins. A readiness probe does not delay the app; use a startupProbe on the sidecar if the app needs it ready first.
  • Keeps running for the life of the Pod and is restarted if it exits, whatever the Pod's own restartPolicy is.
  • Stops after the app containers when the Pod shuts down. The kubelet sends sidecars SIGTERM only after the last app container has fully stopped, then stops them in reverse order, so a log shipper can send the final lines.
  • Does not block Job completion: once the main containers finish, the Pod completes and the sidecar is stopped.
  • Can have probes (liveness, readiness and startup) and lifecycle hooks, which ordinary init containers cannot. Its readiness counts towards the Pod's.

A non-zero exit code from a sidecar at Pod termination is normal and can be ignored.

A sidecar in a real cluster: git-sync and nginx

git-sync, a Kubernetes project, "pulls a git repository into a local directory, waits for a while, then repeats", and its README calls it "a perfect 'sidecar' container". It publishes each sync atomically, by switching a symlink to a new checkout, so the web server never sees a half-updated site. It also replaces the old gitRepo volume type, which is permanently disabled from Kubernetes 1.36.

A git-sync sidecar in a real cluster. Readers send HTTP to Service web, which sends port 80 traffic to the nginx container in two Pods, web-1 on worker node 1 and web-2 on worker node 2, both replicas of one Deployment. In each Pod, a git-sync native sidecar pulls from a Git repository outside the cluster and writes into an emptyDir volume named content, and nginx serves the files from that volume.

Each replica has its own copy of the content and its own git-sync. Here is the Deployment:

YAML
apiVersion: apps/v1kind: Deploymentmetadata:  name: webspec:  replicas: 2  selector:    matchLabels:      app: web  template:    metadata:      labels:        app: web    spec:      initContainers:      - name: git-sync        image: registry.k8s.io/git-sync/git-sync:v4.8.0        restartPolicy: Always            # this makes it a native sidecar        args:        - --repo=https://github.com/example-org/website        - --root=/usr/share/nginx        - --link=html        - --period=30s        volumeMounts:        - name: content          mountPath: /usr/share/nginx      containers:      - name: nginx        image: nginx:1.27        ports:        - containerPort: 80        volumeMounts:        - name: content          mountPath: /usr/share/nginx      volumes:      - name: content        emptyDir: {}

Replace the repository with your own. Both containers mount the volume at the same path, so the html link git-sync keeps up to date is exactly nginx's default web root. Apply it, expose it and check it:

Shell
kubectl apply -f web.yamlkubectl expose deployment web --port=80kubectl get pods -l app=webkubectl logs deployment/web -c git-sync

READY shows 2/2: native sidecars count towards the ready total. Push a change to the repository and, within one sync period, both Pods serve it, with no new image and no rollout. Without -c, kubectl logs picks the container named in the kubectl.kubernetes.io/default-container annotation, or else the first app container, so always name the container you want in a multi-container Pod.

One catch: nginx can start before the first sync has finished and briefly serve an empty site. If that matters, run git-sync once with --one-time as an ordinary init container in front of the sidecar, or give the sidecar a startupProbe.

The older pattern: a second app container

You will still see sidecars written as a second entry in spec.containers, and older exam-style tasks may ask for exactly that. The YAML is the same except that the sidecar moves into containers and loses restartPolicy:

Shell
      containers:      - name: nginx        image: nginx:1.27        # ...      - name: git-sync        image: registry.k8s.io/git-sync/git-sync:v4.8.0        args: ['--repo=https://github.com/example-org/website', '--root=/usr/share/nginx', '--link=html']        volumeMounts:        - name: content          mountPath: /usr/share/nginx

If a task says "add a sidecar container" and shows or checks spec.containers, follow the task. Otherwise prefer the native form: it gets start order, shutdown order and Jobs right.

Sidecar vs init container

Native sidecars are built on init containers, so the two are easy to confuse. The difference is one field and what it implies:

  • Init container (no restartPolicy, or Never or OnFailure since 1.35): runs once, must exit with status 0, and the next container waits for it to finish. Use it for setup work: wait for a database, download a file, run a migration.
  • Sidecar (restartPolicy: Always, the only value that makes a sidecar): starts, keeps running, is restarted if it stops, and stops last. Use it for help the app needs all the time.

The classic mistake is writing a long-running helper as an init container and forgetting restartPolicy: Always. The helper never exits, so the Pod sits at Init:0/1 forever and the app never starts. Init containers in Kubernetes covers init containers and the Init statuses in detail.

Sidecars in Docker and Docker Compose

Docker has no Pod, so it has no built-in sidecar. You build the same pair by hand. In Docker Compose that means two services, git-sync and nginx, sharing a named volume; with plain docker run, the classic example is a log shipper that joins the app's network namespace and shares its log volume:

Shell
docker volume create app-logsdocker run -d --name app -v app-logs:/var/log/app busybox:1.36 \  sh -c 'while true; do date >> /var/log/app/app.log; sleep 5; done'docker run -d --name log-shipper --network container:app -v app-logs:/var/log/app busybox:1.36 \  tail -F /var/log/app/app.log

--network container:app puts the sidecar in the app's network namespace, so localhost means the same thing in both, just as in a Pod. In Docker Compose the equivalent is network_mode: "service:app" on the sidecar service plus a shared named volume.

git-sync and nginx, in a Pod and in Docker Compose. A Git repository at the top feeds both sides. Left, in blue, in a Kubernetes Pod: git-sync writes to a shared volume that nginx serves; git-sync starts before nginx and stops after it, the two are one unit scheduled onto one node, and more replicas means raising the Deployment's count. Right, in gray, in Docker Compose: the same git-sync, shared volume and nginx, as two services and a named volume on one host, with start order and restarts yours to set. A caption says push to Git and the site updates with no new image.

What Docker does not give you is the Pod: nothing ties the two containers together as one unit, schedules and replicates the pair across machines, or gives the helper a built-in start-first, stop-last lifecycle; ordering is up to you. That is what Kubernetes adds with native sidecars, and it is why the pattern is far more common on Kubernetes than on plain Docker.

Debugging a sidecar

  • See every container's state: kubectl describe pod <pod> lists sidecars under Init Containers, with their state, restart count and last exit code.
  • Read the right logs: kubectl logs <pod> -c <sidecar>, and add --previous if it has restarted.
  • Get inside it: kubectl exec -it <pod> -c <sidecar> -- sh, for example to check that the shared file is where you expect.
  • Pod stuck in Init: check that the sidecar has restartPolicy: Always, and that any init container listed before it actually finishes. Init:N/M counts sidecars too; a sidecar that has started counts as done.
  • App starts before the sidecar is ready: a readiness probe won't hold it back; add a startupProbe to the sidecar.
  • Sidecar sees no data: the volume name or mountPath usually differs between the two containers.
  • Cluster rejects the field: restartPolicy: Always on an init container needs Kubernetes 1.29 or newer (1.28 with the feature gate). Check with kubectl version.

When not to use a sidecar

  • A library or shared service would do. The Kubernetes blog calls a sidecar "generally not the preferred approach unless the use case justifies it", because it adds complexity, resource use and latency.
  • Logs from one file. Have the app write to /dev/stdout and let the node's agent collect it. A logging-agent sidecar can use significant resources, and its own logs don't show in kubectl logs.
  • Certificates. On Kubernetes 1.37, Pod certificates can replace a certificate-refresher sidecar.
  • A mesh proxy in every Pod. Istio's ambient mode moves the proxy to the node; part 6 compares the two.

For the exam

The CKAD (v1.37) names the sidecar first in "Understand multi-container Pod design patterns (e.g. sidecar, init and others)", under Application Design and Build (20%). A typical task: add a sidecar that shares a volume with the app and follows its log, then show its output with kubectl logs -c. On the CKA, a Pod stuck in Init because a sidecar lacks restartPolicy: Always is a fair Troubleshooting task.

Common questions

Is a sidecar the same as a multi-container Pod?

A sidecar is one kind of multi-container Pod. Others are the ambassador (a proxy for outgoing connections) and the adapter (which reshapes the app's output). All three put helper containers next to the app in one Pod; part 1 compares them.

Does a sidecar have its own IP address?

No. Every container in a Pod shares the Pod's IP address and ports, so two containers in the same Pod cannot both listen on the same port.

Can I add a sidecar to a running Pod?

No. A Pod's container list cannot be changed in place. Add the sidecar to the Deployment's Pod template (kubectl edit deployment) and let the rollout replace the Pods, or for a bare Pod, edit the manifest and run kubectl replace --force -f pod.yaml. Changing an existing sidecar's image later restarts just that container, not the Pod.

The sidecar containers page on kubernetes.io has the full reference.

Keep going

The Kubernetes Pod Patterns series:

Sidecars make sense once you have watched one start before the app and outlive it. The Pods and multi-container patterns module of the Gazeek CKAD course gives you a real cluster for each lab, including init containers and sidecars, with checks that tell you when you have it right. For more exam-style tasks, try the CKAD practice questions with answers, and keep field names fresh with the free Gazeek Memory Gym.

More from the blog