👤
💾 Module 3 3 LabsEst. 30 min

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

sequenceDiagram participant Admin participant Cluster as Kubernetes API participant Dev as Developer participant Pod Admin->>Cluster: Creates PV (e.g. 100GB AWS EBS) Note over Cluster: PV status: Available Dev->>Cluster: Creates PVC (Requests 10GB) Cluster-->>Cluster: Evaluates available PVs Cluster->>Cluster: Binds PVC to PV Note over Cluster: PV/PVC status: Bound Dev->>Pod: Deploys Pod mounting PVC Pod->>Cluster: Attaches Storage

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

Lab 1: Requesting Storage
Deploy a PersistentVolumeClaim asking for 1Gi of storage.
kubectl apply -f pvc.yaml
Lab 2: Verify Claim
Check if the claim successfully bound to a volume.
kubectl get pvc
Lab 3: Mounting to a Pod
Create a Pod that mounts the PVC so that database files survive restarts.
kubectl apply -f db-pod.yaml

Interview Prep: Storage

Q: Explain Persistent Volumes (PVs) and Persistent Volume Claims (PVCs)

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.

Q: Why use a StatefulSet instead of a Deployment for databases?

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.

Q: What is a StorageClass and what is dynamic provisioning?

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.

Module Knowledge Check

Question 1

Terminal Simulator
root@k8s-master:~#