Wednesday, September 14, 2022

K8 Volume

Storage/Volume In Kubernetes:


Why we need Volume in Kubernetes: If pods got deleted data which is inside pod is also deleted and cannot recovered.

When volume is mounted with pods data is stored in that volume.

Volumes are required in stateful containers/pods

Stateful Application: Examples of stateful application are database, applications that store data.

Stateless Application: They don't keep record of state. Each request is completely new.

Storage Volme: 

1)Local Volume: Volume is provided from the node on which pods are created.

2)Shared Volume:

  -> SAN

  -> NAS

PV/Persistent Volume: It is a volume controller resource which contains storage information, type of storage, where storage is running and which mode.

PVC/Persistent Volume Claim: Space is claimed from PV for POD that is claimed through PVC.

POD --PVC --Bound--PV--Storage Server







PV and PVC Details Information: 

Kubernetes Persistent Volumes and Claims Explained (learnitguide.net)



====================================

In Kubernetes, a volume can be thought of as a directory which is accessible to the containers in a pod. We have different types of volumes in Kubernetes and the type defines how the volume is created and its content.

The concept of volume was present with the Docker, however the only issue was that the volume was very much limited to a particular pod. As soon as the life of a pod ended, the volume was also lost.

On the other hand, the volumes that are created through Kubernetes is not limited to any container. It supports any or all the containers deployed inside the pod of Kubernetes.

Kubernetes Persistent Storage | Everything You Need To Know (k21academy.com)

Types of Kubernetes Volume

Some of the Kubernetes Volumes are :

  • emptyDir − It is a type of volume that is created when a Pod is first assigned to a Node. It remains active as long as the Pod is running on that node. The volume is initially empty and the containers in the pod can read and write the files in the emptyDir volume. Once the Pod is removed from the node, the data in the emptyDir is erased.
  • hostPath − This type of volume mounts a file or directory from the host node’s filesystem into your pod.
  • gcePersistentDisk − This type of volume mounts a Google Compute Engine (GCE) Persistent Disk into your Pod. The data in a gcePersistentDisk remains intact when the Pod is removed from the node.
  • awsElasticBlockStore − This type of volume mounts an Amazon Web Services (AWS) Elastic Block Store into your Pod. Just like gcePersistentDisk, the data in an awsElasticBlockStore remains intact when the Pod is removed from the node.
  • nfs − An nfs volume allows an existing NFS (Network File System) to be mounted into your pod. The data in an nfs volume is not erased when the Pod is removed from the node. The volume is only unmounted.
  • gitRepo − A gitRepo volume mounts an empty directory and clones a git repository into it for your pod to use.
  • azureDiskVolume − An AzureDiskVolume is used to mount a Microsoft Azure Data Disk into a Pod.


EmptyDir volume

emptyDir is a temporary directory and when the pod is destroyed, it will destroy the shared volume and all its contents.

Let’s create a pod with two containers that use an emptyDir volume to exchange data:

  1. Create a emptyDir-pod.yml and give it a executable access using
chmod 777 emptyD-r-pod.yml



kubectl create -f emptyDir-pod.yml


Open another terminal, get a shell to the running Container:

kubectl exec -it redis -- /bin/bash

In your shell, go to /data/redis, and then create a file and dont exit the container :

echo "I am inside redis container" >> test_file.txt

In your shell, list the running processes in the container :

 sudo apt-get update
sudo apt-get install procps
ps aux
You output will look like this :USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
redis 1 0.1 0.1 33308 3828 ? Ssl 00:46 0:00 redis-server *:6379
root 12 0.0 0.0 20228 3020 ? Ss 00:47 0:00 /bin/bash
root 15 0.0 0.0 17500 2072 ? R+ 00:48 0:00 ps aux

In the shell, kill the Redis process:

sudo kill <pid> where is <pid> will be redis process ID
sudo kill 1

In your original terminal, watch for changes to the Redis Pod.You will see something like this:

NAME      READY     STATUS     RESTARTS  AGE
redis 1/1 Running 0 11s
redis 0/1 Completed 0 4m
redis 1/1 Running 1 4m

At this point, the Container has terminated and restarted on its own .

6. Delete the Pod that you created for this exercise:

  • kubectl delete pod redis








Hostpath volume

hostpath also node-local types k8s volume. This has the accessibility to from the host without accessing the cluster resources.

Persistent Volume : A persistent volume is a piece of storage in a Kubernetes cluster.

  1. Creating Persistent Volume : In the specication section we can see we are sharing local storage of 10 GB with read-write access and the folder path that we want to share.

Inside the shared folder I have created a file index.html and added follwing content “Hey this is Sarthak Srivastava Executing HostPath Volume in Kubernetes” and make it executable using chmod 777 index.html

2. Save the file and create the persistent volume.

sudo kubectl create -f hostpath.yml

3. You can view persistant volumes using :

sudo kubectl get pv

PersistentVolumeClaim

Now we will create a PersistentVolumeClaim which will claim the space to be referenced in a pod. A PersistentVolumeClaim is created by specifying the minimum size and the access mode they require from the persistentVolume.

In the specication section we can see we are claiming local storage of 5 GB out of 10 with read-write access.

4. Save the file and create the persistent volume claim .

Create a Pod

The next step is to create a Pod that uses your PersistentVolumeClaim as a volume.

This is the configuration file for the Pod named httpd in which no of replicas are 2 , containers are using image httpd , port exposed is 80 , mount location in container are /usr/local/apache2/htdocs

5. Create the Pod

sudo kubectl create -f apache-deployment.yml

6. Now we will expose our deployment and run the command to get the url

sudo kubectl expose deployment httpd --type=NodePort
sudo minikube service httpd

7. Copy the url and paste it on the browser

Or access the shell of the container running in your Pod using :

#This will take you to inside of the container
kubectl exec -it httpd -- /bin/bash
apt update
apt install curl
curl http://localhost/

This will show same output as above mentioned on browser

Clean up

Delete the Pod, the PersistentVolumeClaim and the PersistentVolume:

kubectl delete pod httpd
kubectl delete pvc hostpath-pvc
kubectl delete pv pv-local







No comments:

Post a Comment

Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117