Theory
Persistent Volumes (PV) & Claims (PVC) in Kubernetes
📋 Overview
In the world of container orchestration, pods are ephemeral—they can be destroyed and recreated at any time, losing their local filesystem. Persistent Volumes (PV) and Persistent Volume Claims (PVC) decouple storage from the pod lifecycle. This allows stateful applications (databases, file stores) to maintain data consistency even as pods migrate across different nodes in a Kubernetes cluster.
🏗️ Core Principles & Characteristics
- Persistent Volume (PV): A low-level storage resource (EBS, NFS, Local Disk) provisioned by a cluster administrator. It exists independently of any pod.
- Persistent Volume Claim (PVC): A request for storage by a developer. It specifies size (e.g., 10Gi) and access modes.
- StorageClass: Enables Dynamic Provisioning. Instead of admins manually creating PVs, a StorageClass automatically creates a PV when a PVC is requested.
- Binding: The process where Kubernetes matches a PVC to an available PV that meets the requirements.
⚖️ Trade-offs: Pros & Cons
- Pros: Decouples infrastructure (storage) from application logic; supports multi-cloud portability; allows for automated backups and snapshotting.
- Cons: Complexity in managing "ReadWriteMany" (shared storage) across multiple nodes; potential performance overhead compared to raw local disks.
🌍 Real-World Implementation
- StatefulSets: Used for databases (MySQL, MongoDB). Each pod in a StatefulSet gets its own unique PVC, ensuring that if
db-0restarts, it always reconnects to its specific disk. - EBS/Azure Disk (ReadWriteOnce): Most common for databases. The disk can only be mounted to one node at a time.
- EFS/NFS (ReadWriteMany): Used for shared assets like user-uploaded images that need to be accessed by multiple web-server pods simultaneously.
💡 Interview "Gotchas" & Tips
- The Reclaim Policy: What happens to the data when the PVC is deleted?
Retain: PV stays, but data must be manually cleaned.Delete: The underlying cloud storage (EBS) is deleted.
- Binding Failures: If a PVC is "Pending," it usually means no PV matches its size/access mode, or the cloud provider ran out of quota.
- Node Affinity: Some PVs (like Local Disks) are tied to a specific node. If the pod tries to schedule on another node, it will fail.
📐 Suggested Architecture Primitives
- CSI (Container Storage Interface): The standard plugin architecture that allows Kubernetes to talk to any storage vendor.
- Access Modes:
RWO(ReadWriteOnce): Single node mount.RWX(ReadWriteMany): Multi-node mount.ROX(ReadOnlyMany): Multi-node read-only.
- Volume Snapshots: For consistent point-in-time backups of the database state.
Canvas