Containers are stateless by design — when a container restarts, its filesystem is reset. For databases and any application that needs to persist data, Kubernetes provides a layered storage model.
Volumes
A Volume in Kubernetes is a directory accessible to containers in a Pod. Unlike container filesystems, Volumes survive container restarts (within the same Pod). However, they are tied to the Pod's lifecycle — when the Pod is deleted, the Volume's data may be lost depending on its type.
Common Volume Types
- emptyDir: Temporary storage shared between containers in the same Pod. Data deleted when Pod ends.
- hostPath: Mounts a directory from the node's filesystem. Use with caution — ties the Pod to a specific node.
- configMap / secret: Injects ConfigMap or Secret data as files.
- nfs: Mounts an NFS share; supports ReadWriteMany.
- awsElasticBlockStore / gcePersistentDisk: Cloud block storage (legacy; use PV/PVC instead).
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: cache-vol
mountPath: /tmp/cache
volumes:
- name: cache-vol
emptyDir: {}
PersistentVolumes (PV) and PersistentVolumeClaims (PVC)
The PV/PVC model decouples storage provisioning from storage consumption:
- A PersistentVolume (PV) is a piece of storage provisioned by an admin or dynamically by a StorageClass. It's a cluster resource, like a node.
- A PersistentVolumeClaim (PVC) is a request for storage by a user/application. It specifies size and access mode. Kubernetes binds the PVC to a matching PV.
# PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: gp3
# Using the PVC in a Pod
spec:
containers:
- name: postgres
image: postgres:16
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: db-pvc
Access Modes
| Mode | Abbreviation | Meaning |
|---|---|---|
| ReadWriteOnce | RWO | Mounted read-write by a single node |
| ReadOnlyMany | ROX | Mounted read-only by many nodes |
| ReadWriteMany | RWX | Mounted read-write by many nodes (NFS, EFS) |
| ReadWriteOncePod | RWOP | Mounted read-write by a single Pod (K8s 1.22+) |
StorageClasses and Dynamic Provisioning
A StorageClass describes the type of storage available and enables dynamic provisioning — a PV is automatically created when a PVC is submitted:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
Cloud providers supply CSI (Container Storage Interface) drivers that implement StorageClasses:
- AWS EBS CSI driver — block storage (RWO)
- AWS EFS CSI driver — shared filesystem (RWX)
- GCE Persistent Disk CSI driver
- Azure Disk / Azure File CSI drivers
Volume Snapshots
Kubernetes supports VolumeSnapshots for point-in-time backups of PVCs. Requires CSI drivers with snapshot support.
Next: ConfigMaps and Secrets — how to inject configuration and sensitive data into Pods without embedding them in container images.