Search code examples
kuberneteskubernetes-pvc

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


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.


Solution

  • 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.