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

๐Ÿ” YAML Breakdown

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:

emptyDir creates a temporary directory on the node that:

๐Ÿ’ก Why mount to /var/www/html?

The lovelearnlinux/webserver:v1 image likely serves files from this directory.

โ†’ emptyDir replaces the default content with an empty directory!


๐Ÿ“Œ When to Use 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:


๐Ÿงช k3s Lab: Test emptyDir Behavior

๐Ÿ”ง Step 1: Deploy the Pod

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

# Verify
kubectl get pods empty-pod

๐Ÿ”ง Step 2: Observe Empty Web Directory

# 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 emptyDir overwrote the imageโ€™s default /var/www/html (which had index.html).

๐Ÿ”ง Step 3: Write Data to 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!"

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

# 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!