0
0
Kubernetesdevops~5 mins

Storage classes for dynamic provisioning in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you need storage for your applications in Kubernetes, you can create it automatically using storage classes. This saves you from manually setting up storage every time. Storage classes tell Kubernetes how to create storage on demand.
When you want your app to get storage automatically without manual setup.
When you need different types of storage like fast SSD or cheaper HDD for different apps.
When you want to manage storage policies like encryption or replication easily.
When you want to avoid errors from manually creating storage volumes.
When you want to scale storage dynamically as your app grows.
Config File - storageclass.yaml
storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast-storage
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  fsType: ext4
reclaimPolicy: Delete
volumeBindingMode: Immediate

This file defines a StorageClass named fast-storage. It uses the AWS Elastic Block Store (EBS) provisioner to create gp2 type volumes formatted with ext4 filesystem. The reclaimPolicy: Delete means storage is deleted when no longer used. volumeBindingMode: Immediate means volumes are created right when requested.

Commands
This command creates the StorageClass in your Kubernetes cluster so it can be used for dynamic storage provisioning.
Terminal
kubectl apply -f storageclass.yaml
Expected OutputExpected
storageclass.storage.k8s.io/fast-storage created
This command lists all StorageClasses available in the cluster to verify that your new StorageClass was created.
Terminal
kubectl get storageclass
Expected OutputExpected
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE fast-storage kubernetes.io/aws-ebs Delete Immediate false 10s
This command creates a PersistentVolumeClaim that requests storage using the StorageClass named fast-storage. Kubernetes will dynamically create the storage volume.
Terminal
kubectl apply -f pvc.yaml
Expected OutputExpected
persistentvolumeclaim/my-pvc created
This command checks the status of the PersistentVolumeClaim to see if the storage has been successfully provisioned and bound.
Terminal
kubectl get pvc my-pvc
Expected OutputExpected
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE my-pvc Bound pvc-12345678-90ab-cdef-1234-567890abcdef 5Gi RWO fast-storage 15s
Key Concept

If you remember nothing else from this pattern, remember: StorageClasses let Kubernetes create storage automatically when your app asks for it.

Common Mistakes
Not specifying the correct provisioner in the StorageClass.
Kubernetes won't know how to create the storage and the PVC will stay pending.
Use the provisioner that matches your cloud or storage system, like kubernetes.io/aws-ebs for AWS.
Creating a PersistentVolumeClaim without specifying the StorageClass name.
Kubernetes may use the default StorageClass or fail if none is set, causing unexpected storage types or errors.
Always specify the storageClassName field in your PVC to use the desired StorageClass.
Deleting the StorageClass while PVCs are still using it.
Existing volumes may become unmanaged or cause errors when deleted unexpectedly.
Only delete StorageClasses when no PVCs are using them or after cleaning up PVCs.
Summary
Create a StorageClass YAML file to define how storage is dynamically provisioned.
Apply the StorageClass to the cluster using kubectl apply.
Create a PersistentVolumeClaim that requests storage from the StorageClass.
Check the PVC status to confirm storage was provisioned and bound.