🎯 Goal
By the end, you will viscerally know:
- why some data disappears
- why some data survives pod restarts
- when to use each volume
- why databases require PVCs
🧠 The 3 volume types we will test
| Volume | Data survives pod restart? | Use case |
|---|---|---|
emptyDir |
❌ NO | temp files, cache |
hostPath |
⚠️ SOMETIMES | node-specific (dangerous) |
PVC |
✅ YES | databases, user data |
📁 Project structure
k8s-volumes-lab/
├── emptydir.yaml
├── hostpath.yaml
├── pvc.yaml
└── pod-pvc.yaml
🧩 PART 1 — emptyDir (DATA DIES WITH POD)
📄 emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
name: emptydir-demo
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
▶️ Apply
kubectl apply -f emptydir.yaml
kubectl exec -it emptydir-demo -- sh
✍️ Create data
echo "HELLO FROM EMPTYDIR" > /data/file.txt
cat /data/file.txt
❌ Kill the pod
kubectl delete pod emptydir-demo
kubectl apply -f emptydir.yaml
kubectl exec -it emptydir-demo -- cat /data/file.txt
💥 Result
No such file or directory
🧠 Lesson
emptyDir lives only as long as the pod lives
🧩 PART 2 — hostPath (DATA TIED TO NODE)
📄 hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
name: hostpath-demo
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: host
mountPath: /data
volumes:
- name: host
hostPath:
path: /tmp/hostpath-demo
type: DirectoryOrCreate
▶️ Apply
kubectl apply -f hostpath.yaml
kubectl exec -it hostpath-demo -- sh
✍️ Create data
echo "HELLO FROM HOSTPATH" > /data/file.txt
🔁 Restart pod
kubectl delete pod hostpath-demo
kubectl apply -f hostpath.yaml
kubectl exec -it hostpath-demo -- cat /data/file.txt
✅ Result
HELLO FROM HOSTPATH
⚠️ Why this is dangerous
- Data exists only on one node
- Pod moves → data gone
- Breaks HA
- NEVER use for DBs in prod
🧠 Lesson
hostPath is node-coupled, not cluster-safe
🧩 PART 3 — PVC (PRODUCTION-SAFE STORAGE)
📄 pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: demo-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
📄 pod-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pvc-demo
spec:
containers:
- name: app
image: busybox
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: demo-pvc
▶️ Apply
kubectl apply -f pvc.yaml
kubectl apply -f pod-pvc.yaml
kubectl exec -it pvc-demo -- sh
✍️ Create data
echo "HELLO FROM PVC" > /data/file.txt
🔁 Restart pod
kubectl delete pod pvc-demo
kubectl apply -f pod-pvc.yaml
kubectl exec -it pvc-demo -- cat /data/file.txt
✅ Result
HELLO FROM PVC
🧠 Lesson
PVC survives pod death — this is how databases live
🧠 FINAL COMPARISON (MEMORIZE THIS)
| Volume | Pod dies | Data |
|---|---|---|
| emptyDir | ❌ | ❌ |
| hostPath | ❌ | ⚠️ (node-only) |
| PVC | ❌ | ✅ |
🔥 WHY DATABASES REQUIRE PVCs
Databases:
- must survive restarts
- must survive rescheduling
- must survive upgrades
PVC gives:
- persistent disk
- node-independent storage
- safe recovery
Without PVC:
Kubernetes is doing exactly what it was designed to do — destroy pods.
🖼️ Visual intuition
🎯 What a 6-year DevOps engineer says in interviews
“Pods are ephemeral, so persistent data must live on PVCs. emptyDir is for temporary data, hostPath is node-coupled and unsafe in production, and PVC abstracts durable storage from pod lifecycle.”


SOCIAL SHARE CARD GENERATOR