Skip to content

k8s PodProbe

参考链接

探针三个类型

The kubelet uses liveness probes to know when to restart a container. For example, liveness probes could catch a deadlock, where an application is running, but unable to make progress. Restarting a container in such a state can help to make the application more available despite bugs.

1. StartupProbe

k8s 1.16 版本新增的探针,用于判断应用程序是否已经启动了。

当配置了 startupProbe 后,会先禁用其他探针,直到 startupProbe 成功后,其他探针才会继续。

作用:由于有时候不能准确预估应用一定是多长时间启动成功,因此配置另外两种方式不方便配置初始化时长来检测,而配置了 statupProbe 后,只有在应用启动成功了,才会执行另外两种探针,可以更加方便的结合使用另外两种探针使用。

startupProbe:
  httpGet:
    path: /api/startup
    port: 80

2. LivenessProbe

用于探测容器中的应用是否运行,如果探测失败,kubelet 会根据配置的重启策略进行重启,若没有配置,默认就认为容器启动成功,不会执行重启策略。

livenessProbe:
  failureThreshold: 5
  httpGet:
    path: /health
    port: 8080
    scheme: HTTP
  initialDelaySeconds: 60
  periodSeconds: 10
  successThreshold: 1
  timeoutSeconds: 5

3. ReadinessProbe

用于探测容器内的程序是否健康,它的返回值如果返回 success,那么就认为该容器已经完全启动,并且该容器是可以接收外部流量的。

readinessProbe:
  failureThreshold: 3 # 错误次数
  httpGet:
    path: /ready
    port: 8181
    scheme: HTTP
  periodSeconds: 10 # 间隔时间
  successThreshold: 1
  timeoutSeconds: 1

探测方法

1. ExecAction

在容器内部执行一个命令,如果返回值为 0,则任务容器时健康的。

livenessProbe:
  exec:
    command:
      - cat
      - /health

2. TCPSocketAction

通过 tcp 连接监测容器内端口是否开放,如果开放则证明该容器健康

livenessProbe:
  tcpSocket:
    port: 80

3. HTTPGetAction

生产环境用的较多的方式,发送 HTTP 请求到容器内的应用程序,如果接口返回的状态码在 200~400 之间,则认为容器健康。

livenessProbe:
  failureThreshold: 5
  httpGet:
    path: /health
    port: 8080
    scheme: HTTP
    httpHeaders:
      - name: xxx
        value: xxx

参数配置

  • initialDelaySeconds: 60 # 初始化时间
  • timeoutSeconds: 2 # 超时时间
  • periodSeconds: 5 # 监测间隔时间
  • successThreshold: 1 # 检查 1 次成功就表示成功
  • failureThreshold: 2 # 监测失败 2 次就表示失败