Multi-Container Patterns
Sidecar, init, adapter, and ambassador patterns: when to use multiple containers in one Pod.
Why Multiple Containers?
Containers in the same Pod share:
- Network namespace β same IP, communicate via localhost
- Volumes β can mount the same emptyDir or PVC
- Lifecycle β start and stop together
Use multiple containers to add cross-cutting concerns without modifying the main app image.
The Four Patterns
1. Sidecar
Augments the main container. Runs alongside it throughout the Pod lifetime.
Pod
βββ App Container β writes logs to /var/log/app/
βββ Sidecar Container β reads /var/log/app/, ships to Elasticsearch
(shared emptyDir volume)Examples: log shipping, metrics collection, TLS proxy, config reloader.
2. Init Container
Runs to completion before the main container starts. Use for prerequisites.
Init Container 1: wait for DB to be ready β exit 0 Init Container 2: run migrations β exit 0 Main Container: start application β (runs forever)
3. Ambassador
Proxies outbound connections. Main container talks to localhost; ambassador forwards to the real backend.
Pod βββ App Container β connects to localhost:6379 βββ Ambassador β forwards :6379 to Redis Cluster
Benefit: swap the backend without changing the app.
4. Adapter
Normalizes output format from the main container for external consumers (e.g. monitoring).
Pod βββ App Container β exposes metrics at /metrics in proprietary format βββ Adapter β reads /metrics, converts to Prometheus format, exposes :9090
Key Constraint
Containers cannot listen on the same port within a Pod (shared network namespace). Assign different ports to each container.