Lab 6.3 - StorageClasses and Dynamic Provisioning
Lab Objectives
By the end of this lab, you will be able to:
- Understand the StorageClass concept.
- Create a custom StorageClass.
- Use dynamic provisioning with a PVC.
- Understand the different provisioner types.
- Verify automatic PV provisioning.
Estimated Duration
45-60 minutes
Prerequisites
- kubectl installed and configured.
- Functional local Kubernetes cluster (minikube or kind).
- Knowledge of StorageClasses (Chapter 6.3).
Part 1: Understanding StorageClasses
A StorageClass defines a "type" of storage with characteristics (provisioner, parameters, reclaim policy). It enables dynamic provisioning of PersistentVolumes.
Step 1.1: List Existing StorageClasses
Check the StorageClasses available in your cluster:
kubectl get storageclass
# or
kubectl get sc
On minikube, you should see a default StorageClass (e.g., standard or hostpath). On kind, there may be standard or none.
Check the details of a StorageClass:
kubectl describe storageclass <storageclass-name>
Part 2: Creating a Custom StorageClass
For a local cluster, we will create a StorageClass that uses hostPath as a provisioner (via a local or manual provisioner).
Step 2.1: Create a StorageClass
Create a file storageclass-local.yaml:
# storageclass-local.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner # No automatic provisioning for hostPath
volumeBindingMode: WaitForFirstConsumer # Wait for a Pod to use the PVC before creating the PV
allowVolumeExpansion: false # Does not allow volume expansion
Explanation:
provisioner: kubernetes.io/no-provisioner: Indicates there is no automatic provisioning. For a local cluster with hostPath, we must create PVs manually, but the StorageClass allows grouping them.volumeBindingMode: WaitForFirstConsumer: The PVC waits for a Pod to be created before binding the PV. This allows the scheduler to choose the right node.allowVolumeExpansion: false: Volumes cannot be expanded after creation.
Apply the StorageClass:
kubectl apply -f storageclass-local.yaml
Verify it is created:
kubectl get storageclass local-storage
Part 3: Dynamic Provisioning (Simulation)
For true dynamic provisioning, you would need a provisioner like:
kubernetes.io/aws-ebs(AWS)kubernetes.io/azure-disk(Azure)kubernetes.io/gce-pd(GCP)kubernetes.io/cinder(OpenStack)
On minikube, you can use the k8s.io/minikube-hostpath provisioner if available.
Step 3.1: Create a StorageClass with Dynamic Provisioning (minikube)
If using minikube, create a StorageClass that uses the minikube provisioner:
# storageclass-minikube.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: minikube-dynamic
provisioner: k8s.io/minikube-hostpath # Minikube provisioner
volumeBindingMode: Immediate # Bind immediately
reclaimPolicy: Delete # Delete the PV when the PVC is deleted
allowVolumeExpansion: true # Allow expansion
Apply it:
kubectl apply -f storageclass-minikube.yaml
Step 3.2: Create a PVC with the StorageClass
Create a file pvc-dynamic.yaml:
# pvc-dynamic.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-dynamic
spec:
storageClassName: minikube-dynamic # Uses the StorageClass with dynamic provisioning
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
Apply the PVC:
kubectl apply -f pvc-dynamic.yaml
Verify the PVC is created and a PV has been automatically provisioned:
kubectl get pvc pvc-dynamic
kubectl get pv
You should see a new PV automatically created with status Bound and linked to the PVC.
Part 4: Using the Dynamic PVC
Create a Pod that uses the dynamic PVC:
# pod-dynamic-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-dynamic-pvc
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c"]
args: ["echo 'Data in a dynamic volume - $(date)' > /data/dynamic-data.txt && cat /data/dynamic-data.txt && sleep 3600"]
volumeMounts:
- name: dynamic-storage
mountPath: /data
volumes:
- name: dynamic-storage
persistentVolumeClaim:
claimName: pvc-dynamic
Apply the Pod:
kubectl apply -f pod-dynamic-pvc.yaml
Check the logs:
kubectl logs pod-dynamic-pvc
Part 5: Volume Expansion (Optional)
If allowVolumeExpansion: true, you can expand a PVC after creation.
Modify the PVC to request more space:
kubectl patch pvc pvc-dynamic -p '{"spec":{"resources":{"requests":{"storage":"200Mi"}}}}'
Verify the request was updated:
kubectl get pvc pvc-dynamic
Note: Actual volume expansion depends on the provisioner and the underlying storage type.
Part 6: Cleanup
Delete the created resources:
kubectl delete pod pod-dynamic-pvc
kubectl delete -f pvc-dynamic.yaml
# The PV will be automatically deleted if reclaimPolicy: Delete
kubectl delete -f storageclass-minikube.yaml
kubectl delete -f storageclass-local.yaml
Lab Summary
In this lab, you explored StorageClasses and dynamic provisioning. You learned to create custom StorageClasses, use automatic PV provisioning via PVCs, and understand the different binding modes and reclaim policies.
Next Steps
The last lab in this module will show you how to use PersistentVolumes with StatefulSets for stateful applications.
Lab 6.4: StatefulSet with Persistent Storage
Lab created: December 2024