PVC a degree of flexibility for persistent volume management. However, pre-allocating some persistent volume pools might not be cost-efficient, especially in a public cloud.
Kubernetes also assists in this kind of situation by supporting dynamic provisioning for persistent volumes. The Kubernetes administrator defines the provisioner of the persistent volume, which is called StorageClass. Then, the PVC asks StorageClass to dynamically allocate a persistent volume, and then associates it with the PVC as follows:
In the following example, AWS EBS is used as the StorageClass. When creating the PVC, the StorageClass dynamically creates an EBS then registers it as Persistent Volume (PV), and then attaches it to the PVC:
$ cat storageclass-aws.yml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: aws-sc
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
$ kubectl create -f storageclass-aws.yml
storageclass "aws-sc" created
$ kubectl get storageclass
NAME. TYPE
aws-sc. kubernetes.io/aws-ebs
Once StorageClass has been successfully created, then, create a PVC without PV, but specify the StorageClass name. In this example, this would be aws-sc, as follows:
$ cat pvc-aws.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-aws-1
spec:
storageClassName: "aws-sc"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
$ kubectl create -f pvc-aws.yml
persistentvolumeclaim/pvc-aws-1 created
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-03557eb8-bc8b-11e8-994f-42010a800085 10Gi RWO Delete Bound default/pvc-aws-1 aws-sc 1s
The following screenshot shows the EBS after submitting to StorageClass to create a PVC. AWS console shows a new EBS which is created by StorageClass:
Note that managed Kubernetes services such as Amazon EKS (https://aws.amazon.com/eks/), Google Kubernetes Engine (https://cloud.google.com/container-engine/), and Azure Kubernetes Service (https://azure.microsoft.com/en-us/services/kubernetes-service/) create StorageClass by default. For example, Google Kubernetes Engine sets up a default storage class as a Google Cloud persistent disk. For more information, please refer to Chapter 10, Kubernetes on AWS, Chapter 11, Kubernetes on GCP, and Chapter 12, Kubernetes on Azure:
//default Storage Class on GKE $ kubectl get sc NAME TYPE standard (default) kubernetes.io/gce-pd