🚗 What is the Sidecar Pattern? The Sidecar Pattern is a Kubernetes design pattern where two containers run in the same Pod and work to...
🚗 What is the Sidecar Pattern?
The Sidecar Pattern is a Kubernetes design pattern where two containers run in the same Pod and work together.
Think of it like:
🧑 Main app + 🛵 Sidekick (helper container)
They share the same network and storage space because they are in the same pod.
🎯 Use Cases of Sidecar:
-
Logging agents
-
Monitoring agents
-
Proxy services (like Envoy, Istio)
-
Data sync or backup helpers
-
File watchers or preprocessors
📦 Simple Example: Nginx Web Server + Sidecar That Updates Content
We'll create a pod:
-
nginx
: serves web content. -
updater
: a sidecar container that periodically updates the HTML file.
🧾 Full YAML Example: nginx-sidecar-pod.yaml
🔍 Explanation:
Component | Purpose |
---|---|
nginx | Main container to serve the web content. |
updater | Sidecar that writes new content every 5s. |
shared-data | Shared volume so both containers can access /usr/share/nginx/html . |
⚙️ How It Works:
-
updater
writes a file:/shared/index.html
→ shared with nginx at/usr/share/nginx/html
. -
nginx
serves this file on port 80. -
Every 5 seconds, the file is updated with the current time.
🔗 Test It
-
Apply the pod:
-
Port forward to access it locally:
-
Open your browser:
You'll see:
Which updates every 5 seconds. 🎉
COMMENTS