3

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.

Alexz
  • 741
  • 1
  • 8
  • 20
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102

2 Answers2

7

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
  • 741
  • 1
  • 8
  • 20
  • Thankyou so much for reply it clear my doubt about which one will be better to use. pods having PVC or only one single PVC between pods. – Harsh Manvar Dec 26 '18 at 12:45
  • may i use single shared volume for statefulset for my mysql replication cluster ? – Harsh Manvar Dec 27 '18 at 06:17
  • 1
    PersistentVolumes support the following different access mode, like accessModes: - ReadWriteOnce - ReadOnlyMany, etc(number of nodes not pods). Storage providers(EBS, GCE) will have different capabilities and each PV’s access modes are set to the specific modes supported by that particular volume. In your case with dynamic provisioning, actual storage is dynamically provisioned, PV created and bound to PVC(at least how it should be). – Alexz Dec 27 '18 at 11:12
  • Thankyou so much for reply ..! it will help a lot – Harsh Manvar Dec 27 '18 at 12:42
3

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
  • 21,554
  • 7
  • 52
  • 48
  • Thankyou so much for your answer it helps a lot to clearify the doubt. i am new to docker kubernetes. – Harsh Manvar Dec 26 '18 at 12:46
  • I know this is little old, but want to clarify one doubt. If I have two replica of a pod and have defined pvc under pod template section. Also, I have defined one hostPath volume. So, would there be more than one volume created here (corresponding to each pvc) on each node?. Otherwise, how can hostPath pv be shared among multiple pods. – NumeroUno Jul 23 '19 at 20:04