This article explores storage in StatefulSets and explains persistent storage operations in Kubernetes, including provisioning methods and configurations for Pods.
In this article, we explore storage in StatefulSets and explain how persistent storage operates in Kubernetes. We begin with a review of persistent volumes (PV) and persistent volume claims (PVC) used with Pods, then dive into different provisioning methods and their use in StatefulSets.
Dynamic provisioning simplifies the process by automatically creating PVs when you define a PVC and reference a StorageClass. This eliminates the need to manually provision PVs.
StatefulSets support scenarios where multiple replicas share the same volume. If you reference a common PVC within a StatefulSet, all replicas will attempt to access the same storage. This setup works if your underlying storage supports multi-reader or multi-writer capabilities.
Separate Volumes for Each Pod Using VolumeClaimTemplates
For scenarios like MySQL replication where each Pod requires dedicated storage, a volume claim template allows Kubernetes to automatically create a unique PVC for each Pod in a StatefulSet.
Step 2: Create a StatefulSet with a VolumeClaimTemplate
Copy
Ask AI
# statefulset-definition.yamlapiVersion: apps/v1kind: StatefulSetmetadata: name: mysql labels: app: mysqlspec: replicas: 3 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql volumeMounts: - mountPath: /var/lib/mysql name: data-volume volumeClaimTemplates: - metadata: name: data-volume spec: accessModes: - ReadWriteOnce storageClassName: google-storage resources: requests: storage: 500Mi
In this configuration, Kubernetes provisions a unique PVC for each Pod automatically based on the volume claim template. This ensures that every Pod receives its dedicated storage. Additionally, StatefulSets maintain stable storage even if a Pod is rescheduled; the associated PVC and underlying PV remain intact and are reattached to the new Pod instance.That concludes our discussion on storage in StatefulSets. For more information on Kubernetes storage concepts, visit the Kubernetes Documentation.