We now need to update our stateful-set.yaml file to use the do-block-storage StorageClass. Under the StatefulSet spec (.spec), add a new field called volumeClaimTemplates with the following value:
apiVersion: apps/v1
kind: StatefulSet
metadata: ...
spec:
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
storageClassName: do-block-storage
This will use the do-block-storage class to dynamically provision 2 GB PersistentVolumeClaim Objects for any containers which mount it. The PVC is given the name data as a reference.
To mount it to a container, add a volumeMounts property under the spec property of the container spec:
apiVersion: apps/v1
kind: StatefulSet
metadata: ...
spec:
...
template:
...
spec:
initContainers: ...
containers: ...
volumeMounts:
- name: data
mountPath: /usr/share/elasticsearch/data
volumeClaimTemplates: ...
Elasticsearch writes its data to /usr/share/elasticsearch/data, so that's the data we want to persist.