Skip to content
Visual explainer 2h to learn 3 steps

StorageClasses & dynamic provisioning

Provision volumes dynamically and set a default StorageClass.

By Invicly Tech, , in Kubernetes Administrator (CKA). About 9 minutes to read.

Topic 25 of 33

In short

A StorageClass describes a kind of storage and the provisioner that creates it. When a PVC names a class, the provisioner creates a matching PV automatically. One class can be marked default so PVCs without a class still get storage.

Explained simply

Creating a PV by hand for every claim is slow. In real clusters, storage is created on demand. A StorageClass is a recipe: "fast SSD disks from this cloud, deleted when no longer needed". When a claim names that recipe, a matching disk is made automatically.

The piece that actually makes the disk is the provisioner, today almost always a CSI driver (Container Storage Interface). Each cloud and storage vendor ships its own. Kubernetes talks to all of them the same way.

One class can be marked as the default. Then a claim that names no class at all still gets storage. Most managed clusters and tools like kind come with a default class already, which is why a plain PVC often just works.

A StorageClass also controls when the disk is made and what happens to it later: right away or when a Pod needs it, deleted or kept after the claim is gone, and whether it can grow.

Press Play on a diagram to walk through it one step at a time.

SequenceDynamic provisioning
User
API server
CSI provisioner
Storage backend
  1. 1Create PVC, class fast
    User to API server: Create PVC, class fast
  2. 2Watch sees unbound PVC
    CSI provisioner to API server: Watch sees unbound PVC
  3. 3Create volume
    CSI provisioner to Storage backend: Create volume
  4. 4Volume ID returned
    Storage backend to CSI provisioner: Volume ID returned
  5. 5Create PV for the claim
    CSI provisioner to API server: Create PV for the claim
  6. 6PVC is Bound
    API server to User: PVC is Bound
FlowA PVC with WaitForFirstConsumer
  1. 1
    PVC created

    Class local-path; status Pending

  2. 2
    Pod created

    Uses the claim; scheduler picks node2

  3. 3
    Node chosen

    Scheduler notes node2 on the PVC

  4. 4
    Provisioner creates volume

    On node2, then creates the PV

  5. 5
    Bound and mounted

    kubelet on node2 mounts it

In depth

Every field of a StorageClass

provisioner names the driver, such as ebs.csi.aws.com, pd.csi.storage.gke.io, disk.csi.azure.com or rancher.io/local-path. parameters are passed to that driver and mean different things for each, for example a disk type like gp3. reclaimPolicy is Delete (default) or Retain and is copied into every PV the class creates. volumeBindingMode is Immediate (default) or WaitForFirstConsumer. allowVolumeExpansion lets claims grow. mountOptions and allowedTopologies fine-tune mounts and zones.

The provisioner and parameters cannot be changed after creation; you delete and recreate the class, which does not affect existing PVs. Changing a class's reclaimPolicy also does not change PVs it already made, so patch those PVs directly if needed.

  • kubectl get sc shows PROVISIONER, RECLAIMPOLICY, VOLUMEBINDINGMODE
  • kubernetes.io/no-provisioner is for static local PVs with WaitForFirstConsumer
  • kubectl get csidrivers lists installed CSI drivers

Immediate versus WaitForFirstConsumer

With Immediate, the disk is created as soon as the claim appears. In a cluster spread over zones, that disk might land in zone a, and then the Pod using it can only run on nodes in zone a, even if those are full. With WaitForFirstConsumer, the claim waits until the scheduler picks a node for a Pod that uses it, and the disk is made in that node's zone.

WaitForFirstConsumer is the better default for zonal or local storage, and kind's local-path class uses it. Its side effect confuses people: the PVC stays Pending until a Pod exists, and that is normal. Also, a Pod that sets spec.nodeName skips the scheduler, so the delayed binding never happens; use a nodeSelector instead.

CompareWhen is the volume created?
Immediate
  • As soon as the PVC exists
  • Zone picked without knowing the Pod
  • Pod may be stuck in the wrong zone
  • PVC Bound before any Pod
WaitForFirstConsumer
  • After a Pod is scheduled
  • Volume made in the Pod's zone or node
  • Needed for local volumes
  • PVC Pending until then: normal

How the default class is applied

The DefaultStorageClass admission plugin fills in storageClassName for new claims that leave it out, using the class annotated storageclass.kubernetes.io/is-default-class: "true". If several are marked default, the most recently created one wins, which is rarely what you meant, so keep exactly one. Claims created while no default existed are updated once a default appears.

To opt out of dynamic provisioning entirely, set storageClassName: "" on the claim; it will then bind only to PVs without a class. To switch the default, set the annotation to "false" on the old class and "true" on the new one. The kubectl get sc output marks the default with (default) after its name.

What a CSI driver does

A CSI driver usually runs as two parts. A controller Deployment, with sidecars such as external-provisioner and external-attacher, watches claims and calls the storage API to create, delete and attach volumes. A node DaemonSet, registered with each kubelet, mounts the volume into the Pod's folder on that node.

So when dynamic provisioning fails, look in two places: the PVC events (provisioning errors such as quota limits or bad parameters come back here) and the driver's controller Pod logs. Attach problems show up as VolumeAttachment objects and as FailedAttachVolume or FailedMount events on the Pod.

  • kubectl describe pvc NAME for ProvisioningFailed events
  • kubectl get volumeattachments
  • kubectl get pods -A | grep csi

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Create a StorageClass~40m
  2. 2
    Provision a volume dynamically~40m
  3. 3
    Set the default StorageClass~40m

Key points

  • Mark the default with annotation storageclass.kubernetes.io/is-default-class: true.
  • WaitForFirstConsumer delays provisioning until a Pod is scheduled, matching its zone.
  • Dynamically provisioned PVs inherit the class reclaimPolicy, which defaults to Delete.
  • allowVolumeExpansion lets you grow a PVC by editing its request.
  • storageClassName: "" on a PVC disables dynamic provisioning.

Key terms

Tap a card to flip it. 0/5 known

Examples

yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: rancher.io/local-path
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

Default class using the local-path provisioner.

Switching the default StorageClass

A cluster has class standard as default, and the team wants fast to be the default instead. Step 1: list the classes. Step 2: remove the default flag from standard. Step 3: add it to fast. Step 4: create a claim with no class and confirm it gets fast.

bash
kubectl get sc
kubectl patch sc standard -p \
  '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
kubectl patch sc fast -p \
  '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
kubectl get sc                 # fast (default)
kubectl apply -f pvc-no-class.yaml
kubectl get pvc -o custom-columns=NAME:.metadata.name,CLASS:.spec.storageClassName

Growing a volume that is full

A database PVC of 1Gi is full. Step 1: check that its class allows expansion. Step 2: patch the claim to request 5Gi. Step 3: watch the claim's conditions; many CSI drivers resize online, while some show FileSystemResizePending until the Pod restarts. Step 4: check the new size inside the Pod.

bash
kubectl get sc fast -o jsonpath='{.allowVolumeExpansion}'; echo   # true
kubectl patch pvc db-data -p '{"spec":{"resources":{"requests":{"storage":"5Gi"}}}}'
kubectl describe pvc db-data | grep -A3 Conditions
kubectl get pvc db-data        # CAPACITY 5Gi when done
kubectl exec db-0 -- df -h /var/lib/postgresql/data

Quick reference

  • Default class annotation: storageclass.kubernetes.io/is-default-class: "true"
  • Keep exactly one default; with several, the newest one wins
  • storageClassName: "" on a PVC turns off dynamic provisioning
  • Dynamic PVs copy reclaimPolicy from the class, default Delete
  • WaitForFirstConsumer: PVC Pending until a Pod uses it, and that is normal
  • Do not use nodeName with WaitForFirstConsumer; use a nodeSelector
  • Provisioner and parameters are immutable; recreate the class to change them

Common mistakes

  • Marking two classes as default, which makes the choice ambiguous.
  • Waiting for a WaitForFirstConsumer PVC to bind before creating the Pod.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A PVC using a WaitForFirstConsumer class has been Pending for 10 minutes and no Pod uses it. What is wrong?

Question 2

2What reclaim policy do dynamically provisioned PVs get if the class sets none?

Question 3

3Which PVC setting makes sure no class, not even the default, is used?

Question 4

4You want to change a StorageClass's parameters.type from gp2 to gp3. How?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List all StorageClasses with their provisioner and binding mode, and write the name of the default class to /opt/default-sc.txt.

  2. Exercise 2medium

    Create StorageClass slow-retain using the same provisioner as the default class, with reclaimPolicy Retain, WaitForFirstConsumer and expansion allowed. Create a 100Mi PVC named mydata from it and a Pod named user that binds it.

  3. Exercise 3hard

    Delete the PVC from the previous task and show that the PV and its data stay. Then make the PV reusable for a new claim.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is dynamic provisioning in Kubernetes?
It means storage is created automatically when a PVC is made, using the provisioner named in a StorageClass, instead of an admin creating each PV by hand.
Why do I have two default StorageClasses?
Usually a tool or add-on installed its own class and marked it default. Kubernetes then uses the newest one, so set the annotation to false on the one you do not want.
Does changing a StorageClass affect existing volumes?
No. Settings are copied when a PV is created, so existing PVs keep their original reclaim policy and parameters.

Go deeper

ShareXLinkedInWhatsApp