Chapter 32 of 47 · Storage, part 3

StorageClasses and dynamic provisioning

Creating PVs by hand does not scale: someone has to guess sizes in advance and keep a pile of disks ready. With a StorageClass, you just create a PVC, and Kubernetes asks your cloud or storage system to make a brand-new disk of exactly that size, on the spot. This is dynamic provisioning, and it is what almost every real cluster uses.

Static vs dynamic

Static (chapter 31)

Admin creates PVs first → your PVC binds to one of them. Sizes are whatever the admin guessed. Nothing fits? Pending.

Dynamic (this chapter)

Your PVC names a StorageClass → a disk of exactly the requested size is created for you → a PV is created and bound automatically. No admin in the loop.

What happens on apply

  1. apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: posts-data
    spec:
      storageClassName: fast-ssd
      accessModes: [ReadWriteOnce]
      resources:
        requests:
          storage: 20Gi

    You only write the PVC, naming a StorageClass.

  2. $ kubectl get pvc
    NAME         STATUS    STORAGECLASS
    posts-data   Pending   fast-ssd     ← waiting for a Pod (WaitForFirstConsumer)

    Many classes wait until a Pod actually uses the claim, so the disk is created in the same zone as the node the Pod is scheduled on.

  3. Pod posts-0 scheduled to node-2 (zone eu-west-1b)
    provisioner ebs.csi.aws.com: create 20Gi gp3 disk in eu-west-1b
      → vol-0a1b2c3d created

    The class's provisioner, a CSI driver running in your cluster, calls the cloud API and creates a real disk.

  4. $ kubectl get pv
    NAME                                       CAPACITY   STATUS   CLAIM
    pvc-6f1c2e9a-4b7d-4c1e-9a2f-0d3e5b8c7a11   20Gi       Bound    default/posts-data

    A PV is created automatically (named after the claim's ID) and bound. The disk is attached to node-2 and mounted into the Pod.

A StorageClass

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-ssd
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  encrypted: "true"
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer

provisioner

Which driver creates the disks. Examples: ebs.csi.aws.com (AWS), pd.csi.storage.gke.io (Google), disk.csi.azure.com (Azure), rancher.io/local-path (k3s, kind), k8s.io/minikube-hostpath (minikube).

The default class

$ kubectl get storageclass
NAME                 PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION
standard (default)   k8s.io/minikube-hostpath   Delete          Immediate              false
fast-ssd             ebs.csi.aws.com            Delete          WaitForFirstConsumer   true
In your PVCResult
No storageClassName at allThe default class is used. This is why a plain PVC "just works" on minikube and cloud clusters.
storageClassName: fast-ssdThat class.
storageClassName: ""No dynamic provisioning: bind only to a hand-made PV (static).

The default is marked by the annotation storageclass.kubernetes.io/is-default-class: "true".

Reclaim policies

You delete the PVC. What happens to the disk and the data?

PVC deleted → PV deleted → cloud disk deleted. Data gone.

The default for dynamic provisioning. Tidy, no forgotten disks costing money, but one kubectl delete destroys the data. Fine for caches and test environments.

Growing a volume

The database is running out of space. If the class has allowVolumeExpansion: true, just ask for more:

  1. $ kubectl patch pvc posts-data -p '{"spec":{"resources":{"requests":{"storage":"50Gi"}}}}'
    persistentvolumeclaim/posts-data patched

    Or edit storage: 50Gi in your YAML and apply.

  2. $ kubectl describe pvc posts-data
    Conditions:
      Type                      Status
      FileSystemResizePending   True

    The provisioner grows the cloud disk first. Then the file system on it must grow too.

  3. $ kubectl get pvc posts-data
    NAME         STATUS   CAPACITY
    posts-data   Bound    50Gi

    Most CSI drivers resize the file system while the Pod runs. Some older ones need the Pod restarted.

Volumes can only grow, never shrink. To get a smaller disk you must create a new PVC and copy the data over.

Recap

Quick quiz

StorageClasses: common questions

What is the difference between Retain and Delete reclaim policies?

With Delete, deleting the PVC also deletes the PV and the disk. With Retain, the PV is kept as Released with its data, and an admin cleans it up.