top of page

Difference between EmptyDir and HostPath volume types in Kubernetes


What is the difference between EmptyDir and HostPath?
What is the difference between EmptyDir and HostPath?

There are more than 20 volume types Kubernetes supports: Kubernetes Volume Types

In this article, I will describe different usages of EmptyDir and HostPath volume types. Each of these volumes has its own use case and should be used only in those specific cases.


EmptyDir

An emptyDir volume is first created when a Pod is assigned to a Node and exists as long as that Pod is running on that node.


As the name says, it is initially empty. All Containers in the same Pod can read and write in the same emptyDir volume.


When a Pod is restarted or removed, the data in the emptyDir is lost forever.


Some use cases for an emptyDir are:

  • scratch space for a sort algorithm, for example

  • when a long computation needs to be done in memory

  • as a cache

Example Config File with a pod that uses emptyDir:

apiVersion: v1

kind: Pod

metadata:

  name: my-pod

spec:

  containers:

  - image: my-app-image

    name: my-app

    volumeMounts:

    - mountPath: /cache

      name: cache-volume

  volumes:

    - name: cache-volume

      emptyDir: {}


Note: emptyDir volume should NOT be used for persisting data (database, application data, etc)


HostPath

A hostPath volume mounts a file or directory from the node's filesystem into the Pod. You can specify whether the file/directory must already exist on the node or should be created on pod startup. You can do it using a type attribute in the config file:

apiVersion: v1

kind: Pod

metadata:

  name: my-pod

spec:

  containers:

  - image: my-app-image

    name: my-app

    volumeMounts:

      - mountPath: /test-pd

        name: test-volume

  volumes:

  - name: test-volume 

    hostPath:

    path: /data  #directory on host

    type: Directory #optional

type: Directory defines that the directory must already exist on the host, so you will have to create it there manually first before using the host path.


Other values for type are DirectoryOrCreate, File, and FileOrCreate. Where *OrCreate will be created dynamically if it doesn't already exist on the host.


NOTE: This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.


Some uses for a hostPath are:

  • running a Container that needs access to Docker internals; use a hostPath of /var/lib/docker

  • running cAdvisor in a Container; use a hostPath of /sys

Disadvantages of using this volume type:

  • Pods created from the same pod template may behave differently on different nodes because of different hostPath file/dir contents on those nodes

  • Files or directories created with HostPath on the host are only writable by the root. This means you either need to run your container process as root or modify the file permissions on the host to be writable by a non-root user, which may lead to security issues.

  • You should NOT use the hostPath volume type for StatefulSets.


 

You can learn more about Kubernetes and other DevOps technologies on my Youtube channel 👏


Want to become a Kubernetes administrator?

Then, check out this comprehensive and practical course to become a Kubernetes administrator and master the Certified Kubernetes Administrator exam 🚀:


bottom of page