~13 min
Containers in a Pod have separate filesystems by default; a
volume is a directory made available to some or all of a
Pod's containers, decoupling storage from any single
container's lifetime. Volumes split into two lifetimes.
Ephemeral volumes, such as emptyDir, are created when the
Pod is assigned to a node and exist only as long as that Pod
does. Persistent volumes exist independently of any one
Pod: a PersistentVolume (PV) represents an actual piece of
storage, and a PersistentVolumeClaim (PVC) is a namespaced
request for storage that a Pod mounts by name, without needing
to know how or where that storage is actually provisioned.
For any volume type, data survives a container crash and restart within the same Pod — what changes its fate is whether the Pod itself goes away.
bash
apiVersion: v1
kind: Pod
metadata:
name: fetch-and-serve
spec:
containers:
- name: fetcher
image: fetcher:1.0
volumeMounts:
- name: shared
mountPath: /data
- name: server
image: nginx:1.27
volumeMounts:
- name: shared
mountPath: /usr/share/nginx/html
readOnly: true
volumes:
- name: shared
emptyDir: {}A PVC's accessModes field states how many nodes, and in what
mode, the underlying storage can be mounted at once:
ReadWriteOnce (RWO) allows read-write mounting by a single
node; ReadOnlyMany (ROX) allows many nodes to mount the same
volume read-only at the same time; ReadWriteMany (RWX) allows
many nodes to mount it read-write concurrently; and
ReadWriteOncePod (RWOP) is the strictest of the four,
restricting the volume to exactly one Pod cluster-wide rather
than one node. Not every storage backend supports every mode —
a cloud block-storage volume, for instance, typically only
supports RWO.
When a PVC names no storageClassName and the cluster has a
StorageClass marked default, Kubernetes provisions a new
PersistentVolume dynamically to satisfy the claim using that
default class, rather than requiring an administrator to have
pre-created one. Because a PersistentVolume is a cluster-scoped
object but a PersistentVolumeClaim is namespaced, a Pod can only
mount a PVC that lives in its own namespace.
ReadOnlyMany (ROX) — it allows many nodes, and the Pods scheduled to them, to mount the same volume read-only concurrently.