https://drive.google.com/file/d/1lDfGJRDPE999IBfSeOKfFqulswtLsXUA/view?usp=sharing
apiVersion: v1
kind: Pod
metadata:
name: empty-multi
spec:
containers:
- name: boxone
image: nginx
volumeMounts:
- mountPath: /demo # โ Mounts volume at /demo
name: demo-volume
- name: boxtwo
image: lovelearnlinux/webserver:v1
volumeMounts:
- mountPath: /var/www/html # โ Mounts SAME volume at /var/www/html
name: demo-volume
volumes:
- name: demo-volume
emptyDir: {} # โ Shared ephemeral storage
๐ Key Insight:
Both containers share the same directory on the node:
boxonesees it as/demoboxtwosees it as/var/www/htmlโ Any file written by one container is immediately visible to the other!
๐ก Why this matters:
This enables decoupled, single-purpose containers that collaborate via shared storage.
| Pattern | How It Works |
|---|---|
| Log Shipping | App writes logs to /var/log โ Fluentd sidecar reads and ships them |
| Config Reloading | Config controller writes to /config โ App reloads config on change |
| Metrics Exporter | App writes metrics to /metrics โ Prometheus exporter scrapes them |
| File Processing | Downloader writes to /data โ Processor reads and transforms |
๐ฏ Your Example:
boxtwo(webserver) serves files from/var/www/htmlboxone(nginx) could write files to/demoโ instantly served byboxtwo!
# Apply Pod
kubectl apply -f empty-dir-multiple.yaml
# Verify both containers are running
kubectl get pods empty-multi
boxone (nginx)# Create a file in boxone's mount point
kubectl exec empty-multi -c boxone -- sh -c "echo '<h1>Hello from boxone!</h1>' > /demo/index.html"
# Verify file exists in boxone
kubectl exec empty-multi -c boxone -- cat /demo/index.html
boxtwo (webserver)# Check if file is visible in boxtwo
kubectl exec empty-multi -c boxtwo -- cat /var/www/html/index.html
# โ
Same content: "Hello from boxone!"
# Test via web server
kubectl port-forward empty-multi 8080:80 &
curl <http://localhost:8080>
# โ
"Hello from boxone!" โ served by boxtwo!
# Kill boxtwo container
kubectl exec empty-multi -c boxtwo -- kill 1
# Wait for restart
kubectl get pods empty-multi
# Verify data still there
curl <http://localhost:8080>
# โ
Still works! โ `emptyDir` survives container restarts