DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

Project: Understanding Kubernetes Volume Types

🎯 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
Enter fullscreen mode Exit fullscreen mode

🧩 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: {}
Enter fullscreen mode Exit fullscreen mode

▢️ Apply

kubectl apply -f emptydir.yaml
kubectl exec -it emptydir-demo -- sh
Enter fullscreen mode Exit fullscreen mode

✍️ Create data

echo "HELLO FROM EMPTYDIR" > /data/file.txt
cat /data/file.txt
Enter fullscreen mode Exit fullscreen mode

❌ Kill the pod

kubectl delete pod emptydir-demo
kubectl apply -f emptydir.yaml
kubectl exec -it emptydir-demo -- cat /data/file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ Result

No such file or directory
Enter fullscreen mode Exit fullscreen mode

🧠 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
Enter fullscreen mode Exit fullscreen mode

▢️ Apply

kubectl apply -f hostpath.yaml
kubectl exec -it hostpath-demo -- sh
Enter fullscreen mode Exit fullscreen mode

✍️ Create data

echo "HELLO FROM HOSTPATH" > /data/file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ” Restart pod

kubectl delete pod hostpath-demo
kubectl apply -f hostpath.yaml
kubectl exec -it hostpath-demo -- cat /data/file.txt
Enter fullscreen mode Exit fullscreen mode

βœ… Result

HELLO FROM HOSTPATH
Enter fullscreen mode Exit fullscreen mode

⚠️ 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
Enter fullscreen mode Exit fullscreen mode

πŸ“„ 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
Enter fullscreen mode Exit fullscreen mode

▢️ Apply

kubectl apply -f pvc.yaml
kubectl apply -f pod-pvc.yaml
kubectl exec -it pvc-demo -- sh
Enter fullscreen mode Exit fullscreen mode

✍️ Create data

echo "HELLO FROM PVC" > /data/file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ” Restart pod

kubectl delete pod pvc-demo
kubectl apply -f pod-pvc.yaml
kubectl exec -it pvc-demo -- cat /data/file.txt
Enter fullscreen mode Exit fullscreen mode

βœ… Result

HELLO FROM PVC
Enter fullscreen mode Exit fullscreen mode

🧠 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

Image

Image


🎯 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)