StatefulSets are used to manage stateful applications. It creates a standard persistent identifier for each Pod. The identifier is retained after the Pod is migrated, terminated, or restarted. When using persistent storage, you can map storage volumes to identifiers. If your application does not require any persistent identifier, we recommend that you use a Deployment to deploy the application.
latest
or empty, Always
is used. Otherwise, IfNotPresent
is used.On the cluster management page, click the ID of the StatefulSet cluster for which the Pod configurations need to be updated to enter the StatefulSet cluster management page.
In the StatefulSet row for which Pod configurations need to be updated, click Update Pod Configurations.
On the “Updating Pod Configurations” page, modify the update method and set parameters as needed.
Click Finish to update the Pod configurations.
apiVersion: v1
kind: Service ## Create a Headless Service to control the network domain
metadata:
name: nginx
namespace: default
labels:
app: nginx
"spec":
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet ### Create a Nginx StatefulSet
metadata:
name: web
namespace: default
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3 # The default is 1.
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "cbs"
resources:
requests:
storage: 10Gi
For more details, refer to Kubernetes official documentation.
kubectl create -f StatefulSet YAML filename
For example, to create a StatefulSet YAML file named web.yaml, run the following command:kubectl create -f web.yaml
kubectl get StatefulSet
A message similar to the one below indicates that the file has been created:NAME DESIRED CURRENT AGE
test 1 1 10s
Run the following command to view the update policy type of the StatefulSet.
kubectl get ds/<daemonset-name> -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}'
StatefulSet has the following update policy types:
– OnDelete: the default upgrade policy. You have to manually delete the old StatefulSet Pod to create a new one after the StatefulSet is updated.
Run the following command to update a StatefulSet.
kubectl edit StatefulSet/[name]
Use for debugging and verification. We do not recommend using this in production environments. You can update any StatefulSet parameters this way.
Run the following command to update the image of a specific container.
kubectl patch statefulset <NAME> --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"<newImage>"}]'
We recommend that you only update the container image and keep other parameters unchanged when the service is updated.
If the StatefulSet is updated using a rolling update, you can check the update status by running the following command:
kubectl rollout status sts/<StatefulSet-name>
Run the following command to delete a StatefulSet.
kubectl delete StatefulSet [NAME] --cascade=false
--cascade=false indicates that only the StatefulSet is deleted, not the Pods. Run the following command if you need to delete the Pods as well.
kubectl delete StatefulSet [NAME]
For more information about StatefulSet, refer to Kubernetes official documentation.
Was this page helpful?