How to use Persistent Volume and Persistent Claims | Kubernetes

Share on:

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.

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 knowHow 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
 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
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
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
1persistentvolumeclaim/jhooq-pvc created

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
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) -

1NAME                 READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
2jhooq-pod-with-pvc   1/1     Running   0          11m   10.233.90.3   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 10.233.90.3:8080/hello
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 -

Pros
  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
Cons
  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)

Conclusion

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