https://drive.google.com/file/d/1KQZ7ejpCI7BREx_-qFwpC_C-U61NZU3-/view?usp=sharing

πŸ” YAML Breakdown

apiVersion: v1
kind: Pod
metadata:
  name: nnappone
  namespace: learning
  labels:
    app: nnappone
spec:
  volumes:
    - name: "web-data"
      hostPath:
        path: "/tmp/webserver"   # ← Directory on the NODE
  containers:
    - name: networknuts-app
      image: lovelearnlinux/webserver:v1
      volumeMounts:
        - mountPath: "/var/www/html"  # ← Mounted in container
          name: "web-data"
      resources:
        requests:
          cpu: "400m"
          memory: "128Mi"
        limits:
          cpu: "500m"
          memory: "256Mi"
      ports:
        - containerPort: 80

πŸ”‘ Key Insight:

hostPath mounts a directory from the node’s filesystem into the Pod.

πŸ’‘ Why /tmp/webserver?


πŸ“Œ When to Use hostPath

Use Case Why
Single-node clusters (k3s on laptop) Simple persistent storage
DaemonSets (logging agents) Access node logs (/var/log)
System-level Pods Access node filesystem (e.g., CNI plugins)

⚠️ When NOT to use hostPath:


πŸ§ͺ k3s Lab: Test hostPath Persistence

πŸ”§ Step 1: Create Namespace & Deploy Pod

# Create namespace
kubectl create namespace learning

# Apply Pod
kubectl apply -f pod-with-volume.yml

# Verify
kubectl get pods -n learning -o wide
# Note the NODE where Pod is running (e.g., k3s-node1)

πŸ”§ Step 2: Write Data to the Volume

# Create a file in the Pod
kubectl exec nnappone -n learning -- sh -c "echo '<h1>Persistent via hostPath!</h1>' > /var/www/html/index.html"

# Verify
kubectl exec nnappone -n learning -- cat /var/www/html/index.html

πŸ”§ Step 3: Delete Pod (Data Survives on Node)

# Delete Pod
kubectl delete pod nnappone -n learning

# Check node filesystem (SSH into the node from Step 1)
ssh <node-ip>
cat /tmp/webserver/index.html
# βœ… "Persistent via hostPath!" β†’ data survived Pod deletion!

πŸ”§ Step 4: Recreate Pod (Data Still There)

# Redeploy Pod (may land on same node in single-node k3s)
kubectl apply -f pod-with-volume.yml

# Verify data is still served
kubectl port-forward nnappone -n learning 8080:80 &
curl <http://localhost:8080>
# βœ… "Persistent via hostPath!"

⚠️ Critical Test: In a multi-node k3s cluster, if the new Pod lands on a different node, the data won’t be there!