Multi-Container Pods (Sidecars)
Explore patterns for running multiple co-located containers within a single Pod, like sidecar containers.
Multi-Container Pods (Sidecars) is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond a Single Container
So far, we've thought of Pods as running a single container. But what if your application needs a helper process?
Kubernetes allows a Pod to run multiple containers. These containers are always co-located and share resources.
Why Multi-Container Pods?
Running multiple containers in one Pod is ideal for applications that are tightly coupled and need to share:
- Network namespace (same IP address, can communicate via
localhost) - Storage volumes
- Lifecycle (they start, stop, and restart together)
This allows for a single unit of management for related processes.
The Sidecar Pattern Explained
A common design is the Sidecar Pattern. Here, a "sidecar" container runs alongside your main application container.
It extends or enhances the main application's functionality without modifying the main app itself. Think of it like a helpful companion!
Sidecar Use Cases
Sidecars are incredibly versatile. Common uses include:
- Log Collection: A sidecar collects logs from the main app and sends them to a central logging service.
- Proxy: A sidecar provides network proxying, like for security (mTLS) or traffic management.
- File Synchronization: A sidecar keeps a shared volume updated with configuration or data.
Pod with Two Containers (YAML)
Let's look at how you define multiple containers in a Pod's YAML manifest. Each container is an item in the containers array.
Notice the nginx-proxy and main-app containers defined under spec.containers.
apiVersion: v1
kind: Pod
metadata:
name: multi-container-pod
spec:
containers:
- name: nginx-proxy
image: nginx:latest
ports:
- containerPort: 80
- name: main-app
image: busybox:latest
command: ["sh", "-c", "echo 'Hello from main app!' && sleep 3600"]Shared Storage: emptyDir
Containers within the same Pod can share data using Kubernetes Volumes. A simple way to do this is with an emptyDir volume.
An emptyDir volume is created when a Pod is assigned to a node and exists as long as that Pod is running on that node. It's temporary storage.
Example: Log Collector Sidecar
Imagine a main application that generates logs to a file. A sidecar can then read this log file and push it to a logging service.
This decouples logging logic from your main application, keeping it clean and focused.
Log Collector YAML (Part 1)
Here's the YAML for our log collector example. We define an emptyDir volume named log-volume. Both containers will mount this volume.
The main-app container writes a log message to /var/log/app.log inside the shared volume.
apiVersion: v1
kind: Pod
metadata:
name: log-collector-pod
spec:
volumes:
- name: log-volume
emptyDir: {}
containers:
- name: main-app
image: busybox:latest
command: ["sh", "-c", "while true; do echo $(date) 'Main app log entry' >> /var/log/app.log; sleep 1; done"]
volumeMounts:
- name: log-volume
mountPath: /var/logLog Collector YAML (Part 2)
Now, the log-sidecar container mounts the same log-volume. It then uses tail -f to continuously read the log file written by the main app.
In a real scenario, this sidecar would process and send these logs to a logging system like Elasticsearch or Splunk.
apiVersion: v1
kind: Pod
metadata:
name: log-collector-pod # Same Pod as before
spec:
volumes:
- name: log-volume
emptyDir: {}
containers:
- name: main-app
image: busybox:latest
command: ["sh", "-c", "while true; do echo $(date) 'Main app log entry' >> /var/log/app.log; sleep 1; done"]
volumeMounts:
- name: log-volume
mountPath: /var/log
- name: log-sidecar
image: busybox:latest
command: ["sh", "-c", "tail -f /var/log/app.log"]
volumeMounts:
- name: log-volume
mountPath: /var/logQuick Check: Sidecar Benefits
Multi-container Pods, especially with the sidecar pattern, offer several advantages for managing complex applications.
Which of the following are key benefits of using a sidecar container?
Recap: Multi-Container Pods
Great job! You've learned about multi-container Pods and the powerful sidecar pattern.
- Pods can host multiple containers that share network and storage.
- The sidecar pattern helps extend or enhance main app functionality.
- Common uses include log collection, proxies, and data sync.
emptyDirvolumes provide temporary shared storage within a Pod.
This pattern is crucial for building robust, modular applications in Kubernetes!
Frequently asked questions
Is the “Multi-Container Pods (Sidecars)” lesson free?
Yes — the full text of “Multi-Container Pods (Sidecars)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Multi-Container Pods (Sidecars)”?
Explore patterns for running multiple co-located containers within a single Pod, like sidecar containers. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Multi-Container Pods (Sidecars)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pods: The Smallest Unit
- Pod Lifecycle and States
- Multi-Container Pods (Sidecars)
- Init Containers and Startup Ordering