Day 36 Task: Managing Persistent Volumes in Your Deployment 💥
1. Kuberenetes Volume
Containers are short-lived in nature. So all the data stored inside a container is also deleted if the container crashes. But kubelet will restart it with a clean state, so it will not have any old data.
So, for this problem k8s introduce a concept of volumes. A volume is a backup kind of directory which is used as storage. A volume is attached to a pod which is shared among the containers of that pod only.
2. Volume Types:
EmptyDir: When we want to share contents between multiple containers on the same pod and not to the host machine. An emptydir volume is first created when a pod is assigned to a node and it also exists as long the pod is running on that particular node.
It is initially empty. Containers in the pod can all read and write the same files in the emptydir volume, but the volume can be mounted at the same or different paths in each container. When a pod got removed from the node also the data in the emptydir got deleted.
HostPath: when we want to access the content of a pod/container from a host machine. A hostpath volume mounts a file or directory from the host node's filesystem into your pod.
3. What is Persistent Volume
Previously in the IT industry storage is managed by storage or system admin. The end user will just get the instructions to use the storage but does not have to worry about the underlying storage management.
In the containerized environment we also want to follow the same rule, for that k8s resolves this problem with the Persistent Volume concept.
A PV is a cluster-wide resource that you can use to store data in a way that it persists beyond the lifetime of a pod. The PV is not backed by locally attached storage on a worker node but by a networked storage system such as EBS (only applicable for Aws cloud) or NFS(network file system) or a distributed filesystem.
4. PersistentVolumeClaim
To use a PV you first need to claim it by using a PVC. The PVC requests a PV with your desired specification (size, access modes, speed etc)from k8s and once a suitable PV is found, it is bound to a PVC.
After a successful bound to a pod, you can mount it as a volume. Once a user finishes its work, the attached PV can be released and it can be reclaimed, also recycled for future usage.
AWS EBS[Elastic Block Storage]
An AWS EBS volume mounts an AWS EBS volume into your pod. Unlike emptyDir, which is erased when a pod is removed, the contents of an EBS volume are preserved and the volume is merely unmounted.
But you have to follow some rules while working with EBS:
The nodes on which pods are running must be on AWS ec2 instances.
Those instances need to be in the same region and availability zone as the EBS volume.
EBS only supports a single EC2 instance mounting a volume.
Task:
Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node.
Create a Persistent Volume Claim that references the Persistent Volume.
Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file.
Apply the updated deployment using the command:
kubectl apply -f deployment.yml
Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this commands
kubectl get pods
,kubectl get pv
Step 1:
Start your minikube cluster on AWS EC2 instasnce and check the status.
Step 2:
So, to perform this whole task first we have to create an EBS(Elastic Block Store) volume in AWS.
Click on volume from the left side of the section.
Step 3:
click on Create Volume.
Step 4:
Select a storage size(here I have chosen 10 as an example, you can increase or decrease the size as per the requirement.)
Now, this is the important part -The availability zone should match with your instance availability zone, in my case instance is on us-ease-1a. So both will be the same.
Click on Create (scroll down).
Step 5:
This is the volume that has been created.
Copy the volume ID and paste it somewhere.
Step 6:
Create a Persistent Volume YAML file.
Paste the EBS volume ID in the awsElasticBlockStore part and in the spec section give the storage size as 1gi(increase or decrease as per requirement).
Create the PV and check its status
Step 7:
Now, I have to claim that 1Gi storage from the EBS volume (10 GB) that I have created previously.
For that create a PVC[Persistent Volume Claim] yaml file.
Apply the changes and create the file, and check the status.
Step 8:
Now, create a deployment file(simply a pod).
Here mount the volume and give a volume path that will be accessible inside the container and mention the PVC Claim name and it will get claimed.
Step 9:
Create the deployment file and check whether the pod is running or not.
Step 10:
Now exec the pod and go inside it. Check the volume mount path(/tmp/persistent) and create a file to see if I delete the pod the container inside of this pod will also get deleted.So, If the Replica Set will create another Pod and will check there if the volume gets mounted there or not.
Create a file(here it is testfile).
exit the container.
Step 11:
Now, delete the pod and again a new pod will get created because of ReplicaSet(mentioned in the deployment file count as 1).
Step 12:
Exec to the Newly created Pod and go inside the /tmp/persistent folder as the volume got mounted here and check the file that has been created in the previous pod, the file is present here also.
Mainly, the whole process is applicable for multi-node clusters [Elastice Kubernetes Services or EKS cluster setup on AWS as the EBS is a part of AWS cloud ], but here as a practice purpose use a single node cluster Minikube.
Thank you for Reading :
Subscribe to my newsletter
Read articles from Maher Messaoudi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by