Kubernetes全栈架构师(资源调度下)--学习笔记
⽬录
StatefulSet扩容缩容
油饼的做法StatefulSet更新策略
StatefulSet灰度发布
StatefulSet级联删除和⾮级联删除
守护进程服务DaemonSet
DaemonSet的使⽤
DaemonSet的更新和回滚
Label&Selector
什么是HPA?
小扇轻摇的时光⾃动扩缩容HPA实践
StatefulSet扩容缩容
查看nginx副本
1[root@k8s-master01 ~]# kubectl get po
2NAME READY STATUS RESTARTS AGE
怎样哄女生开心3web-0 1/1 Running 1 (7h1m ago) 22h
4web-1 1/1 Running 1 (7h1m ago) 22h
5web-2 1/1 Running 1 (7h1m ago) 22h
StatefulSet副本启动顺序按照名称0,1,2,只有web-0完全启动之后才会启动web-1,web-1完全启动之后才会启动web-2
删除的时候顺序与启动相反,从最后⼀个序号开始,2,1,0,如果web-2删除过程中,web-0挂掉了,那么web-1不会被删除,必须等待web-0启动状态变为ready之后,才会删除web-1
打开另⼀个窗⼝监控StatefulSet华硕笔记本电池
1[root@k8s-master01 ~]# kubectl get po -l app=nginx -w
2NAME READY STATUS RESTARTS AGE
3web-0 1/1 Running 1 (7h14m ago) 22h
4web-1 1/1 Running 1 (7h14m ago) 22h
5web-2 1/1 Running 1 (7h14m ago) 22h
扩容到5个副本
1[root@k8s-master01 ~]# kubectl scale --replicas=5 sts web
2statefult.apps/web scaled
监控情况(可以看到按顺序启动)
1[root@k8s-master01 ~]# kubectl get po -l app=nginx -w
2NAME READY STATUS RESTARTS AGE
3web-3 0/1 Pending 0 0s
4web-3 0/1 Pending 0 0s
5web-3 0/1 ContainerCreating 0 0s
6web-3 1/1 Running 0 1s
7web-4 0/1 Pending 0 0s
8web-4 0/1 Pending 0 0s
9web-4 0/1 ContainerCreating 0 0s
10web-4 1/1 Running 0 1s
缩容到2个副本
1[root@k8s-master01 ~]# kubectl scale --replicas=2 sts web
2statefult.apps/web scaled
监控情况(可以看到删除的顺序与启动的顺序相反)
1web-4 1/1 Terminating 0 14m
2web-4 0/1 Terminating 0 14m
3web-4 0/1 Terminating 0 14m
4web-4 0/1 Terminating 0 14m
5web-3 1/1 Terminating 0 14m
6web-3 0/1 Terminating 0 14m
7web-3 0/1 Terminating 0 14m
8web-3 0/1 Terminating 0 14m
9web-2 1/1 Terminating 1 (7h29m ago) 22h
10web-2 0/1 Terminating 1 (7h29m ago) 22h
11web-2 0/1 Terminating 1 (7h29m ago) 22h
12web-2 0/1 Terminating 1 (7h29m ago) 22h
StatefulSet滚动更新的时候会先删除旧的副本,再创建新的副本,如果只有⼀个副本的话,会导致业务不可⽤,所以要根据⾃⼰的实际情况选择使⽤StatefulSet或者Deployment,如果必须固定主机名或者pod名称,建议使⽤StatefulSet
查看主机名称
1[root@k8s-master01 ~]# kubectl exec -ti web-0 -- sh
2# hostname
3web-0
4# exit
StatefulSet更新策略
女娲造人
RollingUpdate
OnDelete
StatefulSet和Deployment⼀样,有⼏种更新⽅式
RollingUpdate
查看更新⽅式
1[root@k8s-master01 ~]# kubectl get sts -o yaml
蔻萝兰2 updateStrategy:
3 rollingUpdate:
4 partition: 0
5 type: RollingUpdate # 默认滚动更新,从下往上更新
扩容到3个副本
1[root@k8s-master01 ~]# kubectl scale --replicas=3 sts web
2statefult.apps/web scaled
查看pod
1[root@k8s-master01 ~]# kubectl get po
2NAME READY STATUS RESTARTS AGE
3web-0 1/1 Running 0 53m
4web-1 1/1 Running 1 (8h ago) 23h
5web-2 1/1 Running 0 15s
滚动更新顺序是web-2,web-1,web-0,从下往上更新,如果更新过程中web-0挂掉了,则会等待web-0恢复到状态为ready之后再继续从下往上滚动更新
打开另⼀个窗⼝监控StatefulSet
1[root@k8s-master01 ~]# kubectl get po -l app=nginx -w
2NAME READY STATUS RESTARTS AGE
3web-0 1/1 Running 0 13s
4web-1 1/1 Running 0 23s
5web-2 1/1 Running 0 33s
修改镜像地址触发更新
1[root@k8s-master01 ~]# kubectl edit sts web
2/image 回车
3# 修改镜像
4 - image: nginx:1.15.3
查看更新过程
1[root@k8s-master01 ~]# kubectl get po
2NAME READY STATUS RESTARTS AGE
3web-0 1/1 Running 0 58m
4web-1 0/1 Terminating 1 (8h ago) 23h
5web-2 1/1 Running 0 4s
查看监控
1web-2 1/1 Terminating 0 101s
2web-2 0/1 Terminating 0 101s
3web-2 0/1 Terminating 0 110s
4web-2 0/1 Terminating 0 110s
5web-2 0/1 Pending 0 0s
6web-2 0/1 Pending 0 0s
7web-2 0/1 ContainerCreating 0 0s
8web-2 1/1 Running 0 2s
9web-1 1/1 Terminating 0 102s
10web-1 0/1 Terminating 0 103s
11web-1 0/1 Terminating 0 110s
12web-1 0/1 Terminating 0 110s
13web-1 0/1 Pending 0 0s
14web-1 0/1 Pending 0 0s
15web-1 0/1 ContainerCreating 0 0s
16web-1 1/1 Running 0 1s
17web-0 1/1 Terminating 0 101s
18web-0 0/1 Terminating 0 102s
19web-0 0/1 Terminating 0 110s
20web-0 0/1 Terminating 0 110s
沐浴晨光21web-0 0/1 Pending 0 0s
22web-0 0/1 Pending 0 0s
23web-0 0/1 ContainerCreating 0 0s
24web-0 1/1 Running 0 1s OnDelete
修改更新状态为OnDelete
1[root@k8s-master01 ~]# kubectl edit sts web
2# 修改以下内容
3 updateStrategy:
4 type: OnDelete
修改镜像地址
1[root@k8s-master01 ~]# kubectl edit sts web
2/image 回车
3# 修改镜像
4 - image: nginx:1.15.2
查看pod,可以看到没有更新
1[root@k8s-master01 ~]# kubectl get po
2NAME READY STATUS RESTARTS AGE 3web-0 1/1 Running 0 3m26s
4web-1 1/1 Running 0 3m36s
5web-2 1/1 Running 0 3m49s
⼿动删除pod触发更新
1[root@k8s-master01 ~]# kubectl delete po web-2
2pod "web-2" deleted
查看pod
查看web-2镜像,可以看到更新成功
查看web-1镜像,可以看到没有更新,所以只有删除的时候才会更新镜像
删除两个pod
查看监控,可以看到按照删除顺序创建
查看所有pod镜像,可以看到三个pod的镜像都更新了
1
[root@k8s-master01 ~]# kubectl get po 2
建筑公司管理制度
NAME READY STATUS RESTARTS AGE 3
web-0 1/1 Running 0 5m6s 4web-1 1/1 Running 0 5m16s 5
web-2 1/1 Running 0 9s
1
[root@k8s-master01 ~]# kubectl get po web-2 -oyaml | grep image 2
- image: nginx:1.15.23
imagePullPolicy: IfNotPrent 4 image: nginx:1.15.25
imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f4241
[root@k8s-master01 ~]# kubectl get po web-1 -oyaml | grep image 2
- image: nginx:1.15.33
imagePullPolicy: IfNotPrent 4 image: nginx:1.15.35 imageID: docker-pullable://nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d31
[root@k8s-master01 ~]# kubectl delete po web-0 web-12pod "web-0" deleted 3pod "web-1" deleted
1
web-0 0/1 Pending 0 0s 2
web-0 0/1 ContainerCreating 0 0s 3
web-0 1/1 Running 0 1s 4
web-1 0/1 Pending 0 0s 5
web-1 0/1 Pending 0 0s 6web-1 0/1 ContainerCreating 0 0s 7
web-1 1/1 Running 0 1s
1
[root@k8s-master01 ~]# kubectl get po -oyaml | grep image 2
imageID: docker-pullable://nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d33
- image: nginx:1.15.24
imagePullPolicy: IfNotPrent 5
image: nginx:1.15.26
imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f4247
- image: nginx:1.15.28
imagePullPolicy: IfNotPrent 9
image: nginx:1.15.210
imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f42411
- image: nginx:1.15.212
imagePullPolicy: IfNotPrent 13 image: nginx:1.15.214 imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424