Skip to main content
Course/Primitives/Multi-Container Patterns
7/860 min
Primitives

Multi-Container Patterns

Sidecar, init, adapter, and ambassador patterns: when to use multiple containers in one Pod.

intermediate60 minRead β†’ Lab β†’ Quiz β†’ Practice
🧠 Brain Warm-Up
🧠 Brain Warm-Up: Two containers in the same Pod share the same IP address. Container A listens on port 8080. Can Container B also listen on port 8080? What does "shared network namespace" mean for service communication?

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.