跳转至

Pod 生命周期与健康检查

Pod 是短暂运行单元。Phase 只有 Pending、Running、Succeeded、Failed、Unknown 等粗粒度状态;Running 只说明 Pod 已绑定节点且至少一个容器运行或启动中,不代表业务已经可用。

容器状态与重启策略

kubectl get pod <pod> -n <namespace> -o wide
kubectl describe pod <pod> -n <namespace>
kubectl get pod <pod> -n <namespace> -o jsonpath='{.status.containerStatuses}'

容器状态包括 Waiting、Running、Terminated。排障重点查看 Waiting Reason、上次 Terminated Exit Code、Signal、OOMKilled 和 Restart Count。

三类 Probe

Probe 回答的问题 失败结果
startupProbe 应用是否完成慢启动 未成功前屏蔽 liveness/readiness;持续失败则重启容器
readinessProbe 当前是否可以接收流量 从 Service EndpointSlice 移除,不重启容器
livenessProbe 进程是否已经无法自我恢复 kubelet 重启容器
startupProbe:
  httpGet:
    path: /actuator/health/liveness
    port: 8080
  periodSeconds: 5
  failureThreshold: 30

readinessProbe:
  httpGet:
    path: /actuator/health/readiness
    port: 8080
  periodSeconds: 5
  timeoutSeconds: 2
  failureThreshold: 3

livenessProbe:
  httpGet:
    path: /actuator/health/liveness
    port: 8080
  periodSeconds: 10
  timeoutSeconds: 2
  failureThreshold: 3

不要把数据库、Redis 等短暂外部依赖失败直接作为 liveness 失败,否则依赖故障会造成全体 Pod 重启风暴。Readiness 可以体现暂时不可接流量,Liveness 只判断应用自身是否无法恢复。

初始化容器

Init Container 在业务容器前顺序执行,全部成功后业务容器才启动。适合权限准备、配置生成或等待必要条件;不适合无限循环等待外部服务。

kubectl logs <pod> -n <namespace> -c <init-container>
kubectl describe pod <pod> -n <namespace>

优雅终止

Pod 删除时,kubelet 执行 preStop(如配置),向容器主进程发送 SIGTERM,等待 terminationGracePeriodSeconds,最后才 SIGKILL。

terminationGracePeriodSeconds: 60
containers:
  - name: app
    lifecycle:
      preStop:
        exec:
          command: ["sh", "-c", "sleep 10"]

应用必须让 PID 1 正确接收 SIGTERM,停止接收新请求、完成在途请求并关闭连接。Grace Period 应大于应用真实优雅退出时间,但不能无限延长发布和节点维护。

CrashLoopBackOff 排查

kubectl describe pod <pod> -n <namespace>
kubectl logs <pod> -n <namespace> -c <container> --previous
kubectl get pod <pod> -n <namespace> \
  -o jsonpath='{.status.containerStatuses[0].lastState.terminated}'

优先区分:入口命令错误、配置缺失、权限、依赖不可用、Probe 误杀、Java OOM、容器内存 OOMKilled。BackOff 是重启退避现象,不是根因。

Pod Condition

常见 Condition 包括 PodScheduled、Initialized、ContainersReady、Ready。按顺序判断能快速定位:未调度、Init 未完成、容器未就绪、或自定义 Readiness Gate 未满足。

官方参考:Pod LifecycleConfigure Probes