Dynamic storage provisioning is a key part of scaling applications. When a storage class is not specified by a Persistent Volume Claim (PVC), Kubernetes uses the default option. Let's perform the following steps to set our preferred storage class as the default:
- Create a new storage class and define it as the default at the same time by setting the is-default-class value to true. Our example here uses the io1 volume type and limits iopsPerGB to 10. It also sets reclaimPolicy to Retain, meaning that, if the user deletes the related PVC, the volume will be retained (the other two retain policy options are Recycle and Delete):
$ cat <<EOF | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: aws-io1-slow
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: kubernetes.io/aws-ebs
parameters:
type: io1
iopsPerGB: "10"
fsType: ext4
reclaimPolicy: Retain
allowVolumeExpansion: true
EOF
- 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
aws-gp2 kubernetes.io/aws-ebs 6m28s
aws-io1-slow (default) kubernetes.io/aws-ebs 4m29s
- Let's set the existing storage class, aws-io1-slow, as the non-default option:
$ kubectl patch storageclass aws-io1-slow -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
- Now, define aws-gp2 as the default storage class again:
$ kubectl patch storageclass aws-gp2 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
- Confirm the new default storage class:
$ kubectl get sc
NAME PROVISIONER AGE
aws-gp2 (default) kubernetes.io/aws-ebs 10m
aws-io1-slow kubernetes.io/aws-ebs 8m
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.