π― 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.β


Top comments (0)