While most pods run a single container, the sidecar pattern — running a helper container alongside your main application within the same pod — is a powerful, common Kubernetes design pattern. This guide covers implementing it.
What the Sidecar Pattern Provides
Containers within the same pod share network namespace and can share storage volumes — a sidecar container adds functionality (logging, proxying, monitoring) to your main application container without modifying the application itself, a form of composition at the pod level.
Common Sidecar Use Cases
- Log shipping — a sidecar tails application logs and forwards them to a centralized logging system
- Service mesh proxies — a sidecar handles mTLS, traffic management, and observability transparently to the application
- Configuration/secret syncing — a sidecar keeps a local file updated from an external source
Basic Multi-Container Pod Definition
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: app
image: myapp:latest
volumeMounts:
- name: logs
mountPath: /var/log/myapp
- name: log-shipper
image: fluent-bit:latest
volumeMounts:
- name: logs
mountPath: /var/log/myapp
readOnly: true
volumes:
- name: logs
emptyDir: {}
Both containers share the logs volume — the application writes logs, and the sidecar reads and ships them elsewhere, without the application needing any awareness of the shipping mechanism.
Sidecar Containers vs Init Containers
Distinct from init containers (which run to completion before the main container starts), sidecars run alongside the main container for the pod's entire lifetime — different use cases: init containers for one-time setup, sidecars for ongoing auxiliary functionality.
Using the Native Sidecar Container Feature (Newer Kubernetes)
initContainers:
- name: sidecar
image: fluent-bit:latest
restartPolicy: Always
Recent Kubernetes versions support native "sidecar" semantics within initContainers (using restartPolicy: Always) — ensures proper startup/shutdown ordering relative to the main container, an improvement over the traditional plain multi-container approach for certain use cases.
Service Mesh as a Sidecar Pattern Example
Popular service mesh implementations (Istio, Linkerd) inject a proxy sidecar into every pod, transparently handling mTLS, traffic routing, and observability — a large-scale, sophisticated application of the sidecar pattern, worth understanding as the pattern's most prominent real-world use case.
Resource Considerations for Sidecars
See Kubernetes Resource Requests and Limits Explained — sidecars consume real cluster resources too; factor their resource requirements into your overall pod resource planning, not just the main application container's needs.
Debugging Multi-Container Pods
kubectl logs POD_NAME -c container-name
kubectl exec -it POD_NAME -c container-name -- /bin/sh
Specify the container name explicitly (-c) when working with multi-container pods, since commands need to know which specific container within the pod you're targeting.
When Not to Use the Sidecar Pattern
If functionality can be cleanly separated into its own independent pod/service instead (not requiring the tight coupling of shared network/volume namespace), a separate deployment is often simpler to manage and scale independently — reserve sidecars for genuinely pod-local, tightly-coupled auxiliary functionality.
Common Errors
Sidecar container causes the entire pod to be marked unhealthy — verify each container's own probes are configured appropriately; a failing sidecar probe can affect overall pod readiness depending on your configuration, worth understanding for your specific sidecar's role.
Continue Reading
- Kubernetes Pods, Deployments & Services Explained
- How to Manage Persistent Storage in Kubernetes (PersistentVolumes & PVCs)
- How to Set Up Liveness and Readiness Probes in Kubernetes
Browse more articles in Kubernetes & Container Orchestration.