Skip to main content
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.

Persistent Volumes and Static Provisioning

Static provisioning involves a three-step process:
  1. Create a PersistentVolume.
  2. Create a PersistentVolumeClaim.
  3. Reference the PVC in your Pod specification.
Below is an example of each configuration:
For static provisioning, ensure that the PV capacity and access modes match the requirements of your applications.

PersistentVolume Definition

PersistentVolumeClaim Definition

Pod Definition Referencing the PVC


Dynamic Provisioning with StorageClasses

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.

StorageClass Definition

PVC Definition Using Dynamic Provisioning

Pod Definition Referencing the Dynamically Provisioned PVC


Using StatefulSets with Shared Storage

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.

Shared Storage StatefulSet Example

Ensure that your storage solution supports concurrent access if you plan to share the same volume across multiple Pods.

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 1: Define the StorageClass

Step 2: Create a StatefulSet with a VolumeClaimTemplate

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.

Watch Video