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.

  1. All the files inside the container are temporary which means if you terminate the container you are going to lose all your files.
  2. 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.

How to use Persistent Volume and Persistent Claims

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

  1. Kubernetes Cluster: - You should have Kubernetes cluster up and running (If you do not know "How to setup then please refer to this article)
  2. 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)
  3. 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
 4  name: jhooq-demo-pv
 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:
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
1persistentvolume/jhooq-demo-pv created

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
2jhooq-demo-pv   1Gi        RWO            Retain           Available           local-storage            9s

Here is the screen of the status

create kubernetes persistent volume for local storage

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
 4  name: jhooq-pvc
 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
1persistentvolumeclaim/jhooq-pvc created

Step 3 - Lets check our Persistent Volume and persistent Volume Claim

1kubectl get pvc
1vagrant@kmaster:~$ kubectl get pvc
3jhooq-pvc   Bound    jhooq-demo-pv   1Gi        RWO            local-storage   17h

create persistent volume claim in your kubernetes cluster

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
 4  name: jhooq-pod-with-pvc
 5  labels:
 6    name: jhooq-pod-with-pvc
 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
1pod/jhooq-pod-with-pvc created

Lets check the pod status

1$kubectl get pod
1NAME                 READY   STATUS    RESTARTS   AGE
2jhooq-pod-with-pvc   1/1     Running   0          8m37s

Now you have deployed your pod successfully using the Persistent Volume and Persistent Volume Claim.

Step 3 : Test the microservice deployed under the POD

In this testing step we need to access the microservice which we deployed inside the POD.

To test the POD first we need to find the IP address on which it is running.

Use the following kubectl command to find the IP address of the POD

1$ kubectl get pod -o wide

It should return you with (You may get different IP address) -

2jhooq-pod-with-pvc   1/1     Running   0          11m   node1   <none>           <none>

You should note down the IP address of the POD because we are going to use that IP address for accessing the microservice.

1$ curl
1Hello - Jhooq-k8s

If you are using my docker-image then it should return a message "Hello - Jhooq-k8s" but you can use any docker image or docker container of your choice.

Pros and Cons

After using the persistent volume and persistent volume claim, I must say its always beneficial to use both when you are working in the production environment because you can not just delete the data after the end of the POD cycle.

The most favorable use case which I can see setting up the database such as MySQL, Oracle, Postgress inside the persistent volume so that it is always there irrespective of your POD life cycle.

But here I collected some of the Pros and Cons of using PV and PVC -

  1. Storing and archiving the logs
  2. Setting up the database
  3. Useful for application handling a large number of batch jobs
  4. Storing configs of application
  5. Independent from PODs life-cycle
  6. Easy to use
  7. Easy to backup
  1. Can not be used to store transnational data for performance-intensive application
  2. You need to set up your own backup policies
  3. Couldn't be used for HA(High Availability)


If you are reading this part of the blog post then I must say you have at least implemented PV and PVC inside your Kubernetes cluster. But to conclude it here is recap of what we did -

  1. Gone through the concepts of "What is Persistent Volume and Persistent Volume Claim"
  2. Then we created a Persistent Volume .i.e - jhooq-demo-pv with 1 Gi of storage
  3. Created the Persistent Volume Claim .i.e. - jhooq-pvc to use persistent volume jhooq-demo-pv
  4. Finally created the POD and deployed spring boot microservice.

I hope you learned something new from this post. If you interest in lap session then you can follow me on my YouTube channel

Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series