Bind different Persistent Volume for each replica in a Kubernetes Deployment Bind different Persistent Volume for each replica in a Kubernetes Deployment kubernetes kubernetes

Bind different Persistent Volume for each replica in a Kubernetes Deployment


With Deployments you cannot do this properly. You should use StatefulSet with PVC template to achieve your target. The part of your StatefulSet YAML code snippet could look like this:

...volumeClaimTemplates:- metadata:    name: pv-data  spec:    accessModes:       - ReadWriteOnce    resources:      requests:        storage: 5G

assuming you have 3 replicas, you will see the pods are created one by one sequentially, and the PVC is requested during the pod creation.

The PVC is named as volumeClaimTemplate name + pod-name + ordinal number and as result, you will have the list of newly created PVCs:

pv-data-<pod_name>-0pv-data-<pod_name>-1pv-data-<pod_name>-N

StatefulSet makes the names (not only names in fact) of your pods static and increments them depending on replica count, thats why every Pod will match its own PVC and PV respectively

Note: this is called dynamic provisioning. You should be familiar with configuring kubernetes control plane components (like controller-manager) to achieve this, because you will need configured persistent storage (one of them) providers and understand the retain policy of your data, but this is completely another question...