This tutorial post will help you to understand about Kubernetes Persistent volumes and Claims with Best examples. At the end of this video, you will be able to understand,1. What is Kubernetes Persistent Volumes?
2. What is Kubernetes Persistent Volume Claims?
3. How it is different from other Kubernetes volume types?
4. How to Create Kubernetes Persistent Volume?
5. How to Create Kubernetes Persistent Volume Claims?
6. How to use Kubernetes NFS Persistent Volume?

Kubernetes Persistent Volumes and Claims Explained

In the previous posts, already we have explained the below topics related to Kubernetes. Refer those links to understand this topic from basics.

What is Kubernetes – Learn Kubernetes from Basics
How to Install Kubernetes on Linux (RedHat / CentOS)
How to Install Kubernetes On Ubuntu 16.04 LTS
How to Create Kubernetes Deployment, Services & Pods Using Kubectl
How to Create Kubernetes YAML for Deployment, Service & Pods
Kubernetes Volumes Explained with Examples

Also You can Watch this Entire Tutorial video with more examples on our YouTube Channel – Kubernetes Persistent Volumes and Claims Explained.

Kubernetes Persistent Volumes and Claims Explained

Lets get started.

What is Kubernetes Persistent Volumes?

What is Kubernetes Persistent Volumes

As per official kubernetes website, A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.

In simple words, Kubernetes Persistent Volumes is a solution to store data of our containers permanently even after the pods got deleted.

What is Kubernetes Persistent Volumes Claim?

What is Kubernetes Persistent Volumes Claim

A PersistentVolumeClaim (PVC) is a claim request for some storage space by users.

How it is different from other kubernetes volume types?

lets say you have multiple pod running on different nodes and you used hostpath volume type. Your data written by pod 1 running on worker node 1 will be resides only on worker node 1 and that cannot be access by pod 2 running on worker node 2. similarly pod 1 cannot access data written by pod 2 on worker node 2. right?. Since hostpath is a type that writes data only on your local node directory. Its not kind of a shared volume.
Multiple Pods running different nodes

Other example is, lets say you have one pod running on worker node1 and your pod written some data now on local worker node1.
kubernetes persistent volume examples
But Due to some reasons your pod is rescheduled to run on worker node 2, how about your data written on worker node1? your pod will be running on worker node 2 now, but your data wont be available here on worker node 2 since your data written by pod1 exists only on worker node1.

kubernetes nfs persistent volume examples

So we must have shared volume that should be accessible across all worker nodes only when pods need it. In this case, persistent volume and persistent volume claim can be used at the kubernetes cluster level.

But there is a traditional method to have shared volume across worker nodes at operating system level by mounting some volume through nfs, fc, iscsi on all worker nodes that can share the same volume. This example is discussed in the previous article.

Before I explain you how to create persistent volume and persistent volume claim, Let me explain you What is actually happening in persistent volume and how it works?

In a legacy infrastructure environment, when you need additional storage space to your server, you will reach out to the storage administrator for the space. So there would be a storage administrator who allocates some storage space from storage device to your server as you requested. Similarly, in kubernetes. Persistent volume is a resource type through which you can get your storage space allocated to your kubernetes cluster. Let’s say you got some 10G persistent volume allocated to your kubernetes cluster. Obviously that should be through any one of the kubernetes volume types. Might be through iscsi, fc, nfs, or any other cloud providers. From which you can claim some space you want for your pod using persistent volume claim. Let’s say you want 5gb for your pod. You can use persistent volume claim to request 5gb space from your persistent volume. Now you persistent volume will allocates the space you requested using persistent volume when it is find suitable, now you can use that volume claim in your deployment.

What is Kubernetes Persistent Volume

Lets see how to create Persistent Volume.

1. Create a yaml file for persistent volume to get the storage space for our kubernetes cluster.
2. Create a yaml file to claim the space using peristent volume claim as per our requirement.
3. Define the persistent volume claim in your pod deployment file.

Already I have a single pod running on worker node 1 with two containers. Sample deployment file is given below. It doesn’t have any volume specification. Let’s see how to use persistent volume and claim.

kind: Deployment
apiVersion: apps/v1
metadata:
name: ebay-app
spec:
selector:
matchLabels:
environment: dev
app: ebay
replicas: 1
template:
metadata:
labels:
environment: dev
app: ebay
spec:
containers:
– name: container1-nginx
image: nginx
– name: container2-tomcat
image: tomcat

I have a nfs server that acts as a storage and exported a volume named /nfsdata from 192.168.1.7. Traditional way is to mount the share in all worker nodes, instead we will be using this share through persistent volume. Right. So create a persistent volume yaml file.

#cat nfs_pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: ebay-pv
spec:
capacity:
storage: 20Gi
volumeMode: Filesystem
accessModes:
– ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: ebaystorage
mountOptions:
– nfsvers=4.1
nfs:
path: /nfsdata
server: 192.168.1.7

Persistent Volume supports three types of Reclaim Policy – Retain, Delete and Recycle and also it supports different access modes which are ReadWriteOnce, ReadOnlyMany and ReadWriteMany. For more in detail refer this link https://kubernetes.io/docs/concepts/storage/persistent-volumes/

Lets apply the changes and verify it.

user1@kubernetes-master:~/codes/pv$ kubectl apply -f nfs_pv.yaml
persistentvolume/ebay-pv created
user1@kubernetes-master:~/codes/pv$ kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
ebay-pv   20Gi       RWO            Recycle          Available           ebaystorage             24s

Above output shows that pv “ebay-pv” is created as expected and it is available for claim.

Lets create persistent volume claim:

user1@kubernetes-master:~/codes/ebay$ cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
storageClassName: ebaystorage
accessModes:
ReadWriteOnce
resources:
requests:
storage: 20G

Claim can be given from kubernetes cluster only when it finds suitable any Storageclassname and accessmode are same as specified in this claim file, if any persistent volume doesn’t have these storageclassname or accessmode, then persistent volume claim will not be processed.

Lets apply this and verify it.

user1@kubernetes-master:~/codes/ebay$ kubectl apply -f pvc.yaml
persistentvolumeclaim/myclaim created
user1@kubernetes-master:~/codes/ebay$ kubectl get pvc
NAME      STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myclaim   Bound    ebay-pv   20Gi       RWO            ebaystorage    15s

So our claim is validated and allocated for us.

Now we can use this claim to our pods. Edit your deployment file as below to define the volume specification. I will be using this volume only for my first container.

user1@kubernetes-master:~/codes/ebay$ cat httpd-basic-deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: ebay-app
spec:
selector:
matchLabels:
environment: dev
app: ebay
replicas: 1
template:
metadata:
labels:
environment: dev
app: ebay
spec:
    volumes:
– name: myvolume
persistentVolumeClaim:
claimName: myclaim

containers:
– name: container1-nginx
image: nginx
      volumeMounts:
– name: myvolume
mountPath: “/tmp/persistent”

– name: container2-tomcat
image: tomcat

Just apply the changes.

user1@kubernetes-master:~/codes/ebay$ kubectl apply -f httpd-basic-deployment.yaml
deployment.apps/ebay-app configured

Use “describe” option to find the volume parameters and confirm the claim is successful. it should looks like this.

user1@kubernetes-master:~/codes/ebay$ kubectl describe pods ebay-app
……..trimmed some content……
Volumes:
myvolume:
Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName:  myclaim

ReadOnly:   false
default-token-2tqkb:
Type:        Secret (a volume populated by a Secret)
SecretName:  default-token-2tqkb
Optional:    false
……..trimmed some content……

Thats it, we have successfully created persistent volume persistent volume claim. Now When your pod is rescheduled to other worker node, your data will be still available.

Also You can Watch this Entire Tutorial video with more examples on our YouTube Channel – Kubernetes Persistent Volumes and Claims Explained.

Keep practicing and have fun. Leave your comments if any.

Also refer other articles,

What is Kubernetes – Learn Kubernetes from Basics
How to Install Kubernetes on Linux (RedHat / CentOS)
How to Install Kubernetes On Ubuntu 16.04 LTS
How to Create Kubernetes Deployment, Services & Pods Using Kubectl
How to Create Kubernetes YAML for Deployment, Service & Pods
Kubernetes Volumes Explained with Examples

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.