Kubernetes Storage Classes
Persistent volumes keep data alive after a pod disappears. Creating each volume by hand wastes time, because an administrator must provision disks and write a PersistentVolume object for every request. Storage classes remove that chore. A developer asks for storage, and the cluster builds the disk on demand.
The Vending Machine Analogy
A storage class works like a vending machine with labeled buttons: "Fast SSD", "Standard Disk", "Cheap Archive". A user presses a button and receives the product. Nobody walks to a warehouse. Each button points to a recipe that tells the cluster which provider to call and which settings to use.
The Three Pieces
- StorageClass describes a type of storage and the driver that creates it.
- PersistentVolumeClaim (PVC) is the request a user writes: size, access mode, and class name.
- PersistentVolume (PV) is the real disk. Dynamic provisioning creates it automatically.
Diagram: Dynamic Provisioning
Developer Cluster Cloud / Storage
───────── ───────── ───────────────
creates PVC ──────────▶ reads StorageClass
(10Gi, fast-ssd) │
▼
CSI driver calls ────────▶ creates 10Gi disk
│ │
▼ │
creates PV ◀─────────────────────┘
│
▼
binds PV to PVC
│
▼
Pod mounts the volume
Writing a StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
provisioner: ebs.csi.aws.com
parameters:
type: gp3
fsType: ext4
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
Provisioner
The provisioner names the driver that creates disks. Each cloud and storage vendor ships its own Container Storage Interface (CSI) driver. Examples include ebs.csi.aws.com, pd.csi.storage.gke.io, and disk.csi.azure.com.
Parameters
Parameters pass provider-specific options such as disk type, speed, or encryption. The accepted keys depend on the driver, so consult the driver documentation.
Reclaim Policy
- Delete removes the disk when the claim goes away. This is the default for dynamic volumes.
- Retain keeps the disk and its data for manual recovery.
Volume Binding Mode
- Immediate creates the disk the moment the claim appears.
- WaitForFirstConsumer delays creation until a pod uses the claim. The scheduler picks a node first, and the disk lands in the same zone. This mode prevents the common failure where a disk sits in one zone and the pod runs in another.
Volume Expansion
With allowVolumeExpansion: true, you grow a volume by editing the claim size. Kubernetes supports growing only; shrinking is not possible.
Requesting Storage
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-claim
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 10Gi
A pod references the claim by name.
apiVersion: v1
kind: Pod
metadata:
name: db-pod
spec:
containers:
- name: db
image: postgres:16
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: data-claim
Default Storage Class
Mark one class as the default with an annotation. Claims that omit storageClassName use it.
kubectl patch storageclass fast-ssd -p \
'{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
Local clusters such as minikube and kind ship with a default class, so a claim works immediately.
Why Dynamic Provisioning Helps Teams
Manual provisioning creates a queue. A developer files a request, an administrator builds a disk, and the project waits. Dynamic provisioning removes the queue and keeps control in the hands of administrators, since they decide which classes exist, which settings each class uses, and which limits apply. Developers pick from a short menu and get storage in seconds.
Cost control improves as well. Administrators can offer a cheap class for logs and test data and a premium class for production databases. Teams choose the tier that fits the workload, and finance teams see spending that matches the choices.
Access Modes
| Mode | Meaning |
|---|---|
| ReadWriteOnce | One node reads and writes |
| ReadOnlyMany | Many nodes read |
| ReadWriteMany | Many nodes read and write (needs a shared filesystem driver) |
| ReadWriteOncePod | One pod reads and writes |
Useful Commands
kubectl get storageclass
kubectl describe storageclass fast-ssd
kubectl get pvc
kubectl get pv
Design Tips
- Create several classes, one per performance tier, with names that describe the tier.
- Choose
WaitForFirstConsumerfor zonal disks. - Use
Retainfor production databases to protect against accidental claim deletion. - Enable expansion at class creation; changing it later needs care.
Key Points
- A StorageClass defines how the cluster builds storage.
- A claim triggers dynamic provisioning and ends manual PV creation.
- Binding mode, reclaim policy, and expansion control disk behavior.
- One default class simplifies claims for application teams.
