• K8s之零故障升级Pod健康探测详解


    一、Pod健康探测介绍

    中文官方参考文档:

    Pod探测是Kubernetes中的一种机制,用于检测Pod的状态和健康状况。当探测到Pod状态不正常时,根据重启策略进行相应的Pod操作,探测可以帮助Kubernetes集群自动化地管理容器的健康状态,提高应用程序的可靠性和可用性。

    探测针对Pod中容器进行操作,所以探测定义在kubectl explain pod.spec.containers 字段下面

    1、三种容器探测方法

    • 启动探测(StartupProbe):探测Pod中容器中的应用 是否已经启动,如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。

    • 存活探测(Liveness Probe):探测Pod中容器是否正常运行,如果探测失败,kubelet根据重启策略判断是否重启该容器。

    • 就绪探测(Readiness Probe):检测容器中的应用是否可以接受请求,当探测成功后才使Pod对外提供网络访问,将容器标记为就绪状态,可以加到pod前端负载,如果探测失败,则将容器标记为未就绪状态,会把pod从前端负载移除。

    三种探测的优先级是 启动探测最高,存活探测、就绪探测并行,启动探测成功后才会进行下面的探测。

    2、常用三种探测探针

    启动探测、存活探测、就绪探测都支持下面三种探针:

    • exec:在容器执行命令,通过返回码判断是否执行成功,非零表示失败。
    • tcpSocket:通过容器的IP地址和端口执行TCP检查,如果可以建立TCP连接,则表示探测成功。
    • httpGet:通过容器的IP地址、端口号及路径调用 HTTP Get方法,如果响应的状态码大于等于200且小于400,则认为容器健康。

    探针探测结果有以下值:

    • Success:表示通过检测。

    • Failure:表示未通过检测。

    • Unknown:表示检测没有正常进行

    3、探针相关属性说明

    使用帮助命令查看相关属性:

    kubectl explain pod.spec.containers.startupProbe
    
    • 1
    • periodSeconds:执行探测的间隔时间,单位秒,默认10秒
    • timeoutSeconds:执行探测后,超时时间,单位秒,默认1秒
    • successThreshold:连续探测几次成功,才算成功,默认1秒
    • failureThreshold:探测失败重试次数,默认3次,最小1次

    二、探测案例

    1、Pod启动探测案例-startupProbe

    案例一:使用 exec 探测容器内是否可以查看到tomcat进程,如果没有表示探测失败,根据重启策略做出对应的操作。

    cat startupProbe-exec.yaml 
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-startupprobe
      namespace: default
      labels:
        app: tomcat
        env: uat
    spec:
      containers:
      - name: container-startupprobe
        image: tomcat
        startupProbe:
          exec:      # 探测命令,返回非零表示失败
            command: ["/bin/bash", "-c", "ps -ef |grep  tomcat|grep -v grep|awk '{print $2}'"]
    
          initialDelaySeconds: 20  # 容器启动后多久开始探测
          periodSeconds: 20        # 执行探测间隔时间
          successThreshold: 1      # 成功多少次才算成功
          timeoutSeconds: 30       # 执行探针后,等待多少s,才算超时
          failureThreshold: 2      # 失败多少次才算失败
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    执行YAML文件:

    kubectl apply -f startupProbe-exec.yaml
    
    • 1

    动态查看Pod状态:

    get pods pod-startupprobe  -w
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IWivIjV7-1685189668254)(D:\MD归档文档\IMG\image-20230527131605293.png)]

    案例二:使用 tcpSocket 探测容器内是否可以查看到 8080端口,如果探测失败,根据重启策略做出对应的操作。

    cat startupProbe-tcpsocket.yaml 
    
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-startupprobe
      namespace: default
      labels:
        app: tomcat
        env: uat
    spec:
      containers:
      - name: container-startupprobe
        image: tomcat
        ports:
        - containerPort: 8080
        startupProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20  # 容器启动后多久开始探测
          periodSeconds: 20        # 执行探测间隔时间
          successThreshold: 1      # 成功多少次才算成功
          timeoutSeconds: 30       # 执行探针后,等待多少s,才算超时
          failureThreshold: 2      # 失败多少次才算失败
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    执行YAML文件:

    kubectl apply -f startupProbe-tcpsocket.yaml 
    
    • 1

    案例三:使用 httpGet 探测容器内网络是否可以正常访问,如果探测失败,根据重启策略做出对应的操作。

    cat startupProbe-httpget.yaml 
    
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-startupprobe
      namespace: default
      labels:
        app: nginx
        env: uat
    spec:
      containers:
      - name: container-1
        image: nginx
        ports:
        - containerPort: 80
        startupProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 20
          periodSeconds: 20
          successThreshold: 1
          failureThreshold: 2
          timeoutSeconds: 20
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    执行YAML文件:

    kubectl apply -f startupProbe-httpget.yaml 
    
    • 1

    2、Pod存活探测案例-livenessProbe

    案例一:使用 tcpSocket80端口 进行存活检测,如果探测失败,根据重启测试做出相应操作。

    cat livenessProbe-tcp.yaml 
    
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-liveness
      namespace: default
      labels:
        app: nginx
        env: uat
    
    spec:
      containers:
      - name: container-1
        image: nginx
        imagePullPolicy: IfNotPresent
        livenessProbe: 
          tcpSocket:
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    执行YAML文件:

    kubectl apply -f livenessProbe-tcp.yaml 
    
    • 1

    案例二:使用 httpGet/index.html 进行 存活检测,如果探测失败,根据重启测试做出相应操作。

    cat livenessProbe-http.yaml
    
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-liveness
      namespace: default
      labels:
        app: nginx
        env: uat
    
    spec:
      containers:
      - name: container-1
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        livenessProbe: 
          httpGet:
            path: /index.html
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    执行YAML文件:

    kubectl apply -f livenessProbe-tcp.yaml 
    
    • 1

    3、Pod就绪探测案例-readinessProbe

    就绪探测,如果探测失败会从Pod前端负载移除,所以我们要借助Service 资源才能看到效果,如下案例检测 80端口是否启动,如果没有检查到,则从 Service 中移除:

    cat readinessProbe-http.yaml 
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: svc-readiness
      labels:
        app: nginx
    spec:
      type: NodePort
      ports:
      - name: server
        port: 80
        targetPort: 80
        nodePort: 30080
      selector:
        app: nginx
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-readiness
      namespace: default
      labels:
        app: nginx
    spec:
      containers:
      - name: container-1
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        readinessProbe: 
          httpGet:
            path: /index.html
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    查看service、pod资源信息:

    kubectl get pod,svc -l app=nginx
    
    • 1

    查看service 中关联的Pod:

    kubectl describe svc|grep Endpoints
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K3Ri7rQR-1685189668255)(D:\MD归档文档\IMG\image-20230527195046169.png)]

    4、启动、存活、就绪探测混合使用案例

    案例:

    cat probe.yaml 
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: svc-probe
      labels:
        app: nginx
    spec:
      type: NodePort
      ports:
      - name: server
        port: 80
        targetPort: 80
        nodePort: 30080
      selector:
        app: nginx
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-probe
      namespace: default
      labels:
        app: nginx
    spec:
      containers:
      - name: container-1
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
    
        livenessProbe: # 存活探测,探测服务是否正常
          httpGet:
            path: /index.html
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
        readinessProbe: # 就绪探测,探测服务是否可以接受请求
          httpGet:
            path: /index.html
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
        startupProbe:  # 启动探测,探测容器内程序是否启动
          httpGet:
            path: /index.html
            port: 80
          timeoutSeconds: 15
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    执行YAML文件:

    kubectl apply -f probe.yaml 
    
    • 1

    三、总结

    1、探测总结:

    一共演示了三种探测,分别是启动探测,存活探测、就绪探测,启动顺序是启动探测最先执行,当启动探测成功后,存活探测和就绪探测并行,三种探测场景如下:

    • 启动探测(startupProbe):探测容器中程序是否启动,如果失败,根据重启策略进行对应操作。
    • 存活探测(livenessProbe):探测容器中程序是否正常运行,如果失败,根据重启策略进行对应操作。
    • 就绪探测(readinessProbe):探测容器中程序是否可以接受请求,如果失败,将从前端代理移除。

    2、存活探测和就绪探测区别:

    存活探测,探测失败是根据重启策略做对应操作,而就绪探测,探测失败,是将从前端代理移除,如service中移除,移除后就无法正常对外访问了。

  • 相关阅读:
    架构——方法多态(重载)
    AJAX的使用,搭建web服务器,AJAX响应消息类型,JSON
    【C#/.NET】MAUI上的依赖注入
    JUC03-volatile、CAS及并发原子类
    细胞器基因组|比较基因组分析助力深度挖掘细胞器进化关系
    Three.js使用ExtrudeGeometry拉伸模型
    【C语言】-结构体内存对齐。附详细图解
    java8特性,lambda表达式,简写的演变及应用
    关于js_节流的介绍和简单的使用
    移动端适配解决方案
  • 原文地址:https://blog.csdn.net/weixin_45310323/article/details/130905446