Kubernetes tip: How to use the ‘storageClass' attribute 🤔
Tags: kubernetes, showdev, productivity, beginners
Published: 2019-10-10 13:45:47

This is a short blog post that covers an aspect of Kubernetes dynamic provisioning which often trips up folks when they are first exploring it (confused me for sure!)

In order to use a `StorageClass`, all you need to do is reference it from a `PersistentVolumeClaim`. But `StorageClass` is not a mandatory field - so it is possible to not use it at all, or provide an empty string (`""`) as its value. It's important to understand these options and the impact they'll have.

Let's clarify these permutations and combinations, once and for all!


> Kubernetes `StorageClass` concepts (and more) were covered in-depth in ["The definitive guide to Kubernetes Volumes (Part 2)"](https://dev.to/azure/the-definitive-guide-to-kubernetes-volumes-part-2-31c0)

Just like a `PersistentVolume` encapsulates storage related details, a `StorageClass` provides a way to describe the "classes" of storage. Here is an example of a `StorageClass` for an [Azure Disk](https://azure.microsoft.com/services/storage/disks/?WT.mc_id=devto-blog-abhishgu).



    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      labels:
        kubernetes.io/cluster-service: "true"
      name: default
    parameters:
      cachingmode: ReadOnly
      kind: Managed
      storageaccounttype: Standard_LRS
    provisioner: kubernetes.io/azure-disk
    reclaimPolicy: Delete
    volumeBindingMode: Immediate


### When `storageClass` attribute is absent

You can choose not to include `storageClass` attribute in your `PersistentVolumeClaim` if you want to use the `default` Storage Class in your cluster. For example, [Azure Kubernetes Service](https://azure.microsoft.com/services/storage/disks/?WT.mc_id=devto-blog-abhishgu) includes two pre-seeded storage classes. 

You can check the same by running `kubectl get storageclass` command

    NAME                PROVISIONER                AGE
    default (default)   kubernetes.io/azure-disk   6d10h
    managed-premium     kubernetes.io/azure-disk   6d10h

- `default` storage class: provisions a standard [Azure Disk backed by a Standard HDD](https://docs.microsoft.com/azure/virtual-machines/windows/disks-types?WT.mc_id=devto-blog-abhishgu#standard-hdd)
- `managed-premium` storage class: provisions a premium [Azure Disk backed by Premium SSD](https://docs.microsoft.com/azure/virtual-machines/windows/disks-types?WT.mc_id=devto-blog-abhishgu#premium-ssd)


> In this case, the `default` Storage Class (and its associated `provisioner`) is used even if a `PersistentVolume` exists and it matches the requirements in the `PersistentVolumeClaim`.


### When `storageClass` attribute is set to an empty string

This is a tricky one!

![](https://media.giphy.com/media/5jSZKACQEHxwhxQTEZ/giphy-downsized.gif)

You already have a `PersistentVolume` along with the corresponding storage medium provisioned and want to use that (without referring to a custom Storage Class or the `default` one) 

In this case, just set `storageClass` to an empty string (`""`) in the `PersistentVolumeClaim`. This will suppress dynamic provisioning!

> Check out ["How to add persistent storage to your Kubernetes apps on Azure"](https://dev.to/azure/how-to-add-persistent-storage-to-your-kubernetes-apps-on-azure-2kb2) to see this in action

### When a specific `storageClass` attribute is referenced

> The ["How to use custom Storage Classes in Kubernetes"](https://dev.to/azure/how-to-use-custom-storage-classes-in-kubernetes-3jo4) blog post demonstrates this scenario with an example

In many cases, you might want to create custom Storage Classes in addition to what's already installed in your Kubernetes cluster e.g. to change the way you want to handle removal of your `PersistentVolume` and storage medium once the `PersistentVolumeClaim` is deleted.

In this case, the `provisioner` specified in your custom Storage Class will be used to create the storage medium (and the corresponding `PersistentVolume` object) to satisfy the storage request details in the `PersistentVolumeClaim`.

Note: If you reference an invalid storage class from a `PersistentVolumeClaim`, dynamic provisioning will fail



That's all for now. I would love to have your feedback and suggestions! Just [tweet](https://twitter.com/abhi_tweeter) or drop a comment. Also, don't forget to like and follow 😃😃