Persistent Storage
Learn how to maintain data across pod lifecycles using Volumes, PVs, and PVCs.
Theory: Data Persistence
When a Pod crashes, any files written inside the container are lost. To solve this, K8s provides Volumes.
Key Concepts
- emptyDir: A temporary volume tied to a Pod's lifecycle. Dies when Pod dies.
- Persistent Volume (PV): Cluster-wide storage provisioned by an administrator.
- Persistent Volume Claim (PVC): A request for storage by a user/developer ("I need 5GB of fast storage").
- StorageClass (SC): Enables dynamic provisioning. If a PVC requests an SC, K8s automatically creates the PV for you!
Deep Dive: The PV/PVC Lifecycle
Storage in Kubernetes decouples the underlying infrastructure from the applications. An administrator provisions Persistent Volumes (PVs), and developers claim them using Persistent Volume Claims (PVCs).
3. StatefulSets vs Deployments
While Deployments are great for stateless web servers, databases need stable storage. StatefulSets provide pods with persistent network identifiers (e.g., db-0, db-1) and guarantee that each pod keeps its specific PVC even if the pod restarts.
Hands-on Labs
Interview Prep: Storage
A PV is a piece of storage in the cluster provisioned by an administrator. A PVC is a request for storage by a user/developer. Claims are bound to Volumes, establishing a 1-to-1 relationship, decoupling the developer from the underlying storage technology.
StatefulSets provide persistent, stable network identifiers and ordered deployment/scaling. Most importantly, each replica in a StatefulSet gets its own dedicated PVC template. If a Pod reschedules, it automatically re-attaches to its exact original storage volume, which is critical for databases.
A StorageClass allows administrators to define different tiers of storage (e.g. SSD, HDD). Instead of administrators creating PVs manually, dynamic provisioning automatically creates a new PV on the fly (via an external provisioner like AWS EBS) the moment a developer creates a PVC referencing that StorageClass.