[Kubernetes] StatefulSet use

Get into the habit of writing together! This is the 17th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

1. Expansion and shrinkageStatefulSet

StatefulSetAs with Deployment, DaemonSetyou can also manually expand or shrink. Mainly by modifying the replicasfields .

Expansion StatefulSet:

# 由于实验环境中只有两个 Node,扩容操作不会成功,大家可以自行在本地尝试
kubectl scale sts web --replicas=5
statefulset.apps/web scaled
复制代码

Shrinking StatefulSet:

kubectl patch sts web -p '{"spec":{"replicas":1}}'
statefulset.apps/web patched
复制代码

2. UpdateStatefulSet

StatefulSetYou can use the field spec.updateStrategy.typeto set the update strategy, which can be used to update ,StatefulSet , , , and , etc. in the update . Currently, two update strategies are supported:Podcontainer imagesresource requestslimitslabelsannotations

  • RolingUpdate: The default strategy, after updating the StatefulSettemplate , the old one is automatically deleted Podand the new one is created Pod, and the update order is opposite to the serial number index
  • OnDelete: After updating a StatefulSettemplate , a Podnew one will only be created if the old one is manually deletedPod
  1. Use the default policy to update the image of the container, first open Poda change process
kubectl get pods -w -l app=nginx
复制代码
  1. PodThe updated image isnginx-slim:0.7
$ kubectl patch statefulset web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"registry-vpc.cn-hangzhou.aliyuncs.com/chenshi-kubernetes/nginx-slim:0.7"}]'
statefulset.apps/web patched
复制代码
  1. Look at the process Podof change
$ kubectl get pods -w -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          38s
web-1   1/1     Running   0          36s
# 先更新 web-1 Pod
web-1   1/1     Terminating   0          68s
web-1   0/1     Terminating   0          69s
web-1   0/1     Terminating   0          75s
web-1   0/1     Terminating   0          75s
web-1   0/1     Pending       0          0s
web-1   0/1     Pending       0          0s
web-1   0/1     ContainerCreating   0          0s
web-1   1/1     Running             0          2s
# 然后更新 web-0 Pod
web-0   1/1     Terminating         0          79s
web-0   0/1     Terminating         0          79s
web-0   0/1     Terminating         0          87s
web-0   0/1     Terminating         0          87s
web-0   0/1     Pending             0          0s
web-0   0/1     Pending             0          0s
web-0   0/1     ContainerCreating   0          0s
web-0   1/1     Running             0          3s
复制代码
  1. Looking at Podthe container image, you can find that it has been updated to the new image
$ kubectl get pod -l app=nginx -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}{end}'
web-0   registry-vpc.cn-hangzhou.aliyuncs.com/chenshi-kubernetes/nginx-slim:0.7
web-1   registry-vpc.cn-hangzhou.aliyuncs.com/chenshi-kubernetes/nginx-slim:0.7

$ kubectl rollout status sts/web
partitioned roll out complete: 2 new pods have been updated...
复制代码

7. DeleteStatefulSet

StatefulSetThere are two ways to delete :

  • Non-cascading delete: StatefulSetwhen , the StatefulSetcreated by Podwill not be deleted
  • Cascading deletion: StatefulSetWhen , StatefulSetboth itself and the created Podwill be deleted, and the deletion order is opposite to the ordinal index

(1) Non-cascading deletion

Use the --cascade=falseparameter to indicate non-cascading deletes:

$ kubectl delete statefulset web --cascade=false
statefulset.apps "web" deleted
# 由 StatefulSet 创建的 Pod 并没有被删除,依然是运行状态
$ kubectl get pods -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          50m
web-1   1/1     Running   0          51m
复制代码

try to delete manuallyweb-0 Pod

$ kubectl delete pod web-0
pod "web-0" deleted
# 由于删除了 web StatefulSet,所以 web-0 Pod 没有被继续创建了
$ kubectl get pods -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
web-1   1/1     Running   0          53m
复制代码

rebuildweb StatefulSet

# 由于没有删除 nginx 服务,所以重新创建的时候有报错提醒,可以忽略
$ kubectl create -f web.yaml
statefulset.apps/web created
Error from server (AlreadyExists): error when creating "web.yaml": services "nginx" already exists
复制代码

View running in the current environmentPod

# 当前环境运行的都是新创建的 Pod,原来单独剩下的 web-1 Pod 被删除了
$ kubectl get pods -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          3m7s
web-1   1/1     Running   0          2m54s
复制代码

The more detailed process is as follows:

$ kubectl get pods -w -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
# 删除 web-0
web-0   1/1     Terminating   0          52m
web-0   0/1     Terminating   0          52m
web-0   0/1     Terminating   0          52m
web-0   0/1     Terminating   0          52m
# web-1 处于运行状态
web-1   1/1     Running       0          62m
# 新创建 web StatefulSet 时,按顺序首先创建 web-0 Pod,创建成功后,由于检测到环境中有单独留下的 web-1 Pod,于是老的 web-1 Pod 被删除,然后重新创建新的 web-1 Pod
web-0   0/1     Pending       0          0s
web-0   0/1     Pending       0          0s
web-0   0/1     ContainerCreating   0          0s
web-0   1/1     Running             0          2s
web-1   1/1     Terminating         0          62m
web-1   0/1     Terminating         0          62m
web-1   0/1     Terminating         0          62m
web-1   0/1     Terminating         0          62m
web-1   0/1     Pending             0          0s
web-1   0/1     Pending             0          0s
web-1   0/1     ContainerCreating   0          0s
web-1   1/1     Running             0          1s
复制代码

Note: StatefulSetWhen , the Podassociated one will not be deleted PersistentVolumes. When StatefulSetrebuilding , the newly created Podwill mount the original one PersistentVolumes.

(2) Cascade delete

Omitting the --cascadeparameter means cascading deletes:

$ kubectl delete statefulset web
statefulset.apps "web" deleted
复制代码

View Podthe change process

# 依然是先删除 web-1 Pod,然后删除 web-0 Pod
$ kubectl get pods -w -l app=nginx
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          19m
web-1   1/1     Running   0          19m
web-1   1/1     Terminating   0          19m
web-0   1/1     Terminating   0          20m
web-0   0/1     Terminating   0          20m
web-1   0/1     Terminating   0          19m
复制代码

Manually remove the nginxservice

$ kubectl delete service nginx
service "nginx" deleted
复制代码

3. PodManagement strategy

For some distributed systems, sometimes it is not mandatory that Podthe startup must be in order, but only to use StatefulSetthe uniqueness and identity features in , the startup order can be set through the spec.podManagementPolicyfield . There are mainly two sequences:

  • OrderedReady: default option, which means to start sequentially
  • Parallel: can start/terminate all in parallel Pod, Podwhen does not need to wait for the other Podmust start/terminate first
  1. create web.yaml,podManagementPolicy: "Parallel"
---
spec:
  serviceName: "nginx"
  podManagementPolicy: "Parallel"
  replicas: 2
复制代码
  1. Monitor Podthe creation process of , and execute the creation:
$ kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created
# 可以看到同时启动了 web-0 和 web-1 Pod
$ kubectl get pods -w -l app=nginx
web-0   0/1   Pending   0     0s
web-1   0/1   Pending   0     0s
web-0   0/1   Pending   0     0s
web-1   0/1   Pending   0     0s
web-1   0/1   ContainerCreating   0     0s
web-0   0/1   ContainerCreating   0     0s
web-1   1/1   Running             0     2s
web-0   1/1   Running             0     2s
复制代码

Guess you like

Origin juejin.im/post/7087487256293802014