How to use Persistent Volume and Persistent Claims | Kubernetes
Working with kubernetes is always fun as well as challenging. The more you dive deep into the kubernetes ecosystem the more you learn.
It always bugged me when I started working with kubernetes that - “How can I retain the data after the end of pod life cycle?"
Answer is -
Kubernetes Persistent Volume and Persistent claims help you to retain the data of the pod even after the end of the pod life cycle
What problems does it solve?
Containers running inside the pod can not share the files with each other.
- All the files inside the container are temporary which means if you terminate the container you are going to lose all your files.
- Secondly if in any case, your container crashes then there is no way to recover files.
Kuberenetes provides volume plugin as “Persistent Volume” to address the above problems.
The lifecycle of these volumes are independent of the lifecycle of pods.
So if PODs are terminated then volumes are unmounted and detached keeping the data intact.
What is Persistent Volume(PV)?
In simple terms, it’s storage available within your Kubernetes cluster. This storage can be provisioned by you or Kubernetes administrator.
It’s basically a directory with some data in it and all the containers running inside the pods can access it. But Persistent Volumes are independent of the POD life cycle.
So if PODs live or die, persistent volume does get affected and it can be reused by some other PODs.
Kubernetes provides many volume plugins based on the cloud service provider you are using -
awsElasticBlockStore, azureDisk, azureFile, cephfs, cinder, configMap, csi, downwardAPI, emptyDir, fc (fibre channel), flexVolume, flocker, gcePersistentDisk, gitRepo (deprecated), glusterfs, hostPath, iscsi, local, nfs, persistentVolumeClaim, projected, portworxVolume, quobyte, rbd, scaleIO, secret, storageos, vsphereVolume
How can you Create persistent volume?
There are some prerequisites before you create your persistent volume
Step 1- Prerequisites
- Kubernetes Cluster: - You should have Kubernetes cluster up and running (If you do not know “How to setup then please refer to this article)
- Docker Image/container: - A docker image that can be deployed as Kubernetes deployment. (I am going to use Spring Boot Docker image. If you do not have then please refer on how to create docker image you can follow this article)
- Directory for persistent Volume storage:- Create one directory .i.e. /home/vagrant/storage inside your Linux machine for persistent volume. You can keep the directory name and path as per your need
Once you are done with all the 3 prerequisites then your ready to proceed.
Step 2 - Create a persistent volume configuration (jhooq-pv.yml)
Its fairly easy to create it. Refer to the following configuration which you can customize with your requirements -
1apiVersion: v1 2kind: PersistentVolume 3metadata: 4 name: jhooq-demo-pv 5spec: 6 capacity: 7 storage: 1Gi 8 volumeMode: Filesystem 9 accessModes: 10 - ReadWriteOnce 11 persistentVolumeReclaimPolicy: Retain 12 storageClassName: local-storage 13 local: 14 path: /home/vagrant/storage 15 nodeAffinity: 16 required: 17 nodeSelectorTerms: 18 - matchExpressions: 19 - key: kubernetes.io/hostname 20 operator: In 21 values: 22 - node1
I saved the above configuration with name jhooq-pv.yml but you can assign any name of your choice.
In step 1 we have created Persistent Volume for Local Storage, now you need to apply the configuration.
Step 3 - Apply the configuration
1kubectl apply -f jhooq-pv.yml
Now you have created the persistent volume with the name “jhooq-demo-pv” inside the Kubernetes cluster
Step 4 - Lets check the status of PV
1kubectl get pv
1NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE 2jhooq-demo-pv 1Gi RWO Retain Available local-storage 9s
Here is the screen of the status
Great now you have created PV, let’s move ahead and create a Persistent Volume claim.
What is Persistent Volume claim?
Persistent volume provides you an abstraction between the consumption of the storage and implementation of the storage.
In the nutshell you can say its a request for storage on behalf of an application which is running on cluster.
How to use Persistent Volume claim(PVC) ?
If you as an application developer wants to use/access Persistent Volume(PV) then you must create a request for storage and it can be done by creating PVC objects.
Step 1 - Alright lets create your first Persistent Volume Claim(jhooq-pvc.yml) -
1apiVersion: v1 2kind: PersistentVolumeClaim 3metadata: 4 name: jhooq-pvc 5spec: 6 volumeName: jhooq-demo-pv 7 storageClassName: local-storage 8 volumeMode: Filesystem 9 accessModes: 10 - ReadWriteOnce 11 resources: 12 requests: 13 storage: 1Gi
Save the above configuration with file name of your choice, in mycase I am saving this file with the name jhooq-pvc.yml
Step 2 - Now apply this configuration using the following command
1kubectl create -f jhooq-pvc.yml
Step 3 - Lets check our Persistent Volume and persistent Volume Claim
1kubectl get pvc
1vagrant@kmaster:~$ kubectl get pvc 2NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE 3jhooq-pvc Bound jhooq-demo-pv 1Gi RWO local-storage 17h
Create a POD using Persistent Volume claim
Now in this step we are going to create a POD using the PV and PVC from the previous steps.
We are going to deploy Spring Boot Docker image but you can use any docker application of your choice. But if you do not have any docker image with you then you can refer to - How to deploy spring boot application in Kubernetes cluster
Step 1 - Create POD configuration yml .i.e. - “jhooq-pod.yml”
1apiVersion: v1 2kind: Pod 3metadata: 4 name: jhooq-pod-with-pvc 5 labels: 6 name: jhooq-pod-with-pvc 7spec: 8 containers: 9 - name: jhooq-pod-with-pvc 10 image: rahulwagh17/kubernetes:jhooq-k8s-springboot 11 ports: 12 - containerPort: 8080 13 name: www 14 volumeMounts: 15 - name: www-persistent-storage 16 mountPath: /home/vagrant/storage 17 volumes: 18 - name: www-persistent-storage 19 persistentVolumeClaim: 20 claimName: jhooq-pvc
One point you can note over here is we are using “persistentVolumeClaim” which is “jhooq-pvc” .
Step 2 : Lets apply the pod configuration
1kubectl apply -f jhooq-pod.yml