https://drive.google.com/file/d/1FHkmwfdfCk3luxpZiid3_RgPJWvYpqeC/view?usp=sharing
apiVersion: v1
kind: Pod
metadata:
name: empty-pod
spec:
containers:
- name: boxone
image: lovelearnlinux/webserver:v1
volumeMounts:
- mountPath: /var/www/html # โ Where volume is mounted in container
name: demo-volume
volumes:
- name: demo-volume # โ Volume name (matches volumeMounts)
emptyDir: {} # โ Ephemeral storage on node
๐ Key Insight:
emptyDircreates a temporary directory on the node that:
- Is shared across containers in the same Pod
- Survives container restarts
- Is deleted when the Pod is deleted
๐ก Why mount to /var/www/html?
The
lovelearnlinux/webserver:v1image likely serves files from this directory.โ
emptyDirreplaces the default content with an empty directory!
emptyDir| Use Case | Why |
|---|---|
| Scratch space | Temporary files (e.g., sorting, processing) |
| Shared communication | Share data between containers in a Pod |
| Cache directory | Store non-critical cached data |
| Log aggregation | Sidecar container reads logs from app container |
โ ๏ธ When NOT to use emptyDir:
- Stateful data (databases, user uploads) โ use PersistentVolume
- Data that must survive Pod deletion โ use PVC
emptyDir Behavior# Apply Pod
kubectl apply -f empty-dir.yaml
# Verify
kubectl get pods empty-pod
# Check web content (should be empty!)
kubectl exec empty-pod -- ls /var/www/html
# (no output = empty directory)
# Try to access web server
kubectl port-forward empty-pod 8080:80 &
curl <http://localhost:8080>
# โ
Returns empty response (no index.html)
๐ Why?
The
emptyDiroverwrote the imageโs default/var/www/html(which hadindex.html).
emptyDir# Create a file in the volume
kubectl exec empty-pod -- sh -c "echo '<h1>Hello from emptyDir!</h1>' > /var/www/html/index.html"
# Verify
kubectl exec empty-pod -- cat /var/www/html/index.html
# Test via port-forward
curl <http://localhost:8080>
# โ
"Hello from emptyDir!"
# Kill the container (not the Pod!)
kubectl exec empty-pod -- kill 1
# Wait for container to restart
kubectl get pods empty-pod
# Check if data survives
curl <http://localhost:8080>
# โ
Still "Hello from emptyDir!" โ data survived container restart!