Does the Storage class need to be created in Kubernetes before referring them in PV/PVC?

9/23/2021

I have a PV alpha-pv in the kubernetes cluster and have created a PVC matching the PV specs. The PV uses the Storage Class: slow. However, when I check the existence of Storage Class in Cluster there is no Storage Class existing and still my PVC was Bound to the PV.

How is this Possible when the Storage Class referred in the PV/PVC does not exists in the cluster?

If I don't mention the Storage Class in PVC, I get error message stating Storage Class Set. There is already an existing PV in the cluster which has RWO access modes, 1Gi storage size and with the Storage class named slow. But on checking the Storage Class details, there is no Storage Class resource in cluster.

If I add the Storage Class name slow in my PVC mysql-alpha-pvc, then the PVC binds to the PV. But I'm not clear how this happens when the Storage Class referred in PV/PVC named slow doesn't exist in the cluster.

Storage Class Error in PVC

PVC Bound to PV even when the Storage class named "slow" does not exists in Cluster

-- Mayur Kadam
kubernetes
kubernetes-pvc
persistent-volume-claims
persistent-volumes

2 Answers

1/25/2022

Hello I already faced the same challenge but solved, Please Make sure :

  1. Your PVC configuration ( RW mode, Size, Name) is matching what is in the PV configuration

  2. Claim name in your Deployment is equal to your PVC

  3. Scale your deployment to (0) then to (1) you will find that it is working smoothly

  4. if you are facing any challenges you could run ( kubectl get events ) to know what is the blocker.

-- Abdelrahman Essam
Source: StackOverflow

9/29/2021

Short answer

It depends.

Theory

One of the main purpose of using a storageClass is dynamic provisioning. That means that persistent volumes will be automatically provisioned once persistent volume claim requests for the storage: immediately or after pod using this PVC is created. See Volume binding mode.

Also:

A StorageClass provides a way for administrators to describe the "classes" of storage they offer. Different classes might map to quality-of-service levels, or to backup policies, or to arbitrary policies determined by the cluster administrators. Kubernetes itself is unopinionated about what classes represent. This concept is sometimes called "profiles" in other storage systems.

Reference.

How it works

If for instance kubernetes is used in cloud (Google GKE, Azure AKS or AWS EKS), they have already had predefined storageClasses, for example this is from Google GKE:

$ kubectl get storageclasses
NAME                 PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
premium-rwo          pd.csi.storage.gke.io   Delete          WaitForFirstConsumer   true                   27d
standard (default)   kubernetes.io/gce-pd    Delete          Immediate              true                   27d
standard-rwo         pd.csi.storage.gke.io   Delete          WaitForFirstConsumer   true                   27d

So you can create PVC's and refer to storageClass, PV will be created for you.


Another scenario which you faced is you can create PVC and PV with any custom storageClassName only for binding purposes. Usually it's used for testing something locally. This is also called static provisioning.

In this case you can create "fake" storage class which won't exist in kubernetes server.

Please see an example with such type of binding:

It defines the StorageClass name manual for the PersistentVolume, which will be used to bind PersistentVolumeClaim requests to this PersistentVolume.

Useful links:

-- moonkotte
Source: StackOverflow