Volumes in Kubernetes
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.
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.
So now lets discuss some of the above volume mentioned in detail :
Note :
Prerequisite : Please go through this pdf to setup Minikube for Local Deplyment . This pdf includes step by step to install and setup docker, kubectl and minikube.
Link : https://docs.google.com/document/d/1-iC93GhUz1LvYAgjgDS6F3g01V0Vk2q4omTP_OifBkI/edit?usp=sharing
You can also look at my previous tutorial in which I used Minikube for Local Deplyment of single node Kubernetes cluster
Please follow each and every step otherwise you will not be able to move forward.
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:
- Create a emptyDir-pod.yml and give it a executable access using
chmod 777 emptyD-r-pod.yml
In the emptyDir-pod.yml , we will be using redis image , name of the volume mount will be redis-storage and mount location will be /data/redis and type of volume that we are using is emptyDir
2. Now for local deployment we need to start minikube server
sudo minikube start
3. Verify Minikube is running.
sudo kubectl get node
4. Verify that the Pod’s Container is running, and then watch for changes to the Pod:
sudo kubectl create -f emptyDir-pod.ymlsudo kubectl get pod redis --watch
5. 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 auxYou 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.
- 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/bashapt 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
I am excited to announce the launch of my new Udemy course, “Apache Airflow Bootcamp: Hands-On Workflow Automation.” This comprehensive course is designed to help you master the fundamentals and advanced concepts of Apache Airflow through practical, hands-on exercises.
You can enroll in the course using the following link: [Enroll in Apache Airflow Bootcamp](https://www.udemy.com/course/apache-airflow-bootcamp-hands-on-workflow-automation/?referralCode=F4A9110415714B18E7B5).
I would greatly appreciate it if you could take the time to review the course and share your feedback. Additionally, please consider sharing this course with your colleagues who may benefit from it.