https://drive.google.com/file/d/1KQZ7ejpCI7BREx_-qFwpC_C-U61NZU3-/view?usp=sharing
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:
hostPathmounts a directory from the nodeβs filesystem into the Pod.
- Data survives Pod deletion
- Data is tied to a specific node
π‘ Why /tmp/webserver?
- Easy to access on the node
- Survives reboots (unlike
/tmpon some systems)
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:
- Multi-node clusters β Pod may reschedule to another node (data lost!)
- Production stateful apps β Use PersistentVolumes instead
- Security-sensitive workloads β
hostPathcan expose node filesystem
hostPath Persistence# 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)
# 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
# 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!
# 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!