Kubernetes multiple pvc with statefulset for each pod vs single pvc for all pods?

12/26/2018

I have deploy kubernetes cluster with stateful pod for mysql. for each pod i have different pvc.

for example : if 3 pod thn 3 5GB EBS PVC

SO Which way is better using one PVC for all pods or use different pvc for each pod.

-- Harsh Manvar
kubernetes
kubernetes-pvc

2 Answers

12/26/2018

StatefulSet must use volumeClaimTemplates if you want to have dedicated storage for each pod of a set. Based on that template PersistentVolumeClaim for each pod is created and configured the volume to be bound to that claim. The generated PersistentVolumeClaims names consists of volumeClaimTemplate name + pod-name + ordinal number. So if you add volumeClaimTemplate part to your StatefulSet YAML(and delete specific persistentVolumeClaim references), smth like that:

volumeClaimTemplates:
  - metadata:
      name: mysql-data    
    spec:
      resources:
        requests:
          storage: 10Gi
      accessModes:
      - ReadWriteOnce

Then go and create your StatefulSet and after to examine one of its pods (kubectl get pods pod-name-0 yaml) you’ll see smth like that(volumes part of the output):

volumes:
- name: mysql-data
  persistentVolumeClaim:
    claimName: mysql-data-pod-name-0.  | dynamically created claim based on the template 

So by using volumeClaimTemplates you don’t need to create a separate PVCs yourself and then in each separate StatefulSet reference that PVC to be mounted in your container at a specific mountPath(remember that each pod of a set must reference a different PVC, 1PVC-1PV) : Part of “containers” definition of your Statefulset YAML:

volumeMounts:
        - name: mysql-data   || references your PVC by -name(not PVC name itself) 
          mountPath: /var/lib/mysql

So to aim for each pod of the set to have dedicated storage and not use volumeClaimTemplates is leading to a lot of problems and over complications to manage and scale it.

-- Alexz
Source: StackOverflow

12/26/2018

A PVC gets bound to a specific PV. For a StatefulSet, in most imaginable cases, you want to have a PV that can be accessed only by certain pod, so that data is not corrupted by write attempt from a parallel process/pod (RWO rather then RWX mode).

With that in mind, you need a PVC per replica in StatefulSet. Creating PVCs for replicas would get problematic very quickly if done manualy, this is why the right way to do it is to use volumeClaimTemplates that will dynamicaly create PVCs for you as you scale your set.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow