Your orders service needs mutual TLS with every caller, its logs shipped to a central store and a schema migration before it starts. You could add a TLS library, a log client and a migration step to the code, then repeat that in every other service and language. Or you could put each job in its own container next to the app, in the same Pod, and leave the app's code alone.
That second option is the sidecar pattern. This post covers what a sidecar shares with the app, the ambassador and adapter variants, common sidecars, the native sidecar containers that became stable in Kubernetes 1.33, startup and shutdown order, and when not to use one, with a diagram you can copy.
What the sidecar pattern is
A sidecar is a helper process that runs next to one instance of an application and shares its lifecycle: it is created, scaled and retired with that instance. The Azure Architecture Center compares it to a motorcycle sidecar and also calls it the sidekick pattern. The app keeps the business logic; the sidecar takes a supporting job such as networking, logging or configuration, and can use another language and its own release schedule.
In Kubernetes, the Pod makes this work. Brendan Burns described the sidecar in a 2015 Kubernetes blog post with a web server and a git-sync container that share a filesystem: push to Git, and the site updates. Containers in one Pod share some, but not all, of their environment:
| Resource | Shared by the containers in a Pod? |
|---|---|
| Network namespace | Yes: one IP address and one port space, so containers reach each other on localhost |
| Volumes | Yes, for each container that mounts them, such as an emptyDir |
| Process namespace | Only with shareProcessNamespace: true |
| Image and root filesystem | No: each container has its own |
| CPU and memory | No: requests and limits are set per container (Pod-level resources, beta in 1.34, can add a shared budget) |
| Node and lifetime | Yes: scheduled together and deleted together |
Sidecar, ambassador and adapter
The same post named two more patterns for containers that run side by side. All three are extra containers in the Pod; they differ in what they do for the app.
| Pattern | What it does | Example from the post |
|---|---|---|
| Sidecar | Extends or enhances the main container | git-sync keeps a web server's files up to date |
| Ambassador | Proxies the app's connections to the outside world | A proxy splits reads and writes between a Redis primary and its replicas; the app connects to localhost |
| Adapter | Presents the app's output in a standard form | Monitoring data from many apps, converted into one format |
Today all three are simply called sidecar containers, and real ones mix the roles: a mesh proxy is an ambassador for outbound calls and a classic sidecar for inbound ones.
What teams run as sidecars
| Job | Typical sidecar | How it reaches the app |
|---|---|---|
| Service mesh data plane | Envoy, as istio-proxy in Istio |
iptables rules in the Pod redirect the app's traffic to the proxy |
| Log shipping | Fluent Bit tailing log files | A shared emptyDir volume |
| Content and config sync | git-sync next to a web server | A shared volume |
| Platform APIs | Dapr's daprd |
HTTP or gRPC on localhost, such as port 3500 |
| Telemetry processing | OpenTelemetry Collector as an agent | OTLP to localhost |
A Dapr app calls localhost:3500 instead of linking a client library per language and per store. The service mesh architecture diagram covers the proxy case in depth; for the telemetry row, see how an OpenTelemetry Collector runs as an agent or a gateway.
Sidecars before Kubernetes 1.28
For years a sidecar was just another entry in containers, and Kubernetes treated it exactly like the app. That caused four well-known problems:
- No startup order. Containers started in spec order without waiting for each other, so the app could send requests before the proxy was ready. Istio added the
holdApplicationUntilProxyStartsoption to work around it. - Jobs that never finish. A Job's Pod succeeds only when all its containers have exited, and a proxy never exits on its own. Teams added scripts that told the sidecar to quit.
- Shutdown races. On deletion every container got SIGTERM at once, so a proxy could stop before the app finished its last requests.
- Init containers without the sidecar. Init containers ran before any sidecar, so a migration that had to go through the mesh could not.
Native sidecar containers with restartPolicy: Always
KEP-753 solved this in Kubernetes itself. A native sidecar is an init container with restartPolicy: Always: it starts at its place in the init sequence and keeps running for the life of the Pod. The feature arrived as alpha in Kubernetes 1.28, was enabled by default as beta in 1.29 and became stable in 1.33 in April 2025.
| Regular init container | Native sidecar | App container | |
|---|---|---|---|
| Declared in | initContainers |
initContainers, with restartPolicy: Always |
containers |
| Runs | Once, to completion, in order | From its place in the init sequence until the Pod ends | After every init container has finished or started |
| The next container starts | When it exits successfully | When it is running, or when its startup probe succeeds | Not applicable |
| Probes | None | Startup, readiness and liveness | Startup, readiness and liveness |
| Restarted after it exits | On failure, unless the Pod's restartPolicy is Never |
Always | Following the Pod's restartPolicy |
| On Pod shutdown | Already finished | Stopped after the app containers, in reverse order | Stopped first |
A regular init container listed after a sidecar can already use it, and, as the sidecar containers documentation notes, a sidecar no longer keeps a Job from completing.
Declaring a sidecar in a Pod spec
This is the Deployment behind the diagram below, as you would write it. migrate runs once per Pod (with three replicas the migration tool must take a lock, or you run it as a separate Job); log-shipper is a native sidecar that reads the app's log files from a shared emptyDir volume. Fluent Bit's own configuration is left out:
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders
namespace: shop
spec:
replicas: 3
selector:
matchLabels:
app: orders
template:
metadata:
labels:
app: orders
spec:
initContainers:
- name: migrate # regular init container: runs once, must exit 0
image: registry.example.com/orders:1.4.2
command: ["/app/orders", "migrate"]
- name: log-shipper # native sidecar: starts here, runs until the Pod ends
image: fluent/fluent-bit:3.2
restartPolicy: Always
volumeMounts:
- name: logs
mountPath: /var/log/orders
readOnly: true
containers:
- name: orders
image: registry.example.com/orders:1.4.2
ports:
- containerPort: 8080
volumeMounts:
- name: logs
mountPath: /var/log/orders
volumes:
- name: logs
emptyDir: {}
- Startup order is the order of
initContainers.migratemust exit 0 beforelog-shipperstarts, and both come beforeorders. Put sidecars that other init containers need, such as a proxy, first. - Volumes are shared by mount only. Both containers mount
logs; the sidecar mounts it read-only. - The mesh adds its own sidecar. Istio's admission webhook injects
istio-proxywhen the Pod is created, and since Istio 1.27 it does so as a native sidecar in eligible Pods by default. Without the Istio CNI plugin, it also adds anistio-initcontainer that sets up the traffic redirection.
Startup, readiness and shutdown
Startup follows the spec. The kubelet starts init containers one by one. For a sidecar it moves on once the container is running or, if the sidecar has a startupProbe, once the probe succeeds, so give proxies a startup probe. A sidecar's readiness probe also counts toward the Pod's Ready condition.
Shutdown runs the other way. When a Pod is deleted, the kubelet stops the app containers first and waits for them to exit, then stops the sidecars in reverse spec order, so the proxy leaves last. All of this fits in one terminationGracePeriodSeconds, 30 seconds by default: if the app uses the whole period, the sidecars get SIGTERM and SIGKILL almost together. Non-zero exit codes from sidecars during termination are expected.
Resources add up. Sidecars run alongside the app, so the scheduler adds their requests to the app's. Since 1.32, a sidecar's OOM score adjustment is no higher than the app containers', which keeps a small memory request from making it the OOM killer's first target. The cost multiplies with replicas: a proxy that requests 100m CPU and 128 MiB, in each of 50 replicas, reserves 5 CPUs and 6.25 GiB.
When a sidecar is the wrong tool
| Option | Runs | Fits when | Costs |
|---|---|---|---|
| Sidecar per Pod | Next to every replica | The job needs the app's network, files or identity, set per app | Resources in every replica, one more container to patch |
| Node agent (DaemonSet) | Once per node | Collecting stdout logs or node metrics for all Pods | Less control per app |
| Library in the app | Inside the process | One language, lowest latency | One implementation per language; redeploy to update |
| Separate service | Its own Deployment | The component must scale on its own | A network hop |
| Sidecar-less mesh | A proxy per node, optional proxies per service | mTLS and policy without a proxy in every Pod | A newer way to operate |
For logs, the Kubernetes logging architecture page warns that streaming a log file through a sidecar can double the node storage those logs use, and that a logging agent in every Pod costs significant resources and bypasses kubectl logs. If the app can log to stdout, a node-level agent is usually enough. For meshes, Istio's ambient mode is the sidecar-less option.
The sidecar pattern diagram
Diagram contents
- istiod
- Pod orders-7c9d4
- 1 · native sidecar
- 2 · init container, runs once
- 4 · app container
- 3 · native sidecar
- emptyDir volume: logs
- checkout
- payments
- orders-db (PostgreSQL)
- Loki (log store)
- mTLS, inbound
- plain HTTP in the Pod
- mTLS, outbound
- xDS config + certificates
- schema migration
- writes /var/log/orders
- ships logs
How to read it:
- Numbers are startup order: istio-proxy, then
migrate, which runs once against orders-db, thenlog-shipper, then theordersapp. Shutdown reverses it: the app stops first, the proxy last. - Blue, top: checkout calls orders over mTLS. The proxy passes plain HTTP to the app inside the Pod, and the app's call to payments leaves through the same proxy, again over mTLS.
- Purple, dashed: istiod pushes xDS routing config to the proxy and signs the workload certificates it requests, outside the request path.
- Amber, bottom: the app writes log files to the
emptyDirvolume; the log shipper reads them and ships them to Loki. For clarity, the migrate and log-shipper edges are drawn straight to their targets; in a meshed Pod their outbound traffic also passes through istio-proxy. - Colors mark roles: teal for sidecars, grey for the init container, blue for the app. The Markdown block lists what the containers share.
Drawing sidecar diagrams clearly
- Draw the Pod as a frame and put every container inside it, so readers see what shares one IP address.
- Label each container's role and startup number: app, sidecar or init container.
- Show how each sidecar reaches the app: traffic through the proxy, a volume node for shared files.
- Keep the control plane off the request path, with dashed edges for configuration.
- Draw one Pod in detail and the other replicas as single nodes.
Common mistakes are sidecars drawn as separate services, request arrows that bypass the proxy, a shared volume without its mounts and no sign of which containers are native sidecars. For the cluster around the Pod, see the Kubernetes architecture diagram; for traffic between Pods, the Kubernetes networking diagram.
Build it in Archboard
- Sign in with Google or GitHub and create a diagram. If you have the manifests, the free Kubernetes YAML to diagram tool turns them into a first draft.
- Add a Frame from the Core pack and name it after the Pod.
- Inside it, add four Container nodes from the Docker pack: istio-proxy, migrate, log-shipper and orders, with their images and the startup number in each label. Color the sidecars teal and the init container grey.
- Add a Volume from the Docker pack for the
emptyDir, and connect orders to it and it to log-shipper. - Outside the frame, add a Pod (checkout) and a Service (payments) from the Kubernetes pack, a Container from the C4 pack for istiod, a Postgres node from the Data pack and a Logs node from the DevOps pack for Loki.
- Draw blue mTLS edges through the proxy, a two-way blue plain HTTP edge from the proxy to orders, a grey edge from migrate to orders-db, a dashed purple edge from istiod and amber edges for the log path.
- Add a Markdown block from the Core pack with the startup and shutdown order, and Core Text notes for the summary lines.
The Kubernetes use case page lists more Kubernetes objects. The example uses 12 diagram nodes, within the Free plan's limit of 15 per diagram.
FAQ
What is a sidecar container in Kubernetes?
A helper container in the app's Pod that adds a supporting function, such as a proxy or a log shipper. From Kubernetes 1.29 (stable in 1.33), you declare it as an init container with restartPolicy: Always, so it starts before the app and stops after it.
What is the difference between a sidecar and an init container? A regular init container runs once and must finish before the next one starts. A sidecar sits in the same list but keeps running, supports probes and is restarted whenever it exits.
Do sidecar containers share resources with the app?
They share the Pod's network, so they talk over localhost, and any volume both mount. CPU and memory are set per container, and the scheduler adds the sidecars' requests to the app's.
How do I stop a sidecar from blocking a Job? Make it a native sidecar. The Job's Pod then completes when the main container exits, and the kubelet stops the sidecar. On older clusters, a script has to tell the sidecar to exit.
Does Istio still use sidecars? Yes. Since Istio 1.27 the proxy is injected as a native sidecar in eligible Pods by default. Ambient mode is the alternative: a per-node proxy handles mTLS, and optional waypoint proxies add layer 7 features.