Authors – All

If you have a few years of experience in the DevOps ecosystem, and you're interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

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:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-example
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  hostPath:
    path: /data

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:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-example
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: slow

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:

Property PV PVC
Scope Multiple pods in a cluster can use it Represents a pod’s request for storage in a namespace
Provisioning Can create it statically, dynamically, or import it Created by a pod to request storage from a PV
Lifecycle Has a lifecycle independent of any using pod Tied to the pod’s lifecycle
Configuration Can configure with access mode, persistentVolumeReclaimPolicy, and storageClassName Can specify storage requirements such as size, access mode, and storage class
Access 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:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-storage
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data

Next, we’ll create the PVC that requests storage from the PV:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-storage
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Finally, we’ll create a pod that uses the PVC as a volume:

apiVersion: v1
kind: Pod
metadata:
  name: pod-storage
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: storage
      mountPath: /data
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: pvc-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.

6. Conclusion

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.

Authors – All

If you have a few years of experience in the DevOps ecosystem, and you're interested in sharing that experience with the community, have a look at our Contribution Guidelines.