Storage Classes
A StorageClass enables dynamic provisioning of PersistentVolumes. Instead of an administrator manually creating PVs, the StorageClass automatically provisions storage when a PVC is created.
Defining a StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-ssd
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp3
fsType: ext4
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
kubectl apply -f storageclass.yaml
kubectl get storageclass
Default Storage Class
A cluster can have one default StorageClass. PVCs that do not specify a storageClassName automatically use the default class.
# View storage classes and identify the default
kubectl get sc
# The default class is marked with "(default)" in the output
Using a StorageClass in a PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dynamic-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: fast-ssd
When this PVC is created, the fast-ssd StorageClass provisions a new PV automatically.
Common Provisioners
Different environments use different provisioners:
# List all storage classes with their provisioners
kubectl get sc -o custom-columns=NAME:.metadata.name,PROVISIONER:.provisioner
Popular provisioners include kubernetes.io/aws-ebs for AWS, kubernetes.io/gce-pd for GCP, kubernetes.io/azure-disk for Azure, and rancher.io/local-path for local development clusters.
Volume Binding Modes
- Immediate -- provisions storage as soon as the PVC is created
- WaitForFirstConsumer -- delays provisioning until a pod using the PVC is scheduled, ensuring the volume is created in the correct availability zone