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:
| Event | Container fs | emptyDir | hostPath | PersistentVolume |
|---|---|---|---|---|
| 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.
volumeMounts
Inside a container: "put volume shared at /var/log/app". The name must match the volume.
Same volume, second container
The log shipper sees the same files at a different path, read-only. This is how sidecars share files (chapter 5).
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)
- Tied to one node: if the Pod moves, its data stays behind (see the simulator).
- Security risk: a Pod that mounts
/or the container runtime socket can take over the whole node. Many clusters forbid hostPath for normal workloads. - Files may be created as root and clash between Pods on the same node.
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
| Type | Data lives | Chapter |
|---|---|---|
emptyDir | As long as the Pod | this one |
hostPath | On one node's disk | this one |
configMap, secret | Read-only files from those objects | 18, 19 |
persistentVolumeClaim | Independent of Pods; real disks | 31, 32 |
ephemeral, csi | Real storage, but only for the Pod's lifetime | 33 |
Recap
- A volume is declared under
spec.volumesand mounted withvolumeMounts. emptyDir: per Pod, survives container restarts, deleted with the Pod. Great for sharing and scratch.hostPath: a node folder. Node-bound and risky; for node agents and experiments only.- For data that must outlive Pods, use PersistentVolumes (next chapter).
Quick check
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.