Volumes
A Volume gives containers in a Pod a place to store files that outlives container restarts.
A container's own filesystem is ephemeral — restart the container and any files it wrote are gone. A Volume attaches storage to a Pod so data can survive container restarts and be shared between containers in that Pod.
Common volume types
- emptyDir — a scratch directory created when the Pod starts and deleted when it stops. Great for sharing files between containers.
- hostPath — mounts a directory from the node's filesystem (use sparingly).
- configMap / secret — surface config data as files.
- persistentVolumeClaim — durable storage that outlives the Pod (next lessons).
The catch with emptyDir
An emptyDir lives and dies with the Pod. Delete the Pod and the data is lost. For data that must truly persist, you need a PersistentVolume.
Example
apiVersion: v1
kind: Pod
metadata:
name: cache-pod
spec:
containers:
- name: app
image: myapp:1.0
volumeMounts:
- name: scratch
mountPath: /data
volumes:
- name: scratch
emptyDir: {}When to use it
- Two sidecar containers share an emptyDir volume to exchange data: one writes log files and the other reads and ships them to Elasticsearch.
- A pod mounts a hostPath volume to read node-level metrics from /proc/sys, needed by a system monitoring agent.
- A developer uses an emptyDir volume as a scratch space for temporary file processing inside a container without persisting data to a PV.
More examples
emptyDir scratch volume
Creates an emptyDir volume shared by two containers; writer writes a file and reader reads it over their shared mount path.
apiVersion: v1
kind: Pod
metadata:
name: scratch-pod
spec:
containers:
- name: writer
image: busybox
command: ["sh", "-c", "echo hello > /data/msg; sleep 3600"]
volumeMounts:
- name: scratch
mountPath: /data
- name: reader
image: busybox
command: ["sh", "-c", "cat /data/msg; sleep 3600"]
volumeMounts:
- name: scratch
mountPath: /data
volumes:
- name: scratch
emptyDir: {}hostPath volume for node access
Mounts the host's /proc directory read-only into the monitor container so it can collect node-level metrics.
spec:
containers:
- name: monitor
image: node-exporter:latest
volumeMounts:
- name: proc
mountPath: /host/proc
readOnly: true
volumes:
- name: proc
hostPath:
path: /proc
type: DirectoryList and describe pod volumes
Inspects a pod's volume declarations via describe to confirm the emptyDir is mounted and its properties.
kubectl describe pod scratch-pod | grep -A10 Volumes:
# scratch:
# Type: EmptyDir
# Medium: ""
# SizeLimit: <unset>
Discussion