【Kubernetes运维篇】零故障升级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
  • 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      # 失败多少次才算失败

执行YAML文件:

kubectl apply -f startupProbe-exec.yaml

动态查看Pod状态:

get pods pod-startupprobe  -w

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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      # 失败多少次才算失败

执行YAML文件:

kubectl apply -f startupProbe-tcpsocket.yaml 

案例三:使用 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

执行YAML文件:

kubectl apply -f startupProbe-httpget.yaml 

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

执行YAML文件:

kubectl apply -f livenessProbe-tcp.yaml 

案例二:使用 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

执行YAML文件:

kubectl apply -f livenessProbe-tcp.yaml 

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

查看service、pod资源信息:

kubectl get pod,svc -l app=nginx

查看service 中关联的Pod:

kubectl describe svc|grep Endpoints

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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

执行YAML文件:

kubectl apply -f probe.yaml 

三、总结

1、探测总结:

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

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

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

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

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/130905446