Beginners guide to emptyDir and hostPath

Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism

Description

How to use emptyDir and hostPath within the Kubernetes ecosystem and how this connects to Kubernetes volumes?


Transcription

What is emptyDir?

In this video, we'll be discussing Kubernetes volumes and specifically the emptyDir and the hostPath volume. Now, emptyDir volume gets created as soon as the Pod is assigned to the node and it stays throughout the lifespan of the Pod. But with the deletion of a pod, the emptyDir also gets a removal. There can be specific use cases that emptyDir can be used, such as the scratch space for some sorting algorithm or the checkpointing of a long computation in the RAM.

EmptyDir can be either based on the node's disk attached to the node or, if you specify the emptyDir medium over here instead of the empty braces, it will create a temporary TMPFS and use the RAM as the volume for that. So we can write it in a yaml file, then in the container section, we can see the image, name, and command, which is simple. Now, this is the critical piece where we define the emptyDir volume. To specify, in the volume section, we say emptyDir: {} if you want the disk volume or the medium as memory or if you want to have RAM. Then next is the volumeMount, where we mount that specific emptyDir into a particular directory inside the container. So this was all about emptyDir.

What is a hostPath?

Next is the hostPath. Now, a hostPath volume mounts a file or a directory from the node's file system into the Pod. A hostPath will mount a directory, which is present on the node and mounted inside the container.

In this particular example, we can see that the image, name, and command are the same. But the volumes we have changed to hostPath, and we give the path on the host. So this particular path is on the host. We can also provide the type, whether it's a file, a directory, a file that should already exist, or a directory that should already exist or create. DirectoryOrCreate will check whether the directory already exists. If not, then it will create. So it can have various use cases, like a running container needs access to docker internals.

The valid docker can be mounted, or cAdvisor can be run in a container by mounting /sys of a node, or it can also be used for a Pod check. A Pod will first check whether this particular directory exists and if yes, only the Pod will run. This is how we can use different use cases for the hostPath. Now, these both are not used for persisting the data. So, it's not used to save the critical data of the databases or the needed applications, but it has its specific use cases. That's all about emptyDir and hostPath volume. Thank you for watching. See you in the next one.

Don't stop now, check out your next lesson