LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Kubernetes

Introduction to Persistent Volume Management

By design, Kubernetes Pods are temporary. If a Pod crashes, gets rescheduled to another server, or scales down, everything saved inside its local container memory is instantly destroyed. While this is great for stateless applications like web servers, it is a disaster for databases, user uploads, or any application where your data shouldn't be lost.

Introduction to Persistent Volume Management in Kubernetes

By design, Kubernetes Pods are temporary. If a Pod crashes, gets rescheduled to another server, or scales down, everything saved inside its local container memory is instantly destroyed. While this is great for stateless applications like web servers, it is a disaster for databases, user uploads, or any application where your data shouldn't be lost.

To solve this, Kubernetes uses a storage architecture that separates the physical storage from the Pods that use it. In this tutorial, we will learn how to configure Persistent Volumes (PVs), Persistent Volume Claims (PVCs), and dynamic StorageClasses so your application data survives pod restarts safely.

What you will learn
  • The difference between Persistent Volumes (PVs) and Persistent Volume Claims (PVCs).
  • How a StorageClass automates the creation of physical disks dynamically.
  • How to write YAML to request storage and attach it to a Pod.
  • The lifecycle and reclaim policies of Kubernetes storage.

1. The Core Concepts of Kubernetes Storage

Think of Kubernetes storage like renting an apartment. The Persistent Volume (PV) is the actual physical apartment building. The Persistent Volume Claim (PVC) is your rental application saying "I need a 2-bedroom apartment." The StorageClass is the property manager who automatically builds new apartments when people request them.

By keeping the "request" (PVC) separate from the "actual disk" (PV), developers don't need to know if the underlying storage is an AWS EBS volume, an Azure Disk, or a local hard drive on a bare-metal server. They just ask for storage, and Kubernetes handles the rest.

Concept What it represents Who manages it?
StorageClass (SC) The blueprint or "property manager". It defines what kind of storage to provision (e.g., fast SSD or cheap HDD) and automates the creation of PVs. Cluster Administrator
Persistent Volume (PV) The actual piece of storage in the cluster (e.g., a 10GB block of a hard drive). It exists independently of any Pod. Admin or StorageClass (Auto)
Persistent Volume Claim (PVC) A request for storage by a developer. E.g., "I need 5GB of ReadWrite storage." Kubernetes matches this claim to an available PV. Application Developer

2. Practical Implementation & Code

Let's look at how to request storage dynamically. We will create a PVC, and then we will create an NGINX Pod that mounts this storage. Because we are using a dynamic StorageClass (which is standard on local setups like Minikube, Docker Desktop, or cloud providers), we do not need to manually create the PV—the StorageClass will do it for us.

Creating the Storage Request and the Pod

Example 1: pvc-and-pod.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-app-pvc
spec:
  accessModes:
    - ReadWriteOnce # Only one node can mount this volume as read-write
  resources:
    requests:
      storage: 2Gi # We are asking for 2 Gigabytes of storage
---
apiVersion: v1
kind: Pod
metadata:
  name: my-storage-pod
spec:
  containers:
    - name: nginx-container
      image: nginx:latest
      volumeMounts:
        - mountPath: "/usr/share/nginx/html/data" # Where the storage appears inside the container
          name: my-persistent-storage
  volumes:
    - name: my-persistent-storage
      persistentVolumeClaim:
        claimName: my-app-pvc # This links the Pod to the PVC created above

Expected output (when running 'kubectl apply -f pvc-and-pod.yaml')

persistentvolumeclaim/my-app-pvc created
pod/my-storage-pod created
Important: The claimName inside the Pod's volume definition MUST exactly match the name of the PersistentVolumeClaim. If there is a typo, the Pod will remain in a "Pending" state forever.

3. Architecture / Workflow Visualized

Let's visualize how the request flows from the Pod down to the actual physical hard drive. Notice how the PVC acts as the middleman protecting the Pod from needing to know the hardware details.

Kubernetes Dynamic Storage Provisioning Workflow

Pod (Application) Mounts Volume PVC (Claim) Requests 2Gi Storage PV (Volume) Bound to PVC StorageClass (Auto-Creates PV)

When the developer submits the PVC, the StorageClass detects the request and automatically provisions the PV, completing the binding process.

4. Step-by-Step Execution Breakdown

When you apply the YAML file above, Kubernetes performs a specific sequence of actions behind the scenes to secure your data.

Step 1: Provisioning: The Kubernetes API receives your PVC request. It checks if there is an existing PV that matches your requirements (2Gi size, ReadWriteOnce access). Since there isn't one, the default StorageClass takes over and provisions a brand new physical disk on your local machine or cloud provider.
Step 2: Binding: Once the physical storage is ready, Kubernetes creates the PV object and "binds" it exclusively to your PVC. Their status changes from Pending to Bound. No other claim can use this PV now.
Step 3: Mounting: Your Pod is scheduled onto a node. The Kubelet on that node mounts the physical disk to the node's file system, and then exposes it inside your container at the exact path you specified (/usr/share/nginx/html/data).
Watch out: Reclaim Policies. If you delete a PVC, what happens to the underlying PV and your data? By default, dynamically provisioned volumes use the Delete reclaim policy. This means deleting the PVC deletes your physical data permanently! If you want to keep the data even after the PVC is deleted, you must change the PV's reclaim policy to Retain.

5. Hands-On Practice

Ready to test your storage knowledge? Fire up your local cluster (Minikube or Docker Desktop) and try these exercises.

Beginner Exercise 1: Create a PVC

Write a YAML file to create a PVC named `database-pvc` requesting 5Gi of storage with the `ReadWriteOnce` access mode. Apply it to your cluster and check its status.

Hint: Use kubectl get pvc. The status should say Bound if your local cluster has dynamic provisioning enabled.

Intermediate Exercise 2: Write Data to the Persistent Volume

Deploy a busybox Pod that mounts the PVC you just created to `/data`. Run a command inside the pod to write a text file to that folder.

Expected result: You can run kubectl exec -it [pod-name] -- sh and type echo "Hello Data" > /data/test.txt.

Advanced Exercise 3: Prove the Persistence

Delete the Pod you created in Exercise 2 (do NOT delete the PVC). Create a brand new Pod that mounts the same PVC to `/new-data`. Execute into the new Pod and check if `test.txt` survived.

Hint: Because the PVC was never deleted, the new Pod will attach to the existing physical storage. Your file will be waiting for you!

6. Interview Questions

Storage is a critical topic for Kubernetes Administrator interviews. Here is what hiring managers want to know.

Q1. What is the difference between static and dynamic volume provisioning?

Answer: In static provisioning, a cluster administrator must manually create PVs in advance. Developers then create PVCs to claim them. In dynamic provisioning, the administrator sets up a StorageClass. When a developer submits a PVC, the StorageClass automatically creates the PV and physical storage on-demand, saving manual effort.

Q2. What are the different Access Modes available for a Persistent Volume?

Answer: There are three main modes. ReadWriteOnce (RWO): The volume can be mounted as read-write by a single node. ReadOnlyMany (ROX): The volume can be mounted read-only by many nodes simultaneously. ReadWriteMany (RWX): The volume can be mounted as read-write by many nodes simultaneously (usually requires network-attached storage like NFS).

Conclusion

Mastering Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) is essential for running serious, stateful applications in Kubernetes. By separating the storage request from the storage implementation, Kubernetes allows developers to build portable applications that work identically on a local laptop, on AWS, or on on-premises servers. Remember to always double-check your Reclaim Policies before deleting PVCs in production!

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.