Chapter 30 of 47 · Storage, part 1

Volumes: emptyDir and hostPath

Anything a container writes to its own filesystem disappears when the container is replaced. A volume is a folder that Kubernetes mounts into the container, kept somewhere else. Different volume types keep data for different lengths of time. This chapter covers the two simplest; the next ones cover storage that truly lasts.

What survives what?

Write a file, then crash the container, delete the Pod, or move it to another node. Watch which places keep the file:

EventContainer fsemptyDirhostPathPersistentVolume
Container crashes and restarts✗ lost✓✓✓
Pod deleted✗✗ lost✓ (on that node)✓
Pod lands on another node✗✗✗ (data stays on the old node)✓ follows the Pod

How volumes are written

Always two parts: declare the volume once at Pod level, then mount it into each container that needs it:

spec:
  volumes:
    - name: shared
      emptyDir: {}
  containers:
    - name: posts
      image: micro-posts:1.0
      volumeMounts:
        - name: shared
          mountPath: /var/log/app
    - name: log-shipper
      image: fluent-bit:3.0
      volumeMounts:
        - name: shared
          mountPath: /logs
          readOnly: true

volumes

Declared under the Pod spec, next to containers. Gives the volume a name and a type (here emptyDir). The type decides where data lives and how long.

emptyDir: scratch space for one Pod

An empty folder created when the Pod starts, deleted when the Pod is removed. It survives container restarts.

Good for

  • Sharing files between containers in a Pod (logs, generated config)
  • A cache or temp files that can be rebuilt
  • Large sorts or uploads being processed

Not for

  • Anything you must keep: orders, uploads, database files
  • Sharing between different Pods (each Pod has its own)
volumes:
  - name: cache
    emptyDir:
      sizeLimit: 500Mi        # Pod is evicted if it writes more
  - name: fast-scratch
    emptyDir:
      medium: Memory          # RAM-backed (tmpfs): very fast, counts against memory limit

hostPath: a folder on the node

Mounts a real folder from the node's own disk into the Pod.

volumes:
  - name: node-logs
    hostPath:
      path: /var/log
      type: Directory          # must already exist (DirectoryOrCreate creates it)
Avoid it for your apps.

Legitimate uses: node-level agents that must see the node itself, such as log collectors reading /var/log or monitoring agents, usually run as a DaemonSet (one Pod per node). Also handy for quick experiments on a single-node minikube.

The volume family

TypeData livesChapter
emptyDirAs long as the Podthis one
hostPathOn one node's diskthis one
configMap, secretRead-only files from those objects18, 19
persistentVolumeClaimIndependent of Pods; real disks31, 32
ephemeral, csiReal storage, but only for the Pod's lifetime33

Recap

Quick check

Your posts app stores uploaded images in an emptyDir. Users say images vanish after every deploy. Why?
Show answer

A deploy (rolling update) replaces the Pods, and each new Pod gets a new, empty emptyDir. Uploads need a PersistentVolume, or better, object storage like S3.

Volumes: common questions

When is an emptyDir volume deleted?

When the Pod is removed from its node. It survives container restarts inside the same Pod.