How to change the default StorageClass

2 min read

The default StorageClass is defined using the storageclass.kubernetes.io/is-default-class annotation as follows:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
(...)

To be able to change it we will have to removed from the current default and set it to the new one.

Let's assume the current default is gp2 and we want to set gp3 as the default instead:

$ kubectl get sc
NAME            PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
gp2 (default)   ebs.csi.aws.com         Delete          WaitForFirstConsumer   true                   2d7h
gp3             ebs.csi.aws.com         Delete          WaitForFirstConsumer   true                   2d7h

We can use kubectl patch to set the annotation on the current default to false like this:

kubectl patch storageclass gp2 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'

Then we just need to add or change the annotation on the StorageClass we want to set as default. That's something we can also achieve using kubectl patch:

kubectl patch storageclass gp3 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

If we check again the kubectl get sc we will be able to see that we have successfully changed the default StorageClass:

$ kubectl get sc
NAME            PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
gp2             ebs.csi.aws.com         Delete          WaitForFirstConsumer   true                   2d7h
gp3 (default)   ebs.csi.aws.com         Delete          WaitForFirstConsumer   true                   2d7h

Posted on 20/09/2021