Volumes & Storage

💤0
Lv 10 XP
← 📦 Containers & Kubernetes · Kubernetes

Volumes & Storage

Advanced ⭐ 120 XP ⏱ 18 min #kubernetes#storage#volumes

Persist data beyond a Pod's life with PersistentVolumes, claims, and storage classes.

📖Theory

A container’s filesystem is ephemeral — it’s lost when the Pod restarts. To persist data, Kubernetes uses volumes. The durable abstraction has three parts:

  • PersistentVolume (PV) — a piece of real storage (a cloud disk, NFS share)
  • PersistentVolumeClaim (PVC) — a Pod’s request for storage of a size/mode
  • StorageClass — defines how to dynamically provision PVs on demand

A Pod mounts a PVC; the StorageClass provisions a matching PV automatically. Access modes matter: ReadWriteOnce (one node), ReadOnlyMany, ReadWriteMany. Stateful apps pair this with StatefulSets for stable per-Pod storage.

🌍Real-World Example
apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: data }
spec:
  accessModes: ["ReadWriteOnce"]
  storageClassName: standard
  resources: { requests: { storage: 10Gi } }
---
# In a Pod spec:
volumes:
  - name: data
    persistentVolumeClaim: { claimName: data }
containers:
  - name: db
    volumeMounts: [{ name: data, mountPath: /var/lib/data }]
✍️Hands-On Exercise
  1. Explain the roles of PV, PVC, and StorageClass.
  2. Write a PVC requesting 10Gi with ReadWriteOnce.
  3. Mount that PVC into a container at a path.
  4. Why do databases use StatefulSets with persistent volumes?
🧾Cheat Sheet
ObjectRole
PersistentVolumeActual storage resource
PersistentVolumeClaimA Pod’s storage request
StorageClassDynamic provisioning
ReadWriteOnceOne node mounts read/write
ReadWriteManyMany nodes read/write
emptyDirEphemeral scratch (Pod life)
💬Common Interview Questions
What's the relationship between a PV, PVC, and StorageClass?

A PVC is a Pod’s request for storage; a StorageClass dynamically provisions a PersistentVolume to satisfy it. The Pod mounts the PVC, decoupled from the underlying storage.

Why are PVCs used instead of mounting storage directly?

They decouple the Pod from the storage implementation — the Pod requests size and access mode, and the cluster provisions matching storage, keeping manifests portable.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type