【k8s资源调度-StatefulSet】

1、部署对象StatefulSet资源(无状态应用)

在这里插入图片描述

StatefulSet针对的是有状态应用,有状态应用会对我们的当前pod的网络、文件系统等有关联。

2、配置文件如下

StatefulSet资源的配置文件粗略如下,如下的配置信息包含了数据卷,目前还未涉及到数据卷的信息,可以在创建StatefulSet的时候把数据卷的信息先删除。如果不删除这个数据卷的信息,创建的StatefulSet会卡在没有数据卷的信息导致无法ready。

---
apiversion: v1
kind: Service
metadata:name: nginxlabels:app: nginx
spec:ports:- port: 80name: webclusterIP: Noneselector:app: nginx
---
apiVersion: apps/v1 # StatefulSet 资源的版本
kind: StatefulSet  #  StatefulSet 类型的资源
metadata:    # 元数据name: web  # StatefulSet的名字
spec:serviceName: "nginx"   # 使用哪个service服务来管理dnsreplicas: 2   # 期望副本数selector:   # 选择器matchLabels: # 按照标签匹配app: nginx  # 选择器名字template:     # pod 的模板metadata:    # 元数据labels:    # pod的标签app: nginx  # 标签值是:app=nginxspec:         # pod的期望信息containers:   # pod的容器信息- name: nginx   # pod的名字image: nginx:1.20   # 容器镜像信息ports:             # 端口- containerPort: 80   # 容器的端口name: web       # 容器内端口的名字# 以下信息可以不先不使用  -----------------------------------------------volumeMounts:  # 加载一个数据卷- name: www    # 加载的数据卷的名字是wwwmountPath: /usr/share/nginx/html  # 加载到容器中的哪个目录
volumeclaimTemplates:  # 数据卷模板metadata:   #数据卷描述name: www   #数据卷的名称annotations:  #数据卷的注解volume.alpha.kubernetes.io/storage-class:anythingspec: #数据卷的规约accessModes:["ReadWriteOnce"] # 访问模式resources:requests:storage: 1Gi  # 需要1G的存储资源

3、创建StatefulSet资源

3.1 根据上文的配置文件创建StatefulSet资源

[root@k8s-master ~]# kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created

3.2 查看创建的StatefulSet资源的信息

通过get选项可以看到statefulset的资源已经创建完成

# 查看statefulset的信息
[root@k8s-master ~]# kubectl get sts
NAME   READY   AGE
web    2/2     58s

3.3 查看pod信息(pod名字是有序的)

这时候pod的名字很有序,不像deploy创建的pod,名字都是一个随机值。

# 查看pod的信息,这时候pod的名字很有序,不像deploy创建的pod,名字都是一个随机值。
[root@k8s-master ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          64s
web-1   1/1     Running   0          63s

3.4 查看service信息

查看service的信息,我们刚创建的service名字是nginx,目前service还是无ip的信息,因为我们目前还未做具体的绑定的操作。(绑定service和外部的信息)目前只是映射到容器内而已,我们有一个基于服务名访问就可以了(可以通过k8s集群中创建一个新的pod来访问)。

# 查看service的信息,我们刚创建的service名字是nginx,目前service还是无ip的信息,因为我们目前还未做具体的绑定的操作。(绑定service和外部的信息)目前只是映射到容器内而已,我们有一个基于服务名访问就可以了
[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d4h
nginx        ClusterIP   None         <none>        80/TCP    70s

3.5 查看StatefulSet资源的描述信息

从如下的信息中可以看到,创建的StatefulSet资源已经创建成功

[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           2 desired | 2 total
Update Strategy:    RollingUpdatePartition:        0
Pods Status:        2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.20Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age   From                    Message----    ------            ----  ----                    -------Normal  SuccessfulCreate  3m4s  statefulset-controller  create Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  3m3s  statefulset-controller  create Pod web-1 in StatefulSet web successful

3.6 通过busybox这个镜像来测试pod的dns是否正常

busybox 是linux内的一个工具的镜像,这个工具镜像内会有我们常用的一些工具。

StatefulSet中每个Pod的DNS格式为(.namespace.svc.cluster.local
可以省略)
statefulSetName-{O…N-1).serviceName.namespace.svc.cluster.local

[root@k8s-master ~]# kubectl run -it --image  busybox:1.28.4    dns-test   /bin/sh
If you don't see a command prompt, try pressing enter.
/ # ping web-0.nginx
PING web-0.nginx (10.2.1.35): 56 data bytes
64 bytes from 10.2.1.35: seq=0 ttl=64 time=0.129 ms
64 bytes from 10.2.1.35: seq=1 ttl=64 time=0.225 ms
64 bytes from 10.2.1.35: seq=2 ttl=64 time=0.134 ms
64 bytes from 10.2.1.35: seq=3 ttl=64 time=0.127 ms
^C
--- web-0.nginx ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.127/0.153/0.225 ms
/ # ping web-1.nginx
PING web-1.nginx (10.2.2.18): 56 data bytes
64 bytes from 10.2.2.18: seq=0 ttl=62 time=0.736 ms
64 bytes from 10.2.2.18: seq=1 ttl=62 time=1.568 ms
64 bytes from 10.2.2.18: seq=2 ttl=62 time=1.469 ms
64 bytes from 10.2.2.18: seq=3 ttl=62 time=0.795 ms
^C
--- web-1.nginx ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.736/1.142/1.568 ms## 以下信息可以查看看pod的dns 域名所绑定的信息
/ # nslookup web-0.nginx
Server:    10.1.0.10
Address 1: 10.1.0.10 kube-dns.kube-system.svc.cluster.localName:      web-0.nginx
Address 1: 10.2.1.35 web-0.nginx.default.svc.cluster.local
/ # nslookup web-1.nginx
Server:    10.1.0.10
Address 1: 10.1.0.10 kube-dns.kube-system.svc.cluster.localName:      web-1.nginx
Address 1: 10.2.2.18 web-1.nginx.default.svc.cluster.local
/ # exit
Session ended, resume using 'kubectl attach dns-test -c dns-test -i -t' command when the pod is running

4、扩容和缩容

扩容和缩容两种写法
kubectl scale statefulset web --replicas=3
kubectl patch statefulset web - p ‘{“spec”:{“replicas”:3))’

4.1 扩容

本文3.1小节创建了statefulset资源是2个,现在扩容到5个,如下,扩容也是有序的扩容

[root@k8s-master ~]# kubectl scale statefulset web --replicas=5
statefulset.apps/web scaled# 查看sts信息
[root@k8s-master ~]# kubectl get sts
NAME   READY   AGE
web    5/5     39m# 查看pod信息,有序的5个资源
[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS        AGE
dns-test   1/1     Running   1 (6m51s ago)   13m
web-0      1/1     Running   0               39m
web-1      1/1     Running   0               39m
web-2      1/1     Running   0               21s
web-3      1/1     Running   0               19s
web-4      1/1     Running   0               18s# 查看sts的资源描述
[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           5 desired | 5 total
Update Strategy:    RollingUpdatePartition:        0
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.20Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age   From                    Message----    ------            ----  ----                    -------Normal  SuccessfulCreate  39m   statefulset-controller  create Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  39m   statefulset-controller  create Pod web-1 in StatefulSet web successfulNormal  SuccessfulCreate  48s   statefulset-controller  create Pod web-2 in StatefulSet web successfulNormal  SuccessfulCreate  46s   statefulset-controller  create Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  45s   statefulset-controller  create Pod web-4 in StatefulSet web successful
[root@k8s-master ~]#

4.2 缩容

上面小节中创建了5个副本,现在缩容到3个,如下,缩容也是有序的缩容

[root@k8s-master ~]# kubectl scale statefulset web --replicas=3
statefulset.apps/web scaled[root@k8s-master ~]# kubectl get sts
NAME   READY   AGE
web    3/3     42m[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (10m ago)   16m
web-0      1/1     Running   0             42m
web-1      1/1     Running   0             42m
web-2      1/1     Running   0             3m38s[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           3 desired | 3 total
Update Strategy:    RollingUpdatePartition:        0
Pods Status:        3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.20Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age    From                    Message----    ------            ----   ----                    -------Normal  SuccessfulCreate  43m    statefulset-controller  create Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  43m    statefulset-controller  create Pod web-1 in StatefulSet web successfulNormal  SuccessfulCreate  4m33s  statefulset-controller  create Pod web-2 in StatefulSet web successfulNormal  SuccessfulCreate  4m31s  statefulset-controller  create Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  4m30s  statefulset-controller  create Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  64s    statefulset-controller  delete Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  62s    statefulset-controller  delete Pod web-3 in StatefulSet web successful

5、镜像更新

  • 镜像更新(目前还不支持直接更新image,需要patch来间接实现)

5.1 更新方式-RollingUpdate

StatefulSet也可以采用滚动更新策略,同样是修改pod,template属性后会触发更新,但是由于od是有序的,在StatefulSet中更新时是基于pod的顺序倒序更新的

5.1.1 更新镜像信息

[root@k8s-master ~]# kubectl patch sts web  --type='json'  -p='[{"op": "replace","path": "/spec/template/spec/containers/0/image","value": "nginx:1.21"}]'
statefulset.apps/web patched

5.1.2 查看statefulset的状态

[root@k8s-master ~]# kubectl get sts web
NAME   READY   AGE
web    3/3     54m

5.1.3 查看pod的状态

[root@k8s-master ~]# kubectl get po
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (21m ago)   28m
web-0      1/1     Running   0             25s
web-1      1/1     Running   0             27s
web-2      1/1     Running   0             30s

5.1.4 查看statefulset的描述信息

可以从最后的events信息看到,更新pod的时候是从序号大的更新到序号小的。

[root@k8s-master ~]# kubectl describe   sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           3 desired | 3 total
Update Strategy:    RollingUpdatePartition:        0
Pods Status:        3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.21Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age                From                    Message----    ------            ----               ----                    -------Normal  SuccessfulCreate  15m                statefulset-controller  create Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  15m                statefulset-controller  create Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  12m                statefulset-controller  delete Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  12m                statefulset-controller  delete Pod web-3 in StatefulSet web successfulNormal  SuccessfulDelete  46s                statefulset-controller  delete Pod web-2 in StatefulSet web successfulNormal  SuccessfulCreate  45s (x2 over 15m)  statefulset-controller  create Pod web-2 in StatefulSet web successfulNormal  SuccessfulDelete  43s                statefulset-controller  delete Pod web-1 in StatefulSet web successfulNormal  SuccessfulCreate  42s (x2 over 54m)  statefulset-controller  create Pod web-1 in StatefulSet web successfulNormal  SuccessfulDelete  41s                statefulset-controller  delete Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  40s (x2 over 54m)  statefulset-controller  create Pod web-0 in StatefulSet web successful

5.1.5 查看statefulset的历史更新信息

[root@k8s-master ~]# kubectl rollout history  sts  web  --revision=0
statefulset.apps/web
REVISION  CHANGE-CAUSE
1         <none>
2         <none>[root@k8s-master ~]# kubectl rollout history  sts  web  --revision=2
statefulset.apps/web with revision #2
Pod Template:Labels:	app=nginxContainers:nginx:Image:	nginx:1.21Port:	80/TCPHost Port:	0/TCPEnvironment:	<none>Mounts:	<none>Volumes:	<none>[root@k8s-master ~]#

5.1.6 利用滚动更新的属性可以实现–灰度发布/金丝雀发布

灰度发布(金丝雀发布)是指在测试环境测试正常,然后上线到正式环境,通过在线上部分服务器发布,验证程序的可靠性,最后通过滚动发布,所有的资源都全部更新。
目标:将项目上线后产生的问题,尽量降到最低。
核心:如何控制每次只更新那些版本,就需要利用partition字段。

  • 利用滚动更新中的partition属性,可以实现简易的灰度发布的效果。
  • 例如我们有5个pod,如果当前partition设置为3,那么此时滚动更新时,只会更新那些序号>=3的pod。
  • 利用该机制,我们可以通过控制partition的值,来决定只更新其中一部分pod,确认没有问题后再主键增大更新的pod数量,最终实现全部pod更新。

通过 【kubectl edit sts web】查看statefulset中多了一个滚动更新的属性 partition

在这里插入图片描述

5.1.6.1 扩容到5个节点
[root@k8s-master ~]# kubectl scale statefulset  web  --replicas=5
statefulset.apps/web scaled
[root@k8s-master ~]# kubectl get statefulsets.apps web
NAME   READY   AGE
web    4/5     10h
[root@k8s-master ~]# kubectl get statefulsets.apps web
NAME   READY   AGE
web    5/5     10h
5.1.6.2 修改partition的值,默认是0

通过 kubectl edit sts web 修改
在这里插入图片描述

5.1.6.3 修改image的版本信息

在这里插入图片描述

5.1.6.4 查看statefulset的状态

通过以下已经查看到statefulset已经更新完成了,ready的状态

[root@k8s-master ~]# kubectl get statefulsets.apps web
NAME   READY   AGE
web    4/5     10h
[root@k8s-master ~]# kubectl get statefulsets.apps web
NAME   READY   AGE
web    5/5     10h
5.1.6.5 查看pod的信息

刚才我们设置的partition的数字为3,总共的副本是5个,通过以下信息可以查看到,更新了web-4和web-3,web-2、web-1、web-0都没有更新。

[root@k8s-master ~]# kubectl describe  pod web-4  |grep ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe  pod web-3  |grep ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe  pod web-2  |grep ImageImage:          nginx:1.21Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[root@k8s-master ~]# kubectl describe  pod web-1  |grep ImageImage:          nginx:1.21Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[root@k8s-master ~]# kubectl describe  pod web-0  |grep ImageImage:          nginx:1.21Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
5.1.6.5 查看更新的历史版本内容信息

[root@k8s-master ~]# kubectl rollout history sts web --revision=0
statefulset.apps/web
REVISION  CHANGE-CAUSE
2         <none>
3         <none># resivion 3的更新内容为 Image:	nginx:1.20
[root@k8s-master ~]# kubectl rollout history sts web --revision=3
statefulset.apps/web with revision #3
Pod Template:Labels:	app=nginxContainers:nginx:Image:	nginx:1.20Port:	80/TCPHost Port:	0/TCPEnvironment:	<none>Mounts:	<none>Volumes:	<none>
5.1.6.6 查看statefulset的描述信息

通过evevts,可以看到了statefulset资源只更新了web-4和web-3

[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           5 desired | 5 total
Update Strategy:    RollingUpdatePartition:        3
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.20Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age                From                    Message----    ------            ----               ----                    -------Normal  SuccessfulDelete  9m6s (x2 over 9h)  statefulset-controller  delete Pod web-4 in StatefulSet web successfulNormal  SuccessfulCreate  9m4s (x3 over 9h)  statefulset-controller  create Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  9m2s (x2 over 9h)  statefulset-controller  delete Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  9m1s (x3 over 9h)  statefulset-controller  create Pod web-3 in StatefulSet web successful
5.1.6.7 滚动更新其他资源
  • 刚才我们是更新了2个pod资源,还剩下3个pod资源没有更新,把partition的值设置为0的时候,其他资源就全部更新了。
  • 实际生产环境中,灰度发布可以根据实际使用情况使用,可以多次灰度发布。

在这里插入图片描述

[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           5 desired | 5 total
Update Strategy:    RollingUpdatePartition:        0
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.20Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age               From                    Message----    ------            ----              ----                    -------Normal  SuccessfulDelete  12m (x2 over 9h)  statefulset-controller  delete Pod web-4 in StatefulSet web successfulNormal  SuccessfulCreate  12m (x3 over 9h)  statefulset-controller  create Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  12m (x2 over 9h)  statefulset-controller  delete Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  12m (x3 over 9h)  statefulset-controller  create Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  8s (x3 over 9h)   statefulset-controller  create Pod web-2 in StatefulSet web successfulNormal  SuccessfulDelete  8s (x2 over 9h)   statefulset-controller  delete Pod web-2 in StatefulSet web successfulNormal  SuccessfulDelete  6s (x2 over 9h)   statefulset-controller  delete Pod web-1 in StatefulSet web successfulNormal  SuccessfulCreate  4s (x3 over 10h)  statefulset-controller  create Pod web-1 in StatefulSet web successfulNormal  SuccessfulDelete  2s (x2 over 9h)   statefulset-controller  delete Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  1s (x3 over 10h)  statefulset-controller  create Pod web-0 in StatefulSet web successful

5.2 更新方式-OnDelete

OnDelete的更新策略是删除pod后才会更新,修改template模板的时候并不会更新。

5.2.1 修改更新策略

在这里插入图片描述

5.2.2 修改镜像版本信息

在这里插入图片描述

5.2.3 镜像更新信息

[root@k8s-master ~]# kubectl rollout history sts web  --revision=0
statefulset.apps/web
REVISION  CHANGE-CAUSE
3         <none>
4         <none>[root@k8s-master ~]# kubectl rollout history sts web  --revision=4
statefulset.apps/web with revision #4
Pod Template:Labels:	app=nginxContainers:nginx:Image:	nginx:1.21Port:	80/TCPHost Port:	0/TCPEnvironment:	<none>Mounts:	<none>Volumes:	<none>

5.2.4 查看pod,pod暂时未更新

[root@k8s-master ~]# kubectl describe pod web-4  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe pod web-3  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe pod web-2  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe pod web-1  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
[root@k8s-master ~]# kubectl describe pod web-0  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b

5.2.5 删除一个pod,可以看到删除的pod信息已经更新

[root@k8s-master ~]# kubectl delete pod web-4
pod "web-4" deleted
[root@k8s-master ~]# kubectl get sts web
NAME   READY   AGE
web    5/5     10h
[root@k8s-master ~]# kubectl get pod web-4
NAME    READY   STATUS    RESTARTS   AGE
web-4   1/1     Running   0          21s
[root@k8s-master ~]# kubectl describe pod web-4  | grep   ImageImage:          nginx:1.21Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[root@k8s-master ~]# kubectl describe pod web-3  | grep   ImageImage:          nginx:1.20Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b

5.2.6 滚动更新其他资源

[root@k8s-master ~]# kubectl delete pod web-3
pod "web-3" deleted
[root@k8s-master ~]# kubectl delete pod web-2
pod "web-2" deleted
[root@k8s-master ~]# kubectl delete pod web-1
pod "web-1" deleted
[root@k8s-master ~]# kubectl delete pod web-0
pod "web-0" deleted
[root@k8s-master ~]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sat, 24 Feb 2024 02:20:14 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           5 desired | 5 total
Update Strategy:    OnDelete
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels:  app=nginxContainers:nginx:Image:        nginx:1.21Port:         80/TCPHost Port:    0/TCPEnvironment:  <none>Mounts:       <none>Volumes:        <none>
Volume Claims:    <none>
Events:Type    Reason            Age                 From                    Message----    ------            ----                ----                    -------Normal  SuccessfulDelete  27m (x2 over 9h)    statefulset-controller  delete Pod web-4 in StatefulSet web successfulNormal  SuccessfulDelete  27m (x2 over 9h)    statefulset-controller  delete Pod web-3 in StatefulSet web successfulNormal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-2 in StatefulSet web successfulNormal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-1 in StatefulSet web successfulNormal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-0 in StatefulSet web successfulNormal  SuccessfulCreate  2m4s (x4 over 10h)  statefulset-controller  create Pod web-4 in StatefulSet web successful## 以下信息可以看到其他的4个副本也同步更新了Normal  SuccessfulCreate  18s (x4 over 10h)   statefulset-controller  create Pod web-3 in StatefulSet web successfulNormal  SuccessfulCreate  15s (x4 over 10h)   statefulset-controller  create Pod web-2 in StatefulSet web successfulNormal  SuccessfulCreate  13s (x4 over 10h)   statefulset-controller  create Pod web-1 in StatefulSet web successfulNormal  SuccessfulCreate  9s (x4 over 10h)    statefulset-controller  create Pod web-0 in StatefulSet web successful

5.3 有状态的资源删除方式

  • 删除方式:删除statefulset和Headlass service
  • 级联删除:删除statefulset时会同时制除pods
    • kubectl delete statefulset web
  • 非级联删除:删除statefulset时不会删除 pod ,删除sts后,pod不会删除
    • kubectl deelte sts web --cascade=false
  • 删除service
    • kubectl delete service nginx

5.3.1 级联删除

5.3.1.1 删除statefulset资源
[root@k8s-master ~]# kubectl delete sts web
statefulset.apps "web" deleted
5.3.1.2 查看statefule资源和pod资源
[root@k8s-master ~]# kubectl get sts web
Error from server (NotFound): statefulsets.apps "web" not found
[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (10h ago)   10h
5.3.1.3 查看service资源
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d15h
nginx        ClusterIP   None         <none>        80/TCP    10h
5.3.1.4 删除service资源
[root@k8s-master ~]# kubectl delete svc nginx
service "nginx" deleted
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d15h

5.3.2 非级联删除

5.3.2.1 创建资源

由于刚才的资源我们已经删除,现在重新创建一个资源

[root@k8s-master ~]# kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created
[root@k8s-master ~]# kubectl get sts web
NAME   READY   AGE
web    2/2     8s
[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (10h ago)   10h
web-0      1/1     Running   0             14s
web-1      1/1     Running   0             12s
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d15h
nginx        ClusterIP   None         <none>        80/TCP    25s
5.3.2.2 删除statefulset资源(添加选项 --cascade=false)
[root@k8s-master ~]# kubectl delete sts web --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
statefulset.apps "web" deleted
5.3.2.3 查看statefule资源和pod资源
[root@k8s-master ~]# kubectl get sts web
Error from server (NotFound): statefulsets.apps "web" not found
[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (10h ago)   10h
web-0      1/1     Running   0             3m15s
web-1      1/1     Running   0             3m13s
5.3.2.4 删除pod资源
[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS      AGE
dns-test   1/1     Running   1 (10h ago)   10h
web-0      1/1     Running   0             3m15s
web-1      1/1     Running   0             3m13s
[root@k8s-master ~]# kubectl delete po web-0 web-1
pod "web-0" deleted
pod "web-1" deleted
5.3.2.5 删除service资源
[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d15h
nginx        ClusterIP   None         <none>        80/TCP    4m29s
[root@k8s-master ~]# kubectl delete svc  nginx
service "nginx" deleted
[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   4d15h

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/488201.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python 3.7.3的安装

参考 Linux安装Python3.7-良许Linux教程网 (lxlinux.net) 1、Index of /ftp/python/3.7.9/ 1、安装gcc&#xff0c;yum -y install gcc 2、 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel…

有趣的CSS - 文字加载动画效果

大家好&#xff0c;我是 Just&#xff0c;这里是「设计师工作日常」&#xff0c;今天分享的是用 css 实现多字符模拟加载动画效果。 《有趣的css》系列最新实例通过公众号「设计师工作日常」发布。 目录 整体效果核心代码html 代码css 部分代码 完整代码如下html 页面css 样式页…

容器_Docker ( 06 )

容器_Docker ( 05 ) Kubernetes 资源对象管理 资源对象文件 模板与帮助信息 资源对象文件优势 命令无法实现高级复杂的功能某些资源对象使用命令无法创建方便管理 , 保存 , 追溯历史 资源对象文件太长 , 记不住怎么办 使用命令创建模板查询帮助信息查询官方手册 生成资源…

2024-02-21 作业

作业要求&#xff1a; 复习课上内容 //已完成结构体字节对齐&#xff0c;64位没做完的做完&#xff0c;32位重新都做一遍&#xff0c;课上指定2字节对齐的做一遍&#xff0c;自己验证 //已完成两种验证大小端对齐的代码写一遍复习指针内容 //已完成完善顺序表已写出的…

300分钟吃透分布式缓存-12讲:为何MC能长期维持高性能读写?

内存管理 slab 机制 讲完淘汰策略&#xff0c;我们接下来学习内存管理 slab 机制。 Mc 内存分配采用 slab 机制&#xff0c;slab 机制可以规避内存碎片&#xff0c;是 Mc 能持续高性能进行数据读写的关键。 slabclass Mc 的 slab 机制是通过 slabclass 来进行运作的&#x…

普中51单片机学习(DS1302)

DS1302时钟 DS1302实时时钟具有能计算2100年之前的秒、分、时、日、日期、星期、月、年的能力&#xff0c;还有闰年调整的能力。内部含有31个字节静态RAM&#xff0c;可提供用户访问。采用串行数据传送方式&#xff0c;使得管脚数量最少&#xff0c;简单SPI 3线接口。工作电压…

测试开源C#人脸识别模块DlibDotNet

百度“C# 换脸”找到参考文献4&#xff0c;发现其中使用DlibDotNet检测并识别人脸&#xff08;之前主要用的是ViewFaceCore&#xff09;&#xff0c;DlibDotNet是Dlib的.net封装版本&#xff0c;后者为开源C工具包&#xff0c;支持机器学习算法、图像处理等算法以支撑各类高级应…

Ansible 常见模块

模块文档 官方模块列表&#xff1a;https://docs.ansible.com/ansible/latest/collections/index_module.html # 查看支持的模块&#xff08;小写 l&#xff09; ansible-doc -l # 查看支持的模块个数-> 3387 ansible-doc -l | wc -l # ansible-doc 模块名 或者 ansible-…

从零开始学IO_FILE的堆利用:理解IO_FILE之fread

要学习基于IO_FILE的堆利用就得了解它的本质&#xff0c;以下会介绍几个主要的IO函数&#xff0c;结合源码和动态调试去学习。 调试环境搭建可参考环境从零开始配置pwn环境&#xff1a;从零开始配置pwn环境&#xff1a;优化pwn虚拟机配置支持libc等指令-CSDN博客 前面分析了系统…

Servlet使用Cookie和Session

一、会话技术 当用户访问web应用时&#xff0c;在许多情况下&#xff0c;web服务器必须能够跟踪用户的状态。比如许多用户在购物网站上购物&#xff0c;Web服务器为每个用户配置了虚拟的购物车。当某个用户请求将一件商品放入购物车时&#xff0c;web服务器必须根据发出请求的…

链表和顺序表的优劣分析及其时间、空间复杂度分析

链表和顺序表的优劣分析及其时间、空间复杂度分析 一、链表和顺序表的优劣分析二、算法复杂度<font face "楷体" size 5 color blue>//上面算法的执行次数大致为&#xff1a;F&#xff08;N&#xff09; N^22*N10;   N 10,F(10) 1002010 130次   N 1…

「JavaSE」String类3:字符串常量池

&#x1f387;个人主页&#xff1a;Ice_Sugar_7 &#x1f387;所属专栏&#xff1a;快来卷Java啦 &#x1f387;欢迎点赞收藏加关注哦&#xff01; 字符串常量池 &#x1f349;常量池&#x1f349;字符串常量池&#x1f349;intern 方法 &#x1f349;常量池 在Java程序中&…