Home:ALL Converter>What happens when we create stateful set with many replicas with one pvc in kubernetes?

What happens when we create stateful set with many replicas with one pvc in kubernetes?

Ask Time:2022-05-16T01:55:35         Author:Sebapp

Json Formatter

Im new to kubernetes and this topic is confusing for me. I've learned that stateful set doesn't share the PV and each replica has it's own PV. On the other hand I saw the examples when one was using one pvc in stateful set with many replicas. So my question is what will happen then? As PVC to PV are bind 1:1 so one pvc can only bind to one pv, but each replica should have its own PV so how is it possible to have one pvc in stateful set in this scenario?

Author:Sebapp,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72250853/what-happens-when-we-create-stateful-set-with-many-replicas-with-one-pvc-in-kube
David Maze :

You should usually use a volume claim template with a StatefulSet. As you note in the question, this will create a new PersistentVolumeClaim (and a new PersistentVolume) for each replica. Data is not shared, except to the extent the container process knows how to replicate data between its replicas. If a StatefulSet Pod is deleted and recreated, it will come back with the same underlying PVC and the same data, even if it is recreated on a different Node.\nspec:\n volumeClaimTemplates:\n - metadata:\n name: data\n spec:\n accessModes: [ReadWriteOnce]\n resources:\n requests:\n storage: 1Gi\n template:\n spec:\n containers:\n - name: name\n volumeMounts:\n - name: data\n mountPath: /data\n\nYou're allowed to manually create a PVC and attach it to the StatefulSet Pods\n# not recommended -- one PVC shared across all replicas\nspec:\n template:\n spec:\n volumes:\n - name: data\n persistentVolumeClaim:\n claimName: manually-created-pvc\n containers:\n - name: name\n volumeMounts:\n - name: data\n mountPath: /data\n\nbut in this case the single PVC/PV will be shared across all of the replicas. This often doesn't work well: things like database containers have explicit checks that their storage isn't shared, and there is a range of concurrency problems that are possible doing this. This also can prevent pods from starting up since the volume types that are straightforward to get generally only support a ReadWriteOnce access mode; to get ReadWriteMany you need to additionally configure something like an NFS server outside the cluster.",
2022-05-16T11:51:47
Harsh Manvar :

i am not sure which example you were following and checked that scenario however yes PV and PVC is 1:1 mapping.\nUsually, PVC gets attached to POD with access mode ReadWriteOnly which mean only one pod can do ReadWrite.\nThe scenario that you have might be seen could be something like there is a single PVC and single PV attach to multiple replicas which could be due to ReadWriteMany.\n\nA PersistentVolumeClaim (PVC) is a request for storage by a user. It\nis similar to a Pod. Pods consume node resources and PVCs consume PV\nresources. Pods can request specific levels of resources (CPU and\nMemory). Claims can request specific size and access modes (e.g., they\ncan be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see\nAccessModes).\n\nRead more about access mode here : https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes\nNFS, EFS and other type of storage support the ReadWriteMany access mode.",
2022-05-15T19:15:33
yy