- Tram Ho
Kubernetes Persistent Storage provides applications deployed on Kubernetes a convenient way to request and consume storage resources. If the Ephemeral Storage has a lifetime of the pod and it can be lost after the pod is deleted or crashed, accidentally turned off, then the Persistent Storage ( persistent storage) has a lifetime independent of the pod and is not lost when the pod is deleted or crashed. Persistent Storage is essential for mission-critical applications that need stable, persistent storage far beyond the pod or even the node the pod is running on.
To create and use Persistent Storage, Kubernetes provides us with two types of API resources, PersistentVolume (PV) and PersistentVolumeClaim (PVC). In fact, Kubernetes supports many different types of volume storage, and some pure volumes can still have persistent storage. However, PV and PVC are very useful for managing storage resources for large projects. It helps to abstract away the infrastructure details, which have a higher complexity than just using pure volumes. Within the scope of this article, I will encourage you to have a basic understanding of PersistentVolume and PersistentVolumeClaim as well as how to use them.
PersistentVolume (PV) is a part of data storage space in the cluster allocated by Cluster Admin or dynamically allocated. It is a cluster resource just as a node is a cluster resource. These PVs are the same as other pure Volumes, however they exist completely independent of any pod using the PV. Currently, Kubernetes supports a lot of different PersistentVolume types installed as plugins like glusterfs, nfs, csi, … (You can see more details here).
Persistent Volume Claim
A user who wants to use the storage space (PV) needs to create a PersistentVolumeClaim (PVC). It is a storage space usage requirement (PV usage requirement). Normally the user will create a manifest PersistentVolumeClaim, specify the amount, type of storage class (storage class), request CPU resource levels, memory,… In addition, PVC can also define modes specific permissions to the storage area (for example: ReadWriteOnce, ReadOnlyMany or ReadWriteMany – you can see more details about these permissions here quyềnhere). Kubernetes will then rely on this information to find and reserve the required storage.
To better understand the relationship between PV and PVC, you can think of them as a node and a pod. If the Pod consumes the node’s resources, here the PVC will consume the PV’s resources.
Example of how to create and use Persistent Volume and Persistent Volume Claim
- Step 1: Create Persistent Volume
In this example, for simplicity, I will guide you to create a hostPath PersistentVolume.
hostPath is the type of volume storage supported by Kubernetes for development and testing on a single node cluster. A hostPath PersistentVolume will attach a file or directory right on the host node to your pod for storage.
Here, I have a configuration file for hostPath PersistentVolume which is
example-pv-volume.yaml as follows:
Then proceed to apply the configuration file:
kubectl apply -f example-pv-volume.yaml
You can view information about the newly created PersistentVolume with the command:
kubectl get pv example-pv-volume
- Step 2: Create PersistentVolumeClaim
Create a mainifest PersistentVolumeClaim named
example-pv-claim.yaml to create a request to use the PersistentVolume created above as follows:
PersistentVolumeClaim with the command:
kubectl apply -f example-pv-claim.yaml
After you create a PersistentVolumeClaim, Kubernetes will look for a PersistentVolume that meets the requirements of the PersistentVolumeClaim. If Kubernetes finds a matching PersistentVolume, with the same storage class (storageClassName), it associates the PV’s claim with that PVC.
You should be able to see this after running the PV get description command again after creating the PVC:
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
example-pv-volume 10Gi RWO Retain Bound default/example-pv-claim hostpath 2m
- Step 3: Create a pod using PersistentVolumeClaim
Finally, you will create a pod that uses your PersistentVolumeClaim as a volume attached to the pod.
Here is the config file
pod-use-pvc.yaml to create a simple nginx pod using volume:
- name: task-pv-container
- containerPort: 80
- mountPath: "/usr/share/nginx/html"
- name: example-pv-storage
And finally create the pod:
kubectl apply -f pod-use-pvc.yaml
Thus, you have a pod that uses the persistence volume as the active hostpath. One caveat for you, however, is that in the pod’s configuration, it will specify a Persistent Volume Claim (PVC) and not a Persistent Volume (PV). Thus, from the perspective of a pod, PVC will be a volume and a pod can have many volumes of different types.
In actual use, Ephemeral Storage, the existence of volumes depending on the existence of pods, is not enough for most applications. To be resilient, reliable, available, and resizable, Kubernetes applications need to be able to run multiple instances on Pods, and the Pods themselves can be scheduled or placed on other Pods. Different nodes in the Kubernetes cluster. What is needed is a stable, persistent repository that goes beyond the Pod or even the Node the Pod is running on. So Persistent Storage is needed! Hopefully, through my article, you will partly understand and understand how to use the basic Persistent Volume (PV) and Persistent Volume Claim (PVC) to create a Persistent Storage. Thank you for following my post.