Actually, It has been done by
Dynamic provisioning of PersistentVolumes.
PersistentVolumes and PersistentVolumeClaims makes it easy to obtain persistent storage without the developer having to deal with the actual storage technology used underneath. But this still requires a cluster administrator to provision the actual storage up front. You think, PersistentVolumes have to been created, but It don't has to be like that all time. Luckily, Kubernetes can also perform this job automatically through dynamic provisioning of PersistentVolumes.
The cluster admin, instead of creating PersistentVolumes, can deploy a PersistentVolume provisioner and define one or more StorageClass objects to let users choose what type of PersistentVolume they want. The users can refer to the StorageClass in their PersistentVolumeClaims and the provisioner will take that into account when provisioning the persistent storage.
The kubernetes make it get simplier by including default StoregeClass definitions. You don't have to point a StorageClass in the yaml manifest like the following:
The PVC yaml file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgresdb-pvc
spec:
resources:
requests:
storage: 1Gi
accessModes:
- ReadWriteOnce
This PVC definition includes only the storage size request and the desired accessmodes, but no storage class. When you create the PVC, whatever storage class is marked as default will be used.
$ kubectl get pvc postgresdb-pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS
postgresdb-pvc Bound pvc-95a5ec12 1Gi RWO standard
$ kubectl get pv pvc-95a5ec12
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS STORAGECLASS
pvc-95a5ec12 1Gi RWO Delete Bound standard
This image that it is from Kubernetes In Action book, summarize all steps, perfectly.
