Using GCE PD storage classes to create dynamic persistent volumes

As part of a StatefulSet, volumeClaimTemplates can provide persistent storage using PersistentVolumes provisioned by a PersistentVolume provisioner of your choice. In this recipe, we will use StorageClass to dynamically create PVs for your application:

  1. Add the gce-pd storage class line under the volumeClaimTemplates section of your application deployment manifest, similar to the following example:  
...
volumeClaimTemplates:
- metadata:
name: datadir
annotations:
volume.beta.kubernetes.io/storage-class: gce-pd
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1G
...
  1. In this recipe, we will deploy the Redis Statefulset using the gce-pd storage class. Review the YAML manifest under the src/chapter5/gcp directory in the example repository before we execute it:
$ cat gcp/redis-statefulset.yml
  1. Create the Redis StatefulSet:
$ kubectl apply -f redis-statefulset.yml
  1. Verify that pods have been created:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
rd-0 1/1 Running 0 2m27s
rd-1 1/1 Running 0 81s
rd-2 0/1 Running 0 19s
  1. List the PVCs and PVs created:
$ kubectl get pvc, pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
datadir-rd-0 Bound pvc-3481b73c-d347-11e9-b514-42010a80005e 1Gi RWO gce-pd 3m1s
datadir-rd-1 Bound pvc-5b8cc2d6-d347-11e9-b514-42010a80005e 1Gi RWO gce-pd 115s
datadir-rd-2 Bound pvc-80d826b9-d347-11e9-b514-42010a80005e 1Gi RWO gce-pd 53s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-3481b73c-d347-11e9-b514-42010a80005e 1Gi RWO Delete Bound default/datadir-rd-0 gce-pd 3m16s
pvc-5b8cc2d6-d347-11e9-b514-42010a80005e 1Gi RWO Delete Bound default/datadir-rd-1 gce-pd 2m11s
pvc-80d826b9-d347-11e9-b514-42010a80005e 1Gi RWO Delete Bound default/datadir-rd-2 gce-pd 68s

Now you know how to dynamically create GCE PD persistent volumes as part of your application deployment.