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
StatefulSet
As with Deployment
, DaemonSet
you can also manually expand or shrink. Mainly by modifying the replicas
fields .
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
StatefulSet
You can use the field spec.updateStrategy.type
to set the update strategy, which can be used to update ,StatefulSet
, , , and , etc. in the update . Currently, two update strategies are supported:Pod
container images
resource requests
limits
labels
annotations
RolingUpdate
: The default strategy, after updating theStatefulSet
template , the old one is automatically deletedPod
and the new one is createdPod
, and the update order is opposite to the serial number indexOnDelete
: After updating aStatefulSet
template , aPod
new one will only be created if the old one is manually deletedPod
- Use the default policy to update the image of the container, first open
Pod
a change process
kubectl get pods -w -l app=nginx
复制代码
Pod
The 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
复制代码
- Look at the process
Pod
of 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
复制代码
- Looking at
Pod
the 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
StatefulSet
There are two ways to delete :
- Non-cascading delete:
StatefulSet
when , theStatefulSet
created byPod
will not be deleted - Cascading deletion:
StatefulSet
When ,StatefulSet
both itself and the createdPod
will be deleted, and the deletion order is opposite to the ordinal index
(1) Non-cascading deletion
Use the --cascade=false
parameter 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:
StatefulSet
When , thePod
associated one will not be deletedPersistentVolumes
. WhenStatefulSet
rebuilding , the newly createdPod
will mount the original onePersistentVolumes
.
(2) Cascade delete
Omitting the --cascade
parameter means cascading deletes:
$ kubectl delete statefulset web
statefulset.apps "web" deleted
复制代码
View Pod
the 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 nginx
service
$ kubectl delete service nginx
service "nginx" deleted
复制代码
3. Pod
Management strategy
For some distributed systems, sometimes it is not mandatory that Pod
the startup must be in order, but only to use StatefulSet
the uniqueness and identity features in , the startup order can be set through the spec.podManagementPolicy
field . There are mainly two sequences:
OrderedReady
: default option, which means to start sequentiallyParallel
: can start/terminate all in parallelPod
,Pod
when does not need to wait for the otherPod
must start/terminate first
- create
web.yaml
,podManagementPolicy: "Parallel"
---
spec:
serviceName: "nginx"
podManagementPolicy: "Parallel"
replicas: 2
复制代码
- Monitor
Pod
the 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
复制代码