DEV Community

Cover image for Difference between EmptyDir and HostPath volume types in Kubernetes
TechWorld with Nana
TechWorld with Nana

Posted on • Edited on

Difference between EmptyDir and HostPath volume types in Kubernetes

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 volume 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: {}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 hostpath.

Other values for type are DirectoryOrCreate, File, 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 root. Which means, you either need to run your container process as root or modify the file permissions on the host to be writable by non-root user, which may lead to security issues

  • You should NOT use hostPath volume type for StatefulSets


► Get 30% off - with this code: UDEMY_NANA_OCT2020: Udemy course here
Kubernetes 101: Compact and easy-to-read ebook bundle 🚀
It's a handy way to quickly look something up or refresh your knowledge at work and use it as your cheatsheet 😎

Like, share and follow me 😍 for more content:

Top comments (0)