Senior
How is storage handled in StatefulSet?
sobes.tech AI
Answer from AI
In a StatefulSet, a separate PersistentVolumeClaim (PVC) is created for each pod, which provides persistent storage with a unique name linked to a specific pod instance. This is achieved using volumeClaimTemplates in the StatefulSet manifest.
Each pod gets its own PVC, which persists even when the pod is restarted or recreated, ensuring data durability. PVCs are created dynamically based on a template, and the PVC name includes the pod index, for example, data-mysql-0, data-mysql-1.
Thus, StatefulSet provides:
- Unique and stable storage for each pod.
- The connection between the pod and its storage remains intact during updates and restarts.
Example snippet from a StatefulSet with volumeClaimTemplates:
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
This allows each pod to have its own disk with persistent storage.