StatefulSets
TOC
Understanding StatefulSets
Refer to the official Kubernetes documentation: StatefulSets
StatefulSet is a Kubernetes workload API object designed to manage stateful applications by providing:
- Stable network identity: DNS hostname
<statefulset-name>-<ordinal>.<service-name>.ns.svc.cluster.local
.
- Stable persistent storage: via
volumeClaimTemplates
.
- Ordered deployment/scaling: sequential Pod creation/deletion: Pod-0 → Pod-1 → Pod-N.
- Ordered rolling updates: reverse-ordinal Pod updates: Pod-N → Pod-0.
In distributed systems, multiple StatefulSets can be deployed as discrete components to deliver specialized stateful services (e.g., Kafka brokers, MongoDB shards).
Creating StatefulSets
Creating a StatefulSet by using CLI
Prerequisites
- Ensure you have
kubectl
configured and connected to your cluster.
YAML file example
# example-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx" # this headless Service is responsible for the network identity of the Pods
replicas: 3 # defines the desired number of Pod replicas (default: 1)
minReadySeconds: 10 # by default is 0
template: # defines the Pod template for the StatefulSet
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: registry.k8s.io/nginx-slim:0.24
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates: # defines PersistentVolumeClaim (PVC) templates. Each Pod gets a unique PersistentVolume (PV) dynamically provisioned based on these templates.
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
---
# example-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
Creating a StatefulSet via YAML
# Step 1: To create the StatefulSet defined in *example-statefulset.yaml*, execute the following command
kubectl apply -f example-statefulset.yaml
# Step 2: To verify the creation and status of your StatefulSet and its associated Pods and PVCs:
kubectl get statefulset web # View StatefulSet
kubectl get pods -l app=nginx # Check Pods managed by this StatefulSet
kubectl get pvc -l app=nginx # Check PVCs created by volumeClaimTemplates
Creating a StatefulSet by using web console
Prerequisites
Obtain the image address. The source of the images can be from the image repository integrated by the platform administrator through the toolchain or from third-party platforms' image repositories.
-
For the former, the Administrator typically assigns the image repository to your project, and you can use the images within it. If the required image repository is not found, please contact the Administrator for allocation.
-
If it is a third-party platform's image repository, ensure that images can be pulled directly from it in the current cluster.
Procedure - Configure Basic Info
-
Container Platform, navigate to Workloads > StatefulSets in the left sidebar.
-
Click Create StatefulSet.
-
Select or Input an image, and click Confirm.
INFO
Note: When using images from the image repository integrated into web console, you can filter images by Already Integrated. The Integration Project Name, for example, images (docker-registry-projectname), which includes the project name projectname in this web console and the project name containers in the image repository.
In the Basic Info section, configure declarative parameters for StatefulSet workloads:
Parameters | Description |
---|
Replicas | Defines the desired number of Pod replicas in the StatefulSet (default: 1). Adjust based on workload requirements and expected request volume. |
Update Strategy | Controls phased updates during StatefulSet rolling updates. The RollingUpdate strategy is default and recommended. Partition value: Ordinal threshold for Pod updates. - Pods with index ≥
partition update immediately. - Pods with index <
partition retain previous spec. Example:-
Replicas=5 (Pods: web-0 ~ web-4) -
Partition=3 (Updates web-3 & web-4 only)
|
Volume Claim Templates | volumeClaimTemplates is a critical feature of StatefulSets that enables dynamic per-Pod persistent storage provisioning. Each Pod replica in a StatefulSet automatically gets its own dedicated PersistentVolumeClaim (PVC) based on predefined templates. - 1. Dynamic PVC Creation: Automatically creates unique PVCs for each Pod with a naming pattern:
<statefulset-name>-<claim-template-name>-<pod-ordinal> . Example: web-www-web-0 , web-www-web-1 . - 2. Access Modes: Supports all Kubernetes access modes.
- ReadWriteOnce (RWO - single-node read/write)
- ReadOnlyMany (ROX - multi-node read-only)
- ReadWriteMany (RWX - multi-node read/write).
- 3. Storage Class: Specify the storage backend via storageClassName. It uses the cluster's default StorageClass if unspecified. Supports various cloud/on-prem storage types (e.g., SSD, HDD).
- 4. Capacity: Configure storage capacity through resources.requests.storage. Example: 1Gi. Supports dynamic volume expansion if enabled by the StorageClass.
|
Procedure - Configure Pod
Pod section, please refer to Deployment - Configure Pod
Procedure - Configure Containers
Containers section, please refer to Deployment - Configure Containers
Procedure - Create
Click Create.
Heath Checks
Managing StatefulSets
Managing a StatefulSet by using CLI
Viewing a StatefulSet
You can view a StatefulSet to get information of your application.
- Check the StatefulSet was created.
- Get details of your StatefulSet.
kubectl describe statefulsets
Scaling a StatefulSet
- To change the number of replicas for an existing StatefulSet:
kubectl scale statefulset <statefulset-name> --replicas=<new-replica-count>
kubectl scale statefulset web --replicas=5
Updating a StatefulSet (Rolling Update)
When you modify the Pod template of a StatefulSet (e.g., changing the container image), Kubernetes performs a rolling update by default (if updateStrategy is set to RollingUpdate, which is the default).
- First, edit the YAML file (e.g., example-statefulset.yaml) with the desired changes, then apply it:
kubectl apply -f example-statefulset.yaml
- Then, you can monitor the progress of the rolling update:
kubectl rollout status statefulset/<statefulset-name>
Deleting a StatefulSet
To delete a StatefulSet and its associated Pods:
kubectl delete statefulset <statefulset-name>
By default, deleting a StatefulSet does not delete its associated PersistentVolumeClaims (PVCs) or PersistentVolumes (PVs) to prevent data loss. To also delete the PVCs, you must do so explicitly:
kubectl delete pvc -l app=<label-selector-for-your-statefulset> # Example: kubectl delete pvc -l app=nginx
Alternatively, if your volumeClaimTemplates
use a StorageClass
with a reclaimPolicy
of Delete
, the PVs and underlying storage will be deleted automatically when the PVCs are deleted.
Managing a StatefulSet by using web console
Viewing a StatefulSet
- Container Platform, and navigate to Workloads > StatefulSets.
- Locate the StatefulSet you wish to view.
- Click the statefulSet name to see the Details, Topology, Logs, Events, Monitoring, etc.
Updating a StatefulSet
- Container Platform, and navigate to Workloads > StatefulSets.
- Locate the StatefulSet you wish to update.
- In the Actions drop-down menu, select Update to view the Edit StatefulSet page, you can update
Replicas
, image
, updateStrategy
, etc.
Deleting a StatefulSet
- Container Platform, and navigate to Workloads > StatefulSets.
- Locate the StatefulSet you wish to delete.
- In the Actions drop-down menu, Click the Delete button in the operations column and confirm.