Difficulty: Intermediate
What is the sidecar pattern in Kubernetes, how are sidecars implemented, and what problems do they solve?
A sidecar is a helper container that runs in the same Pod as the main application container to add a supporting capability, such as proxying traffic, shipping logs or syncing configuration, without changing the application code. Because containers in a Pod share the network namespace and can share volumes, the sidecar can talk to the app over localhost and read the same files.
localhost.emptyDir mounted in both lets the app write logs that a shipper reads.They do not share a filesystem root or (by default) a process namespace; you can enable shareProcessNamespace: true if a sidecar needs to see the app's processes.
localhost:5432 while the proxy handles IAM auth and TLS.For years, sidecars were just extra entries in containers, which caused ordering problems: the app could start before the proxy was ready, and Jobs never completed because the sidecar kept running after the main container exited. Kubernetes fixed this with native sidecars: an entry in initContainers with restartPolicy: Always. This feature has been on by default since 1.29 and is stable since 1.33. Native sidecars:
Related patterns include the ambassador (a sidecar that proxies outbound calls, like the Cloud SQL proxy) and the adapter (a sidecar that converts output into a standard format, such as a metrics exporter).
| Regular container sidecar | Native sidecar (initContainers + restartPolicy: Always) | |
|---|---|---|
| Startup order | Starts alongside the app, no guarantee | Starts and can be ready before the app |
| Shutdown order | Can stop before the app finishes | Stops after the main containers |
| Jobs | Keeps the Job from completing | Job completes when the main container exits |
| Availability | All versions | On by default since 1.29, stable in 1.33 |
apiVersion: v1
kind: Pod
metadata:
name: legacy-billing
spec:
initContainers:
- name: log-shipper
image: fluent/fluent-bit:3.2
restartPolicy: Always # makes it a native sidecar
volumeMounts:
- name: logs
mountPath: /var/log/app
readOnly: true
containers:
- name: app
image: ghcr.io/acme/billing:4.0
volumeMounts:
- name: logs
mountPath: /var/log/app # app writes files here
volumes:
- name: logs
emptyDir: {}
The shipper starts first, shares the logs volume with the app, and is stopped only after the app exits.
Sidecar Pattern, Native Sidecar Containers, Shared Network Namespace, Service Mesh Proxy, Log Shipping