PersistentVolumes & PersistentVolumeClaims make it quite easy to provision persistent storage in Kubernetes.
They shield the developer from dealing with the underlying storage technology. However, a cluster admin has to provision the actual storage up front.
Kubernetes can also perform this job by means of Storage Class. By using StorageClass objects, Kubernetes can dynamically provision the PersistentVolume.
In this post, I’m going to explain how Kubernetes Storage Class objects work with an example of how you can use them.
To better understand this post, I also recommend you go through my previous post on Kubernetes Persistent Volume Claim.
1 – Understanding the Working of Kubernetes Storage Class
Here’s an illustration that describes the process flow of Storage Class.
Let’s understand what’s going on over here:
- In the first step, the cluster administrator sets up a Persistent Volume Provisioner. A typical Kubernetes installation includes provisioners for the most popular cloud providers, so the administrator doesn’t always need to deploy a provisioner.
- In the second step, the administrator defines one or more StorageClass resources. The Kubernetes StorageClass resource basically specifies which provisioner should be used for provisioning the PV when a PVC requests this storage class. The parameters defined in the StorageClass are passed to the provisioner and are specific to each provisioner plugin.
- In the third step, the developer creates a PVC resource referencing an available StorageClass resource.
- When the PVC resource is applied, the fourth step kicks in where Kubernetes checks the StorageClass and asks the corresponding provisioner to provision a PersistentVolume.
- In the fifth step, the provisioner provisions the actual storage, creates a persistent volume, and binds it to the PVC.
- Finally, in the last step, the developer creates a pod with the appropriate PVC.
2 – Kubernetes Storage Class Example
Below is an example Storage Class YAML for reference:
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: ebs-sc provisioner: kubernetes.io/aws-ebs parameters: type: gp2 zones: us-west-1a,us-west-1b
As you can see, the provisioner is set to
kubernetes.io/aws-ebs indicating that the storage class is set to AWS EBS provisioner.
parameters are specifically related to the AWS EBS provisioner such as the type of the volume and availability zones.
3 – Reference a Storage Class in a PVC
See below an example for a Kubernetes PVC where we can reference a storage class.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mongodb-pvc spec: resources: requests: storage: 512M accessModes: - ReadWriteOnce storageClassName: local-path
storageClassName here is
local-path. This is a special type of StorageClass for provisioning storage locally on the node.
Kubernetes Storage Class makes dynamic storage provisioning very easy to set up.
In this post, we understood how the entire Storage Class process works and how you can use a Storage Class within the PVC.
If you have any comments or queries, please mention them in the comments section below.