Kubernetes is a popular open-source platform used for container orchestration. However, one of the key challenges when running stateful applications on Kubernetes is managing storage. Moreover, Kubernetes provides two primary objects for managing storage: PersistentVolume (PV) and PersistentVolumeClaim (PVC).
In this tutorial, we’ll discuss the difference between PV and PVC and provide examples to illustrate their usage.
2. What Is a PersistentVolume (PV)?
A PersistentVolume (PV) is a Kubernetes resource that represents a piece of storage in a cluster. It’s a cluster-wide resource that can be used by multiple pods in a Kubernetes cluster. It can be created in a number of ways, including statically provisioning a volume, dynamically provisioning a volume, or importing an existing volume.
A PV is a way to decouple the storage from the pod, allowing the pod to access the storage independently of the pod’s lifecycle. PVs are designed to be used by multiple pods, which means that they can have a lifecycle independent of any pod that uses them.
Here’s an example of a PV YAML definition:
In this example, we create a PV with a capacity of 10GB and set its access mode to ReadWriteOnce, enabling a single node to mount the volume as read-write.
We can see that the PV has a persistentVolumeReclaimPolicy of Retain, which signifies that the PV won’t be deleted automatically when the PVC is deleted. Also, we set the storageClassName to slow, enabling PVCs to request storage from this PV using the same storage class name.
3. What Is a PersistentVolumeClaim (PVC)?
A PersistentVolumeClaim (PVC) is a Kubernetes resource that represents a request for storage by a pod. It can specify requirements for storage, such as the size, access mode, and storage class. Kubernetes uses the PVC to find an available PV that satisfies the PVC’s requirements.
A PVC is created by a pod to request storage from a PV. Once a PVC is created, it can be mounted as a volume in a pod. The pod can then use the mounted volume to store and retrieve data.
Let’s see an example of a PVC YAML definition:
With this YAML file, we create a PVC requesting 5GB of storage with an access mode of ReadWriteOnce. We set the storageClassName to slow, enabling Kubernetes to match a PV with the PVC’s storage class name.
4. Difference Between PV and PVC
The main difference between PV and PVC is that PV represents a piece of storage in a cluster, while PVC represents a request for storage by a pod.
Here are some other differences between PV and PVC:
|Multiple pods in a cluster can use it
|Represents a pod’s request for storage in a namespace
|Can create it statically, dynamically, or import it
|Created by a pod to request storage from a PV
|Has a lifecycle independent of any using pod
|Tied to the pod’s lifecycle
|Can configure with access mode, persistentVolumeReclaimPolicy, and storageClassName
|Can specify storage requirements such as size, access mode, and storage class
|Multiple pods can access it
|Only the requesting pod can access it
5. PV and PVC Example
Let’s look at an example of how to use PV and PVC in Kubernetes. We’ll create a PVC that requests storage from a PV, and then use the PVC to mount the volume in a pod.
First, we’ll generate the PV by statically provisioning a volume with the following specifications:
Next, we’ll create the PVC that requests storage from the PV:
Finally, we’ll create a pod that uses the PVC as a volume:
- name: nginx
- name: storage
- name: storage
We started by creating a PV with a capacity of 1GB and an access mode of ReadWriteOnce. Afterward, we proceeded to create a PVC that requests 1GB of storage with the same access mode. Finally, to use the PVC as a volume, we created a pod that mounted it at /data.
In this article, we saw two essential Kubernetes resources that are utilized for managing storage. Firstly, PV represents a piece of storage in a cluster, whereas PVC represents a request for storage by a pod.
Additionally, we learned that while there are similarities between these two resources, they also have significant differences in terms of scope, provisioning, lifecycle, configuration, and access.
By understanding the distinctions between PV and PVC, we can make more informed decisions about how to efficiently manage storage in our Kubernetes clusters.