启动探针(Startup Probe)

启动探针是 K8s 1.16+ 引入的新探针,用于处理启动时间较长的应用。

为什么需要启动探针

某些应用(如 JVM 应用、大型 Spring Boot 应用)启动时间可能很长。如果存活探针的 initialDelaySeconds 设置太短,会误杀正在启动的应用;设置太长,又会延误故障检测。

启动探针解决了这个问题:

startup-probe.yaml
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: myapp
    image: myapp:v1
    startupProbe:
      httpGet:
        path: /health/started
        port: 8080
      initialDelaySeconds: 0
      periodSeconds: 5
      failureThreshold: 60  # 5s × 60 = 5 分钟
    livenessProbe:
      httpGet:
        path: /health/live
        port: 8080
      initialDelaySeconds: 0  # 启动探针结束后才生效
      periodSeconds: 10
      failureThreshold: 3
    readinessProbe:
      httpGet:
        path: /health/ready
        port: 8080
      initialDelaySeconds: 0  # 启动探针结束后才生效
      periodSeconds: 5
      failureThreshold: 3

探针执行顺序

flowchart TD
    A["容器启动"] --> B["启动探针检查"]
    B --> |"失败| C["继续检查\n(最多 failureThreshold 次)"]
    B --> |"成功| D["启动探针结束"]
    D --> E["存活探针开始检查"]
    D --> F["就绪探针开始检查"]

本章总结

核心要点

  1. 启动探针用于处理启动时间长的应用:给予足够的启动时间
  2. 启动探针结束后,存活和就绪探针才开始:避免误杀
  3. failureThreshold 决定最大启动时间:5s × 60 = 5 分钟