Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


The basics of creating a hostPath volume and how to use it with a pod inside a Kubernetes cluster.


Introduction to hostPath volume

Hi, in this video, we'll be going through the hostPath volume. In Kubernetes, a hostPath volume means mounting a file or a directory from the node's host inside the pod. Now, you can specify different things like a directory that should be already existing or should be created on the pod creation. Some of the advantages can include, if you create a valid docker hostPath, so a pod can use to access docker, or if you are running a '-c' advisor, then /sys can be used as a hostPath, and this is how the hostPath is created.

In the pod spec section, in volumes, you define the hostPath and this particular path which is /data, will be created because the type here is DirectoryOrCreate, which means if it is not there, then it will be created. And though 'type' is not a mandatory field, /data will be on the node where the pod is pinned up, and it is mounted inside the container in the /demo directory.

How to create a hostPath volume?

Let's create the pod with the kubectl create -f pod.yaml command and check the creation using kubectl get pods. And let's exec it by kubectl exec -it hostpath-pod -- sh. So, the mount entry inside the container is /demo, let's go to the /demo folder through cd, and let's create a test file by using touch test. After exiting it, let's see where it is running by using kubectl get pods -o wide. This particular pod is running on node3. So, I already have node3 open, and inside that, it should have /data. This is node3 and let's go to the directory through cd /data/. We can see our test file is already created, so whatever we create inside the pod, we can see it inside node3. That's how the mounting is done.

We have successfully created a pod and used hostPath as the volume and discussed its advantages. We do not use it for production purposes. The reason behind that is that suppose next time the pod gets deleted and gets created again, gets recreated on a different node, then the files on the hostPath of node3 would not be on node2. So, there will be a mismatch in the files, and you will not get the desired result as expected. That's it for this lecture. Thank you for watching. See you in the next one.

Don't stop now, check out your next lesson