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. Let's begin with the following steps:
- Add the aws-gp2 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: aws-gp2
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1G
...
- In this recipe, we will deploy the Redis StatefulSet using the aws-gp2 storage class. Review the YAML manifest under the src/chapter5/aws directory in the example repository before we execute it:
$ cat aws/redis-statefulset.yml
- Create the Redis StatefulSet using the following example:
$ kubectl apply -f aws/redis-statefulset.yml
- Verify that pods have been created. In this recipe, our example has StatefulSet with three replicas. As a result, you should see three replicas running, similar to the following output:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
rd-0 1/1 Running 0 9m9s
rd-1 1/1 Running 0 7m56s
rd-2 1/1 Running 0 6m47s
- List the PVC and PVs created. You should expect to see three PVCs and three PVs created similar to our example output here:
$ kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
datadir-rd-0 Bound pvc-8a538aa3-7382-4147-adde-1ea3dbaaafb4 1Gi RWO aws-gp2 10m
datadir-rd-1 Bound pvc-171fbee3-39bf-4450-961f-6c1417ff3897 1Gi RWO aws-gp2 9m1s
datadir-rd-2 Bound pvc-b40df89b-5349-4f02-8510-917012579746 1Gi RWO aws-gp2 7m52s$
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-171fbee3-39bf-4450-961f-6c1417ff3897 1Gi RWO Retain Bound default/datadir-rd-1 aws-gp2 9m18s
pvc-8a538aa3-7382-4147-adde-1ea3dbaaafb4 1Gi RWO Retain Bound default/datadir-rd-0 aws-gp2 10m
pvc-b40df89b-5349-4f02-8510-917012579746 1Gi RWO Retain Bound default/datadir-rd-2 aws-gp2 8m10s
Now, you know how to dynamically create persistent volumes as part of your deployment.