Sub topic 11
StorageClasses
This chapter assumes you can apply YAML with kubectl and already know how a PersistentVolumeClaim (PVC) binds to a PersistentVolume (PV). Here you will see how a StorageClass lets the cluster create volumes on demand, so nobody has to pre-create each PV by hand.
What a StorageClass is
A StorageClass describes a "kind" of storage that the cluster can provide, such as fast SSD or cheap disk. It is a cluster-wide object, not tied to a namespace. A claim names a class, and the class tells Kubernetes which driver to call and with what settings.
Without classes, an administrator must create every PV in advance. With a class, a PV is created when a claim asks for it.
A StorageClass example
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: trueThe fields that matter
| Field | Meaning |
|---|---|
| `provisioner` | The driver that creates the storage. Here it is the AWS EBS CSI driver. |
| `parameters` | Driver-specific options. They differ for every driver. `type: gp3` selects the EBS volume type. |
| `reclaimPolicy` | What happens to the PV when its claim is deleted. The default for dynamically created PVs is `Delete`. Use `Retain` to keep the data. |
| `volumeBindingMode` | `Immediate` provisions at once. `WaitForFirstConsumer` waits until a Pod uses the claim. |
| `allowVolumeExpansion` | If `true`, you can grow a claim later. |
WaitForFirstConsumer matters for zonal storage such as EBS. The volume is created in the same zone as the Pod that will use it. With Immediate, the volume might land in a zone where the Pod cannot run.
Using a class from a claim
A claim selects a class with storageClassName.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast
resources:
requests:
storage: 5GiCheck the result with kubectl.
kubectl get storageclass
kubectl get pvc app-dataWith WaitForFirstConsumer, the claim shows Pending until a Pod mounts it. That is expected, not an error.
Default classes and common mistakes
A cluster can mark one class as the default with the annotation storageclass.kubernetes.io/is-default-class: "true". A claim that leaves out storageClassName gets the default class.
Setting storageClassName: "" is different. It means "no class," so the claim binds only to a PV that also has no class.
Common errors:
- Naming a class that does not exist. The claim stays
Pending. - Expecting
Retainwhen you did not set it. Dynamic volumes useDeleteunless the class says otherwise. - Trying to expand a claim when
allowVolumeExpansionis nottrue. - Changing a class and expecting existing PVs to change. Most fields, including
provisionerandparameters, cannot be updated. A class applies only to volumes created after it exists.
Exercise
A claim using a StorageClass with volumeBindingMode: WaitForFirstConsumer stays Pending right after you create it, and no Pod uses it yet. Is something broken? Why or why not?
Show answer
Nothing is broken. With WaitForFirstConsumer, Kubernetes delays provisioning until a Pod that uses the claim is scheduled. This lets it create the volume in the correct zone. The claim becomes Bound after such a Pod is created.