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
kubectlconfigured and connected to your cluster.
YAML file example
Creating a StatefulSet via YAML
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.
-
If the image registry requires authentication, you need to configure the corresponding image pull secret. For more information, see Add ImagePullSecrets to ServiceAccount.
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.
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 (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:
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.
Scaling a StatefulSet
-
To change the number of replicas for an existing StatefulSet:
-
Example:
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:
-
Then, you can monitor the progress of the rolling update:
Deleting a StatefulSet
To delete a StatefulSet and its associated Pods:
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:
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.