Persistent Volume in Kubernetes
To define persistent volume in Kubernetes, it is physical storage like an NFS server or SSDs available for the cluster in the form of objects. It is a pre-provision storage unit present in the cluster which is provided by the administrator. The persistent volume is attached to the cluster which can be used by the pods using the persistent volume claims. The persistent volume claim is a request made by the developer for storage. The developers make some requests for storage and access modes like read or write. The cluster maps the PVC request to the matching PV and in case there is no matching PV then the cluster will dynamically create a matching PV based on the storage class.
Look at the image given below to understand how the persistent volume and persistent volume claim work in a Kubernetes cluster.
As demonstrated in the image, the admin -cluster administrator- creates the persistent volume (PV) within the cluster that will bind to the persistent volume claim (PVC). The pod will use the PVC where the user can create the PVC and pod in the cluster. We will now demonstrate how to set up a pod to use the PVC as storage.
Prerequisites
Before you begin to learn how to configure a pod to use the PVC, make sure you meet the following basic needs:
- Minikubes should be installed to run the kubectl commands
- Kubernetes cluster with one node
- Kubectl CLI
- Basic understanding of persistent volume
When you have these prepared, you can begin.
Platform Setup for Pod Configuration
Kubernetes is the favorite platform of developers for containerized applications. The persistent volume provides persistent storage for containerized applications. Now, we are going to set up the platform for the Kubernetes cluster and configure the pods to use the PVC storage. The first step is to start the minikube terminal by using the following command:
Next, we will create the directory for the files to be created next. The ‘sudo mkdir’ command is used to create the directory:
When you enter this command, the server will ask for the admin password to grant admin privileges to the command.
Now, you can create the html file as a super user and echo data in it. Here, the index.html file will be created where ‘hello from Kubernetes storage’ is stored.
How to Create a Persistent Volume?
This article is mainly focused on creating the persistent volume and configuring the pods for the persistent volume claims. So, in the coming section, we will be guiding you on how to create a persistent volume in Kubernetes. Here, we are going to create the hostpath persistent volume since it is supported by Kubernetes for testing and development on a single-node cluster. The network attached storage is emulated on the node to use a file or directory. Let us begin the process of creating the persistent volume.
Step # 1: Create a Configuration YAML File
First, we need a YAML file for storing the services configuration. The configuration file will contain the details of services to be used for creating the persistent volume. Here is the command to create a YAML file:
When you execute this command, the following YAML file will be created containing the hostpath persistent volume configuration:
Step # 2: Create the PV from the Configuration File
The persistent disc will be built using the configuration file. The persistent volume (PV) will be made using the command line:
The ‘kubectl apply’ command is used to create the persistent volume. The command is followed by the ‘-f’ parameter along with the configuration file name. The following output will be generated after executing the ‘apply’ command:
You can view the information about the persistent volume by using the ‘kubectl get’ command. See the complete command given below:
The persistent volume will contain the name, capacity, access modes, reclaim policy, status, claim, storage class, reason, and age. Look at the output given in the snippet below:
How to Create a Persistent Volume Claim (PVC)?
The persistent volume claims are used by the pods to request physical storage. The following steps will help you learn how to create PVC.
Step # 1: Create the YAML File
First, create the YAML configuration file to present the configuration details in it. If you already have the configuration file, you can simply use that but if you do not have the configuration file then you need to specifically create it. Use the command given to create the YAML configuration file:
When you execute this command, the following YAML configuration file will open up in the terminal:
Step # 2: Create the PVC from the Configuration File
The persistent volume claim is created using the configuration file in the next step. The same ‘kubectl apply’ command can be used to create the PVC from the configuration file. Here is the complete ‘kubectl apply’ command:
Step # 3: Check the PVC Information
The information in the PVC must now be configured. To create the PVC, use the command written below:
This will show that the PV is bound to the PVC, look at the output below:
How to Create a Pod?
The cluster consists of three things, PV, PVC, and pods. We have created the PV and PVC; the final step is to create the pod. Here are the steps for creating the pod:
Step # 1: Create the YAML Configuration File
First, we need to create the configuration YAML file for listing the services. The command given below is used to create the YAML file:
When you execute this command, the following YAML file will open up in your terminal:
Step # 2: Create the Pod from the Configuration File
Next, we will create the pod from the configuration file and to verify whether the pod is in the container or not we will use the following command:
What are the Steps to Perform the Cleanup?
After using all these services, we should perform a cleanup to have free space. For that, we need to be using the commands mentioned here.
This will delete the pod we created previously:
This will delete the PVC which we created above:
This will delete the PV that we created above:
Conclusion
In this tutorial, we learned that a cluster contains a PV, PVC, and a pod. The PV and PVC bind together to fulfill the demands of the pods or the developers. It also guided us on how to create PV, PVC, and pods. After creating all the services, we also learned how to perform the cleanup to delete all these services.