【k8s资源调度-StatefulSet】

02-27 1060阅读

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

【k8s资源调度-StatefulSet】

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

2、配置文件如下

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

---
apiversion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1 # StatefulSet 资源的版本
kind: StatefulSet  #  StatefulSet 类型的资源
metadata:    # 元数据
  name: web  # StatefulSet的名字
spec:
  serviceName: "nginx"   # 使用哪个service服务来管理dns
  replicas: 2   # 期望副本数
  selector:   # 选择器
    matchLabels: # 按照标签匹配
      app: nginx  # 选择器名字
  template:     # pod 的模板
    metadata:    # 元数据
      labels:    # pod的标签
        app: nginx  # 标签值是:app=nginx
    spec:         # pod的期望信息
      containers:   # pod的容器信息
      - name: nginx   # pod的名字
        image: nginx:1.20   # 容器镜像信息
        ports:             # 端口
        - containerPort: 80   # 容器的端口
          name: web       # 容器内端口的名字
# 以下信息可以不先不使用  -----------------------------------------------
        volumeMounts:  # 加载一个数据卷
        - name: www    # 加载的数据卷的名字是www
          mountPath: /usr/share/nginx/html  # 加载到容器中的哪个目录
volumeclaimTemplates:  # 数据卷模板
  metadata:   #数据卷描述
    name: www   #数据卷的名称
    annotations:  #数据卷的注解
      volume.alpha.kubernetes.io/storage-class:anything
  spec: #数据卷的规约
    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             443/TCP   4d4h
nginx        ClusterIP   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:             
Annotations:        
Replicas:           2 desired | 2 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.20
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Volume Claims:    
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  3m4s  statefulset-controller  create Pod web-0 in StatefulSet web successful
  Normal  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.local
Name:      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.local
Name:      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:             
Annotations:        
Replicas:           5 desired | 5 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.20
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Volume Claims:    
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  39m   statefulset-controller  create Pod web-0 in StatefulSet web successful
  Normal  SuccessfulCreate  39m   statefulset-controller  create Pod web-1 in StatefulSet web successful
  Normal  SuccessfulCreate  48s   statefulset-controller  create Pod web-2 in StatefulSet web successful
  Normal  SuccessfulCreate  46s   statefulset-controller  create Pod web-3 in StatefulSet web successful
  Normal  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:             
Annotations:        
Replicas:           3 desired | 3 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.20
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Volume Claims:    
Events:
  Type    Reason            Age    From                    Message
  ----    ------            ----   ----                    -------
  Normal  SuccessfulCreate  43m    statefulset-controller  create Pod web-0 in StatefulSet web successful
  Normal  SuccessfulCreate  43m    statefulset-controller  create Pod web-1 in StatefulSet web successful
  Normal  SuccessfulCreate  4m33s  statefulset-controller  create Pod web-2 in StatefulSet web successful
  Normal  SuccessfulCreate  4m31s  statefulset-controller  create Pod web-3 in StatefulSet web successful
  Normal  SuccessfulCreate  4m30s  statefulset-controller  create Pod web-4 in StatefulSet web successful
  Normal  SuccessfulDelete  64s    statefulset-controller  delete Pod web-4 in StatefulSet web successful
  Normal  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:             
    Annotations:        
    Replicas:           3 desired | 3 total
    Update Strategy:    RollingUpdate
      Partition:        0
    Pods Status:        3 Running / 0 Waiting / 0 Succeeded / 0 Failed
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginx:1.21
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  
        Mounts:       
      Volumes:        
    Volume Claims:    
    Events:
      Type    Reason            Age                From                    Message
      ----    ------            ----               ----                    -------
      Normal  SuccessfulCreate  15m                statefulset-controller  create Pod web-3 in StatefulSet web successful
      Normal  SuccessfulCreate  15m                statefulset-controller  create Pod web-4 in StatefulSet web successful
      Normal  SuccessfulDelete  12m                statefulset-controller  delete Pod web-4 in StatefulSet web successful
      Normal  SuccessfulDelete  12m                statefulset-controller  delete Pod web-3 in StatefulSet web successful
      Normal  SuccessfulDelete  46s                statefulset-controller  delete Pod web-2 in StatefulSet web successful
      Normal  SuccessfulCreate  45s (x2 over 15m)  statefulset-controller  create Pod web-2 in StatefulSet web successful
      Normal  SuccessfulDelete  43s                statefulset-controller  delete Pod web-1 in StatefulSet web successful
      Normal  SuccessfulCreate  42s (x2 over 54m)  statefulset-controller  create Pod web-1 in StatefulSet web successful
      Normal  SuccessfulDelete  41s                statefulset-controller  delete Pod web-0 in StatefulSet web successful
      Normal  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         
    2         
    [root@k8s-master ~]# kubectl rollout history  sts  web  --revision=2
    statefulset.apps/web with revision #2
    Pod Template:
      Labels:	app=nginx
      Containers:
       nginx:
        Image:	nginx:1.21
        Port:	80/TCP
        Host Port:	0/TCP
        Environment:	
        Mounts:	
      Volumes:	
    [root@k8s-master ~]#
    

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

    灰度发布(金丝雀发布)是指在测试环境测试正常,然后上线到正式环境,通过在线上部分服务器发布,验证程序的可靠性,最后通过滚动发布,所有的资源都全部更新。

    目标:将项目上线后产生的问题,尽量降到最低。

    核心:如何控制每次只更新那些版本,就需要利用partition字段。

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

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

      【k8s资源调度-StatefulSet】

      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 修改

      【k8s资源调度-StatefulSet】

      5.1.6.3 修改image的版本信息

      【k8s资源调度-StatefulSet】

      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 Image
          Image:          nginx:1.20
          Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
      [root@k8s-master ~]# kubectl describe  pod web-3  |grep Image
          Image:          nginx:1.20
          Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
      [root@k8s-master ~]# kubectl describe  pod web-2  |grep Image
          Image:          nginx:1.21
          Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
      [root@k8s-master ~]# kubectl describe  pod web-1  |grep Image
          Image:          nginx:1.21
          Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
      [root@k8s-master ~]# kubectl describe  pod web-0  |grep Image
          Image:          nginx:1.21
          Image 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         
      3         
      # 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=nginx
        Containers:
         nginx:
          Image:	nginx:1.20
          Port:	80/TCP
          Host Port:	0/TCP
          Environment:	
          Mounts:	
        Volumes:	
      
      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:             
      Annotations:        
      Replicas:           5 desired | 5 total
      Update Strategy:    RollingUpdate
        Partition:        3
      Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
      Pod Template:
        Labels:  app=nginx
        Containers:
         nginx:
          Image:        nginx:1.20
          Port:         80/TCP
          Host Port:    0/TCP
          Environment:  
          Mounts:       
        Volumes:        
      Volume Claims:    
      Events:
        Type    Reason            Age                From                    Message
        ----    ------            ----               ----                    -------
        Normal  SuccessfulDelete  9m6s (x2 over 9h)  statefulset-controller  delete Pod web-4 in StatefulSet web successful
        Normal  SuccessfulCreate  9m4s (x3 over 9h)  statefulset-controller  create Pod web-4 in StatefulSet web successful
        Normal  SuccessfulDelete  9m2s (x2 over 9h)  statefulset-controller  delete Pod web-3 in StatefulSet web successful
        Normal  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的时候,其他资源就全部更新了。
      • 实际生产环境中,灰度发布可以根据实际使用情况使用,可以多次灰度发布。

        【k8s资源调度-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:             
        Annotations:        
        Replicas:           5 desired | 5 total
        Update Strategy:    RollingUpdate
          Partition:        0
        Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
        Pod Template:
          Labels:  app=nginx
          Containers:
           nginx:
            Image:        nginx:1.20
            Port:         80/TCP
            Host Port:    0/TCP
            Environment:  
            Mounts:       
          Volumes:        
        Volume Claims:    
        Events:
          Type    Reason            Age               From                    Message
          ----    ------            ----              ----                    -------
          Normal  SuccessfulDelete  12m (x2 over 9h)  statefulset-controller  delete Pod web-4 in StatefulSet web successful
          Normal  SuccessfulCreate  12m (x3 over 9h)  statefulset-controller  create Pod web-4 in StatefulSet web successful
          Normal  SuccessfulDelete  12m (x2 over 9h)  statefulset-controller  delete Pod web-3 in StatefulSet web successful
          Normal  SuccessfulCreate  12m (x3 over 9h)  statefulset-controller  create Pod web-3 in StatefulSet web successful
          Normal  SuccessfulCreate  8s (x3 over 9h)   statefulset-controller  create Pod web-2 in StatefulSet web successful
          Normal  SuccessfulDelete  8s (x2 over 9h)   statefulset-controller  delete Pod web-2 in StatefulSet web successful
          Normal  SuccessfulDelete  6s (x2 over 9h)   statefulset-controller  delete Pod web-1 in StatefulSet web successful
          Normal  SuccessfulCreate  4s (x3 over 10h)  statefulset-controller  create Pod web-1 in StatefulSet web successful
          Normal  SuccessfulDelete  2s (x2 over 9h)   statefulset-controller  delete Pod web-0 in StatefulSet web successful
          Normal  SuccessfulCreate  1s (x3 over 10h)  statefulset-controller  create Pod web-0 in StatefulSet web successful
        

        5.2 更新方式-OnDelete

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

        5.2.1 修改更新策略

        【k8s资源调度-StatefulSet】

        5.2.2 修改镜像版本信息

        【k8s资源调度-StatefulSet】

        5.2.3 镜像更新信息

        [root@k8s-master ~]# kubectl rollout history sts web  --revision=0
        statefulset.apps/web
        REVISION  CHANGE-CAUSE
        3         
        4         
        [root@k8s-master ~]# kubectl rollout history sts web  --revision=4
        statefulset.apps/web with revision #4
        Pod Template:
          Labels:	app=nginx
          Containers:
           nginx:
            Image:	nginx:1.21
            Port:	80/TCP
            Host Port:	0/TCP
            Environment:	
            Mounts:	
          Volumes:	
        

        5.2.4 查看pod,pod暂时未更新

        [root@k8s-master ~]# kubectl describe pod web-4  | grep   Image
            Image:          nginx:1.20
            Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
        [root@k8s-master ~]# kubectl describe pod web-3  | grep   Image
            Image:          nginx:1.20
            Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
        [root@k8s-master ~]# kubectl describe pod web-2  | grep   Image
            Image:          nginx:1.20
            Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
        [root@k8s-master ~]# kubectl describe pod web-1  | grep   Image
            Image:          nginx:1.20
            Image ID:       docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
        [root@k8s-master ~]# kubectl describe pod web-0  | grep   Image
            Image:          nginx:1.20
            Image 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   Image
            Image:          nginx:1.21
            Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
        [root@k8s-master ~]# kubectl describe pod web-3  | grep   Image
            Image:          nginx:1.20
            Image 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:             
        Annotations:        
        Replicas:           5 desired | 5 total
        Update Strategy:    OnDelete
        Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
        Pod Template:
          Labels:  app=nginx
          Containers:
           nginx:
            Image:        nginx:1.21
            Port:         80/TCP
            Host Port:    0/TCP
            Environment:  
            Mounts:       
          Volumes:        
        Volume Claims:    
        Events:
          Type    Reason            Age                 From                    Message
          ----    ------            ----                ----                    -------
          Normal  SuccessfulDelete  27m (x2 over 9h)    statefulset-controller  delete Pod web-4 in StatefulSet web successful
          Normal  SuccessfulDelete  27m (x2 over 9h)    statefulset-controller  delete Pod web-3 in StatefulSet web successful
          Normal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-2 in StatefulSet web successful
          Normal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-1 in StatefulSet web successful
          Normal  SuccessfulDelete  14m (x2 over 9h)    statefulset-controller  delete Pod web-0 in StatefulSet web successful
          Normal  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 successful
          Normal  SuccessfulCreate  15s (x4 over 10h)   statefulset-controller  create Pod web-2 in StatefulSet web successful
          Normal  SuccessfulCreate  13s (x4 over 10h)   statefulset-controller  create Pod web-1 in StatefulSet web successful
          Normal  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             443/TCP   4d15h
                nginx        ClusterIP   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             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             443/TCP   4d15h
                nginx        ClusterIP   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             443/TCP   4d15h
                nginx        ClusterIP   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             443/TCP   4d15h
                
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]