Deploy a Multi-Container Application¶
Goal¶
In this tutorial you will deploy an application that runs multiple containers in a single pod.
By the end you will understand:
- how containers share a pod
- how containers communicate via localhost
- how the sidecar pattern works
Problem¶
Some applications require multiple cooperating containers.
Examples include:
- application + logging agent
- application + metrics exporter
- application + proxy
- application + file processor
Instead of running these separately, Kubernetes allows multiple containers in one pod.
All containers inside a pod:
- share the same network
- share volumes
- start and stop together
Architecture¶
Traffic flow in this example:
The application writes logs to a shared directory, and a second container reads those logs.This pattern is called a sidecar container.
Prerequisites¶
Ensure the following are installed and configured:
- Kubernetes cluster
- kubectl configured
- a working namespace
Verify cluster access:
Expected output:Step 1 : Create a Multi-Container Pod¶
Create the file:
multi-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: multi-container-demo
spec:
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: app
image: busybox
command: ["/bin/sh", "-c"]
args:
- while true; do
echo "Hello from app container" >> /data/app.log;
sleep 5;
done
volumeMounts:
- name: shared-data
mountPath: /data
- name: logger
image: busybox
command: ["/bin/sh", "-c"]
args:
- tail -f /data/app.log
volumeMounts:
- name: shared-data
mountPath: /data
This pod contains two containers:
| Container | Purpose |
|---|---|
| app | writes log entries |
| logger | reads and prints logs |
The shared volume allows both containers to access the same data.
Step 2 : Deploy the Pod¶
Apply the configuration.
Verify the pod is running. Expected output: Notice 2/2 containers are running.Step 3 : Inspect the Pod¶
View details about the pod.
Look for the containers section.You should see:
Step 4 : View Logs from the Logger Container¶
Check logs from the logger container.
Expected output: The logger container is reading the log file written by the app container.Step 5 : Access the App Container¶
Open a shell in the app container.
View the log file. You should see entries being written continuously.What Just Happened¶
The two containers share:
| Resource | Description |
|---|---|
| Network | both containers use localhost |
| Storage | shared emptyDir volume |
| Lifecycle | containers start and stop together |
The app container writes logs, and the logger container reads them.
This architecture is called a sidecar pattern.
Common Sidecar Use Cases¶
Sidecars are widely used in production clusters.
Examples include:
| Use Case | Example |
|---|---|
| Logging | Fluentd sidecar |
| Metrics | Prometheus exporters |
| Proxies | Envoy / Istio |
| Security | service mesh proxies |
Troubleshooting¶
Pod Not Starting
Check pod status.
Inspect events.
Container Logs Not Appearing Ensure the logger container is running.
Then check container logs.
Shared Volume Not Working
Confirm both containers mount the same volume.
Look for:
Clean Up¶
Delete the pod when finished.
Key Takeaways¶
You learned how to:
- deploy a pod with multiple containers
- share data between containers using volumes
- inspect individual container logs
- implement the sidecar pattern
Multi-container pods are a foundational pattern used in many Kubernetes architectures.