Does the storage class dynamically provision persistent volume per pod?

4/12/2020

Kubernetes newbie here, so my question might not make sense. Please bear with me.

So my question is, given I have setup Storage Class in my cluster, then I have a PVC (Which uses that Storage Class). If I use that PVC into my Deployment, and that Deployment have 5 replicas, will the Storage Class create 5 PV? one per Pod? Or only 1 PV shared by all Pods under that Deployment?

Edit: Also I have 3 Nodes in this cluster

Thanks in advance.

-- Jplus2
kubernetes

2 Answers

4/12/2020

To answer my question directly.

The Storage Class in this case will only provision one PV and is shared across all pods under the Deployment which uses that PVC.

The accessModes of the PVC does not dictate whether to create one PV for each pod. You can set the accessModes to either ReadWriteOnce/ReadOnlyMany/ReadWriteMany and it will always create 1 PV.

If you want that each Pod will have its own PV, you can not do that under a Deployment

You will need to use StatefulSet using volumeClaimTemplates.

It is Important that the StatefulSet uses volumeClaimTemplates or else, it will still act the same as the Deployment, that is the Storage Class will just provision one PV that is shared across all pods under that StatefulSet.

References:

Kubernetes Deployments vs StatefulSets

Is there a way to create a persistent volume per pod in a kubernetes deployment (or statefulset)?

-- Jplus2
Source: StackOverflow

4/12/2020

The Persistent Volume Claim resource is specified separately from a deployment. It doesn't matter how many replicas the deployment has, kubernetes will only have the number of PVC resources that you define.

If you are looking for multiple stateful containers that create their own PVC's, use a StatefulSet instead. This includes a VolumeClaimTemplate definition.

If you want all deployment replicas to share a PVC, the storage class provider plugin will need to be either ReadOnlyMany or ReadWriteMany

-- Matt
Source: StackOverflow