https://drive.google.com/file/d/1lDfGJRDPE999IBfSeOKfFqulswtLsXUA/view?usp=sharing

๐Ÿ” YAML Breakdown

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:

๐Ÿ’ก Why this matters:

This enables decoupled, single-purpose containers that collaborate via shared storage.


๐Ÿ“Œ Real-World Use Cases

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:


๐Ÿงช k3s Lab: Share Data Between Containers

๐Ÿ”ง Step 1: Deploy the Pod

# Apply Pod
kubectl apply -f empty-dir-multiple.yaml

# Verify both containers are running
kubectl get pods empty-multi

๐Ÿ”ง Step 2: Write Data from 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

๐Ÿ”ง Step 3: Read Data from 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!

๐Ÿ”ง Step 4: Simulate Failure (Data Survives Container Restart)

# 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

๐Ÿ”ง Step 5: Clean Up