Kubernetes 03: Enviromental variable ,replicaset , persistent storage in k8s container (PVC ,PV)

Kubernetes 03: Enviromental variable ,replicaset , persistent storage in k8s container (PVC ,PV)

ยท

7 min read

how we can use Envirometal variable in the Kubernetes world

file : second_pod.yml

then execute this in terminal use kubectl create -f second_pod.yml

there is no replication controller so no one is here to manage the control part if any failure is occurring

now i want to go inside the pod

kubectl exec -it mypod2 -- bash

life of enviromental variable is life of shell . if the shell is running then enviromental variable exist otherwise not so here enviromental variable is x . after you exit the container you can't access the enviromental variable

see nothing is happening means enviromental variable is deleted

In programming world this yaml file is known as a program file but in Kubernetes world it is manifest.

so make the enviromental variable permanently available we have to do something in yaml file add the enviromental variable in yaml file

for syntax visit: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

then apply the changes by using

kubectl exec -it podname -- bash

lets create the file in which enviromental variable required so lets create the mysql file

you will get the error as we have not provided the enviromental variable

command to see the enviromental variable in a pod:

kubectl set env pods -all --list

required manifest of mysql

using set based labels are good usecase

replica set supports set based labels

Different ways of creating a replica of the pod

\>>>Replication controller:-They support equality-based operator

\>>>Replica set:-They support both equality-based and set-based operator

Creating a replica set

\>>>>Manifest file for a replica set

storage management:

if you want to store the data you can store in ram and hard disk. data in ram is not persistent while in Hard disk your data is persistent

you can't store the data directly in the hard disk . for this you have to create folder inside folder created files and inside file save your data then you are able to store the data in hard disk.

to install any os minimum requirement is the hard disk. As all the files are stored on a hard disk when you install the os. you have seen the os has partitions like c and d drives. why do we make two partitions this is if anyhow windows corrupts then I have to recreate it which leads to all the data in the c drive being lost and it doesn't touch the part of the d drive. That's why we do partitions.

while in Linux we do two partitions like / and /home partitions where in / drive all os-related things are saved while in recreations all the data are lost. This type of storage is Emphermal storage. so data in c drive is temporary instead of temporary we call it as thermal storage.

here internally hard disk I have created one partition that is a c drive and attached a pen drive from outside. In this hard disk is thermal storage and pen drive is persistent storage.

Here docker is the one who launch the container and the container is launched so it will need some storage which is provided by docker these are Ephermal om nature. It means if somehow the container is terminated your data is lost.

If I launch the container through docker and the container is terminated all the data is lost but there is no matter again we install the container through the image. The problem arises when we store data inside (like program, code, image etc ) the container and if it is terminated then this data get lost. Again you launch the container but can't get add-on data.

whatever content is provided by the image is saved in the / drive. see above.

here I have deleted anaconda-post.log file from this container. so how we can retrieve it? we can retrieve it by installing again the container with the same image.

see we get back the anaconda file

but if I have created the file and if any how this container is terminated then data will not get retrieved. There is no way to get back data.

so if any folder file you want to save permanent save it by mounting the volume

docker run -dit --name myweb -v /mayank:/var/www/images vimvimal13/apache-webserver-php

if /var/www/images is not inside the container it first creates this then whatever data you save in this it is an illusion that you save it in mounted volume actually your data is synced in /mayank folder. and this folder is not a part of this os.

see the folder /var/www/images is automatically created

now if we terminate this container then if data is saved in that folder then we are successful in my aim.

see have file in /mayank folder . so how to retrieve in container so run the same command.

docker run -dit --name mayaweb -v /mayank:/var/www/images vimal13/apache-webserver-php

kubernetes storage.

samething is going to happen in kubernetes also

whenever you launch the pod there is no volume attached so it give ephermal storage you can check using

kubectl describe pods

so i delete this pod will the data exist that i have created in /var/www/html let see

see we have no file that i have created . in any of the cases if the pod get terminated you don't get the data back.

firstly I want permanent storage if yes then how many folder you want to make permanent (one folder , 2 folder and so on)

now you have to decide where you want to take the storage permanent like in host , pendrive, cloud or NFS server

there are two kinds of user

a) one is a user of Kubernetes

b) one is an admin who set up Kubernetes when you use Kubectl then you act as an admin.

in this user is the one who tells whether he wants persistent storage or not. if yes then which folder needs to be permanent? so the user can claim the persistent volume.

so where the storage comes from whether it comes from the cloud, fileserver, host etc is decided by the admin guys.

\>>>Persistent volume claim (PVC):-It is a request for volume by the user

\>>>Persistent volume (PV):-The Kubernetes admin fulfills the PVC by providing the persistent volume as per the claim. This process is called PV

creating the storage is called provisioning

Storage provisioning in Kubernetes

\>>> Manual provisioning: It is given by the Kubernetes admin

\>>>Dynamic provisioning: As soon as the request comes for PV dynamic PV is created

one storage class is already created in host let see the content in the storage class in yaml

command : kubectl get sc -o yaml

before deleting sc store in another file so when ever we want get back it

kubectl get sc -o yaml > mysc.yml

now we can easily delete storage class

kubectl delete sc --all to delete the storage class

when my user looking for storage and they claim the storage of 2GiB with read write operation

see the syntaxes and example in :

https://kubernetes.io/docs/concepts/storage/persistent-volumes/

manifest of pvc file

but you will see that your pvc is in pending state why because you don't have pv running.

kubectl get pv

so how to create the pv same as pvc

again in pv they have to tell from which physical storage either from cloud, host ,nfs etc . here I am taking host for storage

manifest file for pv file

create the pv from the manifest file

The PVC will be automatically bound to the PV as soon as it is create. Bound will only happen if both the things are happen like if mayank claim 3 GB but pv is of 2 gb then there will be no bound.

finally we have to provide this to user

now if we delete the pod and if this data exist then we are successful in aim . let see

see we get the persistent storage

use : kubectl describe pods

that how we make storage persistent in k8s container.

I hope you guys enjoys the blog do share and like it

want to connect with me

My Contact Info:

๐Ÿ“ฉEmail:-

LinkedIn:- linkedin.com/in/mayank-sharma-devops

ย