【kubernetes】Pod容器3种常用健康探测技术

三种容器探测方法:

Pod容器3种常用健康探测技术:startupProbe启动探针,livenessprobe存活探针,readnessprobe准备就绪探针。

1、startupProbe:

探测容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。

2、livenessprobe:

用指定的方式(exec、tcp、http)检测pod中的容器是否正常运行,如果检测失败,则认为容器不健康,那么Kubelet将根据Pod中设置的 restartPolicy策略来判断Pod 是否要进行重启操作,如果容器配置中没有配置 livenessProbe,Kubelet 将认为存活探针探测一直为success(成功)状态。

3、readnessprobe:

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

Startupprobe,LivenessProbe和ReadinessProbe都支持下面三种探针:

  • exec:在容器中执行指定的命令,如果执行成功,退出码为 0 则探测成功。
spec:
  containers:
  - name: startup
    image: xianchao/tomcat-8.5-jre8:v1
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:  
     exec:   # 探测exec
       command:
       - "/bin/sh"
       - "-c"
       - "aa ps aux | grep tomcat"
     initialDelaySeconds: 20  # 容器启动后20s开始探测
     periodSeconds: 20        # 时间间隔20s再次探测
     timeoutSeconds: 10       #探测开始后,等待响应的超时时间10s
     successThreshold: 1      #成功1次才算成功
     failureThreshold: 3      #失败3次才算失败
  • TCPSocket:通过容器的 IP 地址和端口号执行 TCP 检查,如果能够建立 TCP 连接,则表明容器健康。
spec:
  containers:
  - name: startup
    image: xianchao/tomcat-8.5-jre8:v1
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:
     tcpSocket:    # 探测tcpSocket
       port: 8080
     initialDelaySeconds: 20  # 容器启动后20s开始探测
     periodSeconds: 20        # 时间间隔20s再次探测
     timeoutSeconds: 10       #探测开始后,等待响应的超时时间10s
     successThreshold: 1      #成功1次才算成功
     failureThreshold: 3      #失败3次才算失败
  • HTTPGet:通过容器的IP地址、端口号及路径调用 HTTP Get方法,如果响应的状态码大于等于200且小于400,则认为容器健康。

httpGet探测方式有如下可选的控制字段:

scheme: 用于连接host的协议,默认为HTTP。
host:要连接的主机名,默认为Pod IP,可以在http request head中设置host头部。
port:容器上要访问端口号或名称。
path:http服务器上的访问URI。
httpHeaders:自定义HTTP请求headers,HTTP允许重复headers。

spec:
  containers:
  - name: startup
    image: xianchao/tomcat-8.5-jre8:v1
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:
      httpGet:   # 探测httpGet
        path: /  # http服务器上的访问URI
        port: 8080 # 容器上要访问端口号
     initialDelaySeconds: 20  # 容器启动后20s开始探测
     periodSeconds: 20        # 时间间隔20s再次探测
     timeoutSeconds: 10       #探测开始后,等待响应的超时时间10s
     successThreshold: 1      #成功1次才算成功
     failureThreshold: 3      #失败3次才算失败

任何大于或等于200且小于400的代码表示探测成功。
任何其他代码表示失败,则杀死 Pod 进行重启操作。

探测结果有以下值:

1、Success:表示通过检测。
2、Failure:表示未通过检测。
3、Unknown:表示检测没有正常进行。

Pod探针的属性:

针对Liveness和Readiness两种探针

  • initialDelaySeconds:容器启动后要等待多少秒后探针开始工作,单位“秒”,默认是 0 秒,最小值是 0

  • periodSeconds: 执行探测的时间间隔(单位是秒),默认为 10s,单位“秒”,最小值是1

  • timeoutSeconds: 探针执行检测请求后,等待响应的超时时间,默认为1,单位“秒”。

  • successThreshold:连续探测几次成功,才认为探测成功,默认为 1,在 Liveness 探针中必须为1,最小值为1。

  • failureThreshold: 探测失败的重试次数,重试一定次数后将认为失败,在 readiness 探针中,Pod会被标记为未就绪,默认为 3,最小值为 1

两种探针区别:

ReadinessProbe 和 livenessProbe 可以使用相同探测方式,只是对 Pod 的处置方式不同:

  • readinessProbe 当检测失败后,将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除。
  • livenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施。

案例

一般程序中需要设置三种探针结合使用,并且也要结合实际情况,来配置初始化检查时间和检测间隔,下面列一个简单的 SpringBoot 项目的例子。

cat start-read-live.yaml 
apiVersion: v1
kind: Service
metadata:
  name: springboot-live
  labels:
    app: springboot
spec:
  type: NodePort
  ports:
  - name: server
    port: 8080
    targetPort: 8080
    nodePort: 31180
  - name: management
    port: 8081
    targetPort: 8081
    nodePort: 31181
  selector:
    app: springboot
---
apiVersion: v1
kind: Pod
metadata:
  name: springboot-live
  labels:
    app: springboot
spec:
  containers:
  - name: springboot
    image: mydlqclub/springboot-helloworld:0.0.1
    imagePullPolicy: IfNotPresent
    ports:
    - name: server
      containerPort: 8080
    - name: management
      containerPort: 8081
    readinessProbe:
      initialDelaySeconds: 20   
      periodSeconds: 5          
      timeoutSeconds: 10   
      httpGet:
        scheme: HTTP
        port: 8081
        path: /actuator/health
    livenessProbe:
      initialDelaySeconds: 20
      periodSeconds: 5
      timeoutSeconds: 10
      httpGet:
        scheme: HTTP
        port: 8081
        path: /actuator/health
    startupProbe:
      initialDelaySeconds: 20
      periodSeconds: 5
      timeoutSeconds: 10
      httpGet:
        scheme: HTTP
        port: 8081
        path: /actuator/health
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值