Dynamic storage provisioning is a key part of scaling applications. When a storage class is not specified by a PVC, Kubernetes uses the default option. Let's perform the following steps to set our preferred storage class as the default:
- Let's create a new storage class and define it as the default option at the same time by setting is-default-class to true:
$ cat <<EOF | kubectl apply -f -
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: gce-pd-ssd
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-ssd
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
allowedTopologies:
- matchLabelExpressions:
- key: failure-domain.beta.kubernetes.io/zone
values:
- us-central1-a
- us-central1-b
EOF
- Having more than one default storage class will cause a problem. You need to remove one. To change the status of an existing storage class after it has been created, first pick a storage class:
$ kubectl get sc
NAME PROVISIONER AGE
gce-pd kubernetes.io/gce-pd 3m52s
gce-pd-ssd (default) kubernetes.io/gce-pd 4s
standard (default) kubernetes.io/gce-pd 21m
- Let's set the standard and gce-pd-ssd storage classes as non-default:
$ kubectl patch storageclass standard -p '{"metadata": {"annotations":{"storageclass.beta.kubernetes.io/is-default-class":"false"}}}'
$ kubectl patch storageclass gce-pd-ssd -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
- Let's now define gce-pd as the default storage class again:
$ kubectl patch storageclass gce-pd -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
- Confirm the new default storage class:
$ kubectl get sc
NAME PROVISIONER AGE
gce-pd (default) kubernetes.io/gce-pd 8m25s
gce-pd-ssd kubernetes.io/gce-pd 4m37s
standard kubernetes.io/gce-pd 25m
Now you have learned how to replace the default storage class with a new storage class. Make sure that there is always one default storage class at a time; otherwise PVCs without a storage class defined that are expecting a default storage class will fail.