JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Development Team? Take two weeks Trial! Try Now

How Kubernetes volume is used for PVC to Pod Template

Introduction

Kubernetes Volumes and Designating a PVC

Why we need Volume in Kubernetes

Here, to give you a good idea of what a Persistent volume claim (PVC) is, I want to explain to you first what a volume is and why we need a volume at all with Postgres in particular.

Kubernetes Volumes and Designating a PVC

Kubernetes Volumes Internals

I will explain how we can use volume in the way of persisting data outside of a container so that the data will be persisted across restarts or termination of a given container. Now, I will elaborate on volumes in the world of Kubernetes.

“Volume” in Kubernetes is a reference to a very particular type of object. A Deployment is an object, or a Java Software Development service is an object. So, in the world of Kubernetes, we can write a configuration file that will create something called a “volume”. In Kubernetes, that object is something that allows a container to store some persistent data at the Pod level. In addition to the use of volumes, we also have access to two other types of data storage mechanisms that are “Persistent Volume” and “Persistent Volume claim”.

Note: Volume is very different than a persistent volume and persistent volume claim.

Limitations of Volumes in Kubernetes

But one problem here. The volume is tied to the pod. So, if the pod itself ever dies then the volume dies and goes away as well. So, a volume in Kubernetes will survive container restarts inside of a pod. So, now you got to know that in the world of Kubernetes, a volume is not really appropriate for storing data for a database. It definitely can work in the sense that the container can restart but we are still kind of vulnerable to anything goes wrong at the pod level itself.

Volume vs Persistent Volume(PV)

The other types of objects present in Kubernetes are Persistent Volume claim and persistent volume. I have created a diagram to specify the differences between the PVC and PV.

Kubernetes Volumes and Designating a PVC

Read more - The Importance of Kubernetes in DevOps

Persistent Volume(PV) vs Persistent Volume Claim(PVC)

Summary PVC: So, finally to summarize what a PVC is: PVC is an advertisement of options and we can ask for one of those options inside of our Pod config and handle it to Kubernetes. So, Kubernetes will at its existing stores of Persistent volume, and it will give us the volume that has already been created or it will create a new one on the fly dynamically.

PVC Demo

Persistent Volume Claim Configuration File Overview:

I am going to write out a PVC config file. This is going to advertise possible storage options that can be attached to a Pod config. So, I am going to create a YAML file here named “database-persistent-volume-claim-yaml” file.

database-persistent-volume-claim-yaml file:

Kubernetes Volumes and Designating a PVC

Explanation:

“kubectl describe storageclass”

So, when you execute the above command it gives the below output:

Kubernetes Volumes and Designating a PVC

Designating a PVC in a Pod Template

Now, I am going to create our Postgres deployment file as below:

postgres-deployment-yaml:

Kubernetes Volumes and Designating a PVC

Please look at the “template” section of the above fie. This template is used for every pod i.e. created by this deployment. As “replicas” value I have set to 1, so there will always be one pod at a time. Now I am going to update the template section and will inform this pod that when it is created, it needs to request some type of long-term storage this is nothing but a persistent volume that meets all the requirements that are advertised by PVC configured in our database-persistent-volume-claim.yaml file.

Now, inside the “spec” section of our postgres-deployment-yaml file, I am going to add a new key-value pair. I will put “volumes” of name “postgres-storage” and in the persistentVolumeClaim section, I will give the name of our PVC file.

Now our updated postgres-deployment-yaml file will look like as below:

Updated “postgres-deployment-yaml” file with Volumes configured:

Kubernetes Volumes and Designating a PVC

Explanation:

Note: I have given example here very specific to “PostgreSQL” database.

Applying a PVC:

“kubectl delete pvc database-persistent-volume-claim”

“kubectl delete pv your Persistent Volume Name”

Summary: So, here if we write any data to Postgress db and then for some reason kill that pod then ideally it will not wipe out any other data that had been stored inside the database. So, you can test this yourself by saving any data to Postgres.

Conclusion:

Here in this article, you learned the internals of Kubernetes persistent volume claim and persistent volume. Also, this blog explained to you about the need for volumes in Kubernetes and demo on how to create and designate the PVC configuration files in Pod template through an example of Postgres deployment.

Reference:

 
NSS Note

Some of our clients

team