Kubernetes Storage : Overview of PV and PVC ☸☸

Everything about Storage in Kubernetes :))

Vibhor Chinda
Google Cloud - Community
7 min readApr 3, 2023

--

Introduction 🚩🚩

Hi fellow Readers 👋 :))
It has been long since I wrote something but I feel it’s always better to restart positive habits even if we feel it’s too late than never.
Previously in my articles, I have covered various concepts related to Kubernetes and continuing on those lines. Today I will writing about the concept of “Storage in Kubernetes”.

Storage in Kubernetes is one of my favorite topic as it is really interesting to visualize plus easy to understand too.

So In this article, we will try to learn about :

  • Why do we need to provision storage options in Kubernetes ??
  • What is Persistent Volume (PV) in Kubernetes ??
  • What is Persistent Volume Claim (PVC) in Kubernetes ??
  • How to use PVs and Configure PVCs in Pods ??

It will be extremely interesting article.
So without any further delay, lets get started with it 🐵

Why do we need to provision storage options in Kubernetes ??

Photo by D koi on Unsplash

In the Kubernetes world, the PODs created in Kubernetes are transient in nature.
It means that in a default setup, when a POD is created to carry out any work. It runs the application/work it is meant to do and all the data which is generated during the process basically remains alive inside the POD till the time POD is up and running.
Once the POD dies, the data which was stores inside it also vanishes and the administrator looses all access to it too.

The fact that in the default setup, the data associated with any POD gets deleted after the POD gets deleted. Brings up a huge problem.
As the data can be very important thus needs to be preserved even if the POD is deleted.

So in order to preserve data, we need provision storage options in Kubernetes. Now the question arises what is most basic storage option and how can one provision it ?

What is Persistent Volume (PV) in Kubernetes ?? 😐😐

Persistent Volumes in Kuberentes

Persistent Volume is a cluster-wide pool of storage volumes configured by an administrator to be used by users deploying application on the cluster. The users can now select storage from this pool using Persistent Volume Claims.

In simple words, an administrator basically creates a pool of storage in whatever storage provider organization might be using. Now the PODs running inside various Kubernetes clusters, can claim the storage they require from this pool.

In this way, Persistent Volumes act as a storage option for various PODs running inside Kubernetes Clusters.

We can create Persistent Volume (PV) using YAML files. To create a Persistent Volume (PV) with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.

Below is an example of a Persistent Volume (PV) definition file (pv.yaml)

kind: PersistentVolume
apiVersion: v1
metadata:
name: pv-vol-test
spec:
accessModes: [ "ReadWriteOnce" ]
capacity:
storage: 1Gi
hostPath:
path: /tmp/local/data

There are many aspects and components in this file. Lets break down each one of them 😀

  • Let’s start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the Persistent Volume.
  • Next is kind which signifies the kind of definition file this is. In our case, it is a “PersistentVolume”.
  • Next is metadata, which is a dictionary including the item name and labels. The metadata stores values that are assigned to the Persistent Volume (PV) which is being created.
  • Finally there is spec which is actually an array/list. Following are the values which are present in it and what they mean.
    capacity : It basically specifies what size of storage.
    hostPath : It basically specifies the Path on where the storage will be created.

In a production cluster, you would not use hostPath. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume.

We are done with the Persistent Volume definition file. Now we can save and exit the file.

Use this command to create the Persistent Volume based on the above YAML file :

kubectl create -f pv.yaml

Use this command to list Persistent Volume

kubectl get pv

Use this command to get more details of the Persistent Volume

kubectl describe pv pv-vol-test

What is Persistent Volume Claim (PVC) in Kubernetes ?? 👀👀

Persistent Volume Claim (PVC)

Persistent Volume Claim is an object in Kubernetes, which helps to bind a POD with a suitable Persistent Volume.

To understand how whole cycle works, let us summarize it in some simple points :

  1. Administrator creates some Persistent Volumes in the Storage provider used by the organization.
  2. User create Persistent Volume Claims and specifies the resources their application will be requiring in it.
  3. After that user spins up PODs which will be running their specified applications in them.
  4. The POD definition file will have PVC name specified in it which user created in Step 2
  5. After being associated to the POD, PVC basically attaches that POD to the PV which it finds suitable according to the requirements specified by the user in that PVC.

In this way, Persistent Volume Claim helps to attach suitable Persistent Volume with their associated PODs.

We can create Persistent Volume Claim (PVC) using YAML files. To create a Persistent Volume Claim (PVC) with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.

Below is an example of a Persistent Volume Claim (PVC) definition file (pvc.yaml)

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi

There are many aspects and components in this file. Lets break down each one of them 😀

  • Let’s start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the Persistent Volume Claim.
  • Next is kind which signifies the kind of definition file this is. In our case, it is a “PersistentVolumeClaim”.
  • Next is metadata, which is a dictionary including the item name and labels. The metadata stores values that are assigned to the PersistentVolumeClaim (PVC) which is being created.
  • Finally there is spec which is actually an array/list. Following are the values which are present in it and what they mean.
    storage : It basically specifies what size of storage is required.
    accessModes : It basically specifies what access mode should be present on PV that is required.

We are done with the Persistent Volume Claim definition file. Now we can save and exit the file.

Use this command to create the Persistent Volume based on the above YAML file :

kubectl create -f pvc.yaml

Use this command to list Persistent Volume

kubectl get pvc

Use this command to get more details of the Persistent Volume

kubectl describe pvc myclaim

How to use PVs and Configure PVCs in Pods ?? 🍃 🍃

Photo by Lukas on Unsplash

In previous two sections, we have learnt about what is Persistent Volume (PV) and Persistent Volume Claims (PVC) plus how can to configure both of them.
Though we have learnt to configure both of them, but we still haven’t glued the pieces together so that a POD can use any Persistent Volume (PV) by using Persistent Volume Claim (PVC).

Let us now see how can we associate any Persistent Volume Claim (PVC) in the POD definition file. [Refer to the volumes section]

apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: web
volumes:
- name: web
persistentVolumeClaim:
claimName: myclaim
  • In this case, Pods access storage by using the claim as a volume. Persistent Volume Claim must exist in the same namespace as the Pod using the claim.
  • The cluster finds the claim in the Pod’s namespace and uses it to get the Persistent Volume backing the claim. The volume is then mounted to the host and into the Pod.
  • Persistent Volume is a cluster-scoped and Persistent Volume Claim is a namespace-scoped.

What next ?? 👀 👀

Thanks a lot for reaching till here! This is the end of this article.
But we have only scratched the surface of the K8s ecosystem :))
Much more to go, it will be a fun journey where we will learn a lot of cool stuff together.

Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))

In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles

Twitter — https://twitter.com/ChindaVibhor

LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/

Related Articles

I will still keep on coming with new articles covering a bunch of topics I am exploring.

That’s All folks !! Doodles :))

--

--

Vibhor Chinda
Google Cloud - Community

Software Developer 2 @Guidewire | Ex - VMware | CKA | Exploring Cloud Tech | Developing Patience ✨✨