PV and PVC Explained — Kubernetes

Kiranms
Cloudnloud Tech Community
4 min readFeb 14, 2023

A persistent volume (PV) is a piece of storage in a Kubernetes cluster that is provisioned and managed by the cluster’s administrator. It is a way to separate storage from the actual pod that needs it. PVs are like a network drive that can be shared by multiple pods.

A persistent volume claim (PVC) is a request for a specific amount of storage from a persistent volume. It’s a way for a pod to claim access to a piece of storage that it needs to store data. The pod specifies the amount and type of storage it needs, and the cluster provisions a persistent volume that matches the request.

To summarize, PVs are like a pool of storage resources in a Kubernetes cluster that can be accessed by multiple pods, while PVCs are the way for a pod to request and claim access to a specific amount of storage from a PV.

By using PVs and PVCs, you can ensure that your application’s data is stored persistently and can survive pod restarts or failures. They also enable you to manage your storage resources more efficiently, by allowing you to share storage across multiple pods and dynamically provision storage as needed.

From the below PV and PVC diagram , I have just tried to draw the diagram to understand Persistent Volume and Volume Claim AccessModes in better and simple way.

Persistent Volume Claims (PVCs) can have different access modes, which define how the PVC can be accessed by the pods that use it. The available access modes are:

  1. ReadWriteOnce (RWO): This is the default access mode. It allows the PVC to be mounted as read-write by a single node in the cluster. This means that the PVC can be used by a single pod running on that node and is not available to other nodes in the cluster.
  2. ReadOnlyMany (ROX): This access mode allows the PVC to be mounted as read-only by many nodes in the cluster. This means that the PVC can be used by multiple pods running on different nodes, but they can only read from it, not write to it.
  3. ReadWriteMany (RWX): This access mode allows the PVC to be mounted as read-write by many nodes in the cluster. This means that the PVC can be used by multiple pods running on different nodes, and they can both read from and write to it.
  4. ReadWriteOncePod: The ReadWriteOncePod storage class is a pre-defined storage class that can be used to create a persistent volume with ReadWriteOnce access mode that is intended to be used by a single pod. Kubernetes ensures that pod is the only pod across your whole cluster that can read that PVC or write to it.

When creating a PVC, you can specify the access mode that is appropriate for your use case. It’s important to choose the correct access mode based on the needs of your application and the storage class that you are using. Choosing the wrong access mode can result in unexpected behavior or errors in your application.

In this example, we are creating a PV using a host path on the node’s file system, and assigning it the name my-pv-rox , my-pv-rwo & my-pv-rwx. The storage class name is set to standard, the capacity is set to 5 gigabytes, and the access mode is set to ReadOnlyMany, ReadWriteOnce & ReadWriteMany.

Next, we are creating a PVC named my-pvc-rox, my-pvc-rwo & my-pvc-rwx, requesting 1 gigabyte of storage using the standard storage class, and an access mode of ReadOnlyMany, ReadWriteOnce & ReadWriteMany.This PVC will be bound to the my-pv-rox , my-pv-rwo & my-pv-rwx PVs that we created earlier.

Note that the storageClassName in the PV and PVC specifications must match for them to be successfully bound together. Also, the accessModes specified in the PVC must be compatible with the access modes supported by the PV.

Please see the screen shot that will show the output of each command that being carried out on the minikube.

  1. Check your existing PV and PVC status.

# kubectl get pv,pvc

2. Then create PVs with Kubernetes YAML defination.

#kubectl create -f pv_ReadWriteOnce.yaml; kubectl create -f \ pv_ReadOnlyMany.yaml; kubectl create -f pv_ReadWriteMany.yaml

3. Check the status and you will notice STATUS is Available and CLAIM colume is blank, our PVs are not bout with any PVCs.

# kubectl get pvc

4. Create PVCs with Kubernetes YAML defination.

# kubectl create -f pvc_ReadWriteOnce.yaml; kubectl create -f \ pvc_ReadOnlyMany.yaml; kubectl create -f pvc_ReadWriteMany.yaml

6. Now you can check the status. and notice the STATUS and CLAIM and see how our PVC claim is happen with AccessModes.

# kubectl get pv,pvc

HAPPY LEARNING !!!!!

#Kubernetes #persistentvolume #persistentvolumeclaim #storage #cloudnloud

--

--