Kubernetes, patterns

Your Pod's Plus-One

A sidecar is a helper container riding in the same pod as your app. Here is what that actually means, how it is different from init containers, DaemonSets, and plain microservices, and a 30-second test for picking the right one.

A visual explainer. Kubernetes facts verified against the official docs. Sources at the end.

The short version

01The motorcycle

A motorcycle sidecar is bolted to the bike. It goes where the bike goes, leans when the bike leans, and it never takes a trip on its own. The passenger gets the same view as the rider, the same wind, the same potholes.

A Kubernetes sidecar is the same deal. Your app is the motorcycle: the container with your code, serving traffic. The sidecar is a second container bolted to the same pod: a logging agent, a proxy, a metrics exporter. It sees the same network, reads the same files, and lives and dies on the app's schedule. Different vehicle, same physics.

Step through what that looks like inside a pod:

Step 1 of 4

The takeaway

One pod, two containers, one shared everything. Use the arrows or your keyboard.

02Sidecar vs everything else

People mix up four things that all put "a helper near my app." They differ in where the helper lives, when it runs, and how it talks to your app. Tap through:

pod · 10.0.0.7
appyour code :8080
sidecarhelper, same pod
Lives
Inside your app's pod, as a second container.
Runs
The whole time the app runs.
Talks
Over localhost, plus shared volumes.
Scales
1:1 with the pod, always. Five replicas means five sidecars.
Reach for it
When the helper needs the app's network, files, or lifecycle. Proxies, log shippers, config reloaders.
The whole decision in one table: where it lives, when it runs, how it talks. If those three match your problem, you have your primitive.

03The three classic flavors

Back in 2015, the Kubernetes team wrote the post that named the multi-container patterns. "Sidecar" is the family name, but it has two famous siblings. All three ride in the same pod; they differ in which direction they face:

Sidecar Extends the app.

Their example: pair nginx with a container that syncs a git repo into a shared volume, and you get push-to-deploy. Write the syncer once, reuse it with Apache, Tomcat, anything. The helper makes the main container better at being itself.

Ambassador Proxies localhost out to the world.

Their example: a Redis cluster with one write master and many read replicas. The app just connects to localhost:6379, and the ambassador splits reads and writes to the right servers. The app never learns the cluster exists. In dev, you skip the ambassador and point at a real local Redis. Nothing changes.

Adapter Translates the app's output into a standard shape.

Their example: ten apps export metrics ten different ways (JMX, StatsD, bespoke). One monitoring system wants one format. An adapter per pod reshapes each app's native output into the uniform model. Heterogeneous in, homogeneous out.

In practice, people call all three "sidecars." The purists will correct you, and now you know enough to nod along.

04Sidecars in the wild

The pattern is everywhere once you look. A few you have probably met:

Service mesh Istio and Linkerd inject an Envoy proxy next to every app container.

Your app talks plain HTTP to localhost; the proxy adds mutual TLS, retries, and telemetry. This is the ambassador flavor at planetary scale, and it is why native sidecars mattered so much: the old model could not guarantee the proxy started first or stopped last.

Logging Fluent Bit tails your app's log files from a shared volume and ships them off.

Your app just writes files like it is 2005. The sidecar handles parsing, buffering, and delivery to wherever logs go to retire.

Config and secrets Reloader sidecars watch mounted files and nudge the app when they change.

ConfigMap updated? The sidecar sees it and signals the app, no restart. Same pod, same files, zero downtime.

Managed databases Google's Cloud SQL Auth Proxy runs as a sidecar your app reaches at localhost.

Your code opens a plain database connection; the proxy handles IAM authentication and encryption to the real Cloud SQL instance. The app stays dumb about credentials, which is exactly how you want it.

05The 30-second test

Five situations, five answers. Tap each card and check yourself:

Init container. It runs first, polls the database, and blocks the app until Postgres answers. Then it exits. Setup with an exit is the whole job description of an init container.

Sidecar. An Envoy proxy rides in the pod and terminates TLS on localhost. Your app keeps speaking plain HTTP and never knows the difference. This is the Istio and Linkerd move.

DaemonSet. The job is per machine, not per app. One pod per node, running whether your app is there or not.

Separate Deployment and Service. It is shared by many and scaled by its own load. A sidecar would pointlessly copy it five times, once per app.

Sidecar. A tiny reloader watches the mounted files and nudges the app over localhost. Same pod, same files, zero downtime.

If you got all five, the mental model has landed: match where the helper lives and when it runs to the problem, and the primitive picks itself.

06What it looks like in YAML

The classic shape. Two containers, one pod, one shared volume. The app writes logs like it is 2005; the sidecar ships them:

spec:
  containers:
  - name: app
    image: myapp:2.0
    volumeMounts:
    - name: logs
      mountPath: /var/log/app
  - name: log-shipper
    image: fluent/fluent-bit:3.2
    volumeMounts:
    - name: logs
      mountPath: /var/log/app
      readOnly: true
  volumes:
  - name: logs
    emptyDir: {}

And the modern shape. Since Kubernetes 1.33, the officially blessed way to declare a sidecar is an init container with restartPolicy: Always. It starts before your app containers, shuts down after them, and unlike the old way, it no longer wedges Jobs open forever when the main work finishes:

spec:
  initContainers:
  - name: log-shipper
    image: fluent/fluent-bit:3.2
    restartPolicy: Always   # this line makes it a native sidecar
    volumeMounts:
    - name: logs
      mountPath: /var/log/app
  containers:
  - name: app
    image: myapp:2.0
    volumeMounts:
    - name: logs
      mountPath: /var/log/app

07Keep exploring

08Sources

TAGS: KUBERNETES, PATTERNS