StorageClass
StorageClass provides a way to assign persistent volume to any persistent volume claim dynamically. Kubernetes cluster admins create certain storage classes which end users can use. These storage classes can abstract the details of storage creation and management from the end users. End users just need to specify which storage class to use in their persistent volume claim and then storage class creates persistent volume with appropriate properties and assigns to the claim.
We can define a storage class as shown below.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: hostpath
provisioner: k8s.io/minikube-hostpath
reclaimPolicy: Delete
volumeBindingMode: Immediateprovisioner is the main object which is responsible for creating the persistent volume. There are different types of provisioners available for different type of volumes such as nfs, local, hostpath etc. Kubernetes comes with some provisioners as default which are called as internal provisioners. Most of the time we use external provisioners from vendors like aws, azure etc for network storage.
volumeBindingMode: Immediate indicates that as soon as a pvc is created using this storage classes, a persistent volume will be created and get bounded to it. There are other volumeBindingMode such as WaitForFirstConsumer that says that the persistent volume will not be bounded until a pod is not start using the persistent volume claim.
Using StorageClass
A storage class can be used by setting storageClassName field in pvc definition file as shown below.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-with-storageclass
namespace: learning
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
storageClassName: hostpathDefault Storage class
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
annotations:
storageclass.kubernetes.io/is-default-class: "true"
name: standard
provisioner: k8s.io/minikube-hostpath
reclaimPolicy: Delete
volumeBindingMode: ImmediateAnnotation storageclass.kubernetes.io/is-default-class: "true" is responsible for setting it as default storage class. This enables any pvc with no storage class specified can use this default storage class.