Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


What are the basics of creating an emptyDir volume and how can you create an emptyDir with the help of pods?


Introduction to emptyDir volume

Hi, in this video, we'll be discussing emptyDir volume. An emptyDir volume is created on the node whenever the pod is assigned to that node. It remains throughout the pod lifecycle. The volume will be there whenever a container crashes because it's related to the pod life cycle and not the container life cycle. Some of the use cases can be used for scratch space, maybe for sorting algorithms, or it can be used as a cache, or it can be used for high computations in memory.

You can see the example where we have defined emptyDir inside the pod. In the volume spec, in the volume section of the pod spec, we have defined emptyDir. And we can also make use of a temporary file system bound by RAM by specifying the medium. If we specify medium as RAM instead of these curly braces, then the temporary file system will be mounted, backed by the node RAM; else, whatever the node SSD or disk is there, it will be used.

How to create an emptyDir volume?

So, let's create the pod by using the command kubectl create -f pod.yaml. The pod is created, and inside the pod, the emptyDir should be mounted in the /demo directory. Let's check it by using the command kubectl exec -it emptydir-pod -- sh. Then, enter cd /demo. Now, what we'll do is we will create a file using the command touch test. So, we have created a file inside this directory. Now let's exit the sh. Next, use the command kubectl get pods -o wide to see which node it is assigned. So, it is assigned to node3. Let's go to node3 and use the command docker ps. We have the container up and running. Let's inspect it using docker inspect, and we can see that the mount is there. So, this is the mount there, and the destination is /demo inside the container. So let's cd into this particular directory where the mount is located. It should be created on the node, and if we use the ls command, we can see that the test file is already there.

This directory will remain only till the pod's lifecycle. To end the pod's lifecycle, we have to delete it,and we can do that by using kubectl delete pod emptydir-pod --force --grace-period 0. So, the pod is deleted, and if I go back to ls, we can see that this particular directory is also deleted. We have seen the empty directory use cases and how to use that inside the pod. Also, we have seen how the lifecycle of the empty directory is tied to the pod lifecycle. 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