完整教程:Kubernetes 健康探针详解

在 Kubernetes 中,健康探针是确保应用高可用性和可靠性的关键组件。通过三种不同类型的探针,Kubernetes 能够智能地管理容器的生命周期,确保流量只被路由到准备就绪的容器,并在应用出现故障时自动恢复。

探针工作机制概述

健康探针通过定期执行诊断检查来监控容器的状态,每种探针都有特定的作用和执行时机:

apiVersion: v1
kind: Pod
metadata:
  name: health-check-demo
spec:
  containers:
  - name: app
    image: my-app:latest
    ports:
    - containerPort: 8080
    startupProbe:
      httpGet:
        path: /health-startup
        port: 8080
      failureThreshold: 30
      periodSeconds: 10
    livenessProbe:
      httpGet:
        path: /health-live
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
    readinessProbe:
      httpGet:
        path: /health-ready
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 5

三种探针的详细解析

Startup Probe(启动探针)

启动探针用于检测应用程序何时完成启动过程,特别适用于启动时间较长的应用。

startupProbe:
  httpGet:
    path: /health-startup
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5
  failureThreshold: 30
  successThreshold: 1
  timeoutSeconds: 1
关键特性
  • 目的:确定容器应用是否已成功启动
  • 适用场景:启动缓慢的应用程序(如Java应用、大型单体应用)
  • 工作方式:在启动期间禁用其他探针,直到启动成功
  • 失败后果:容器被杀掉并重启
  • 建议配置:设置较长的 failureThreshold 和适当的 periodSeconds

❤️ Liveness Probe(存活探针)

存活探针用于检测容器是否仍在正常运行,如果检查失败,容器将被重启。

livenessProbe:
  httpGet:
    path: /health-live
    port: 8080
    httpHeaders:
    - name: Custom-Header
      value: Awesome
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3
  successThreshold: 1
检查方式
  • HTTP GET:对指定的路径和端口执行HTTP请求
  • TCP Socket:尝试建立TCP连接
  • Exec:在容器内执行指定命令

✅ Readiness Probe(就绪探针)

就绪探针确定容器是否已准备好接收流量,只有当所有容器都就绪时,Pod才会被添加到服务端点。

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
  initialDelaySeconds: 5
  periodSeconds: 5
  failureThreshold: 1
  successThreshold: 1
关键特性
  • 目的:检测容器是否准备好服务请求
  • 失败后果:从服务的端点列表中移除,停止接收流量
  • 适用场景:应用需要加载大量数据、依赖外部服务初始化等

探针配置参数详解

每种探针都支持相同的配置参数,用于精确控制检查行为:

参数说明默认值
initialDelaySeconds容器启动后到开始第一次检查的等待时间0
periodSeconds执行检查的频率(秒)10
timeoutSeconds检查超时时间1
failureThreshold连续失败多少次后认为检查失败3
successThreshold连续成功多少次后认为检查成功1

实际应用场景

场景1:Web 应用健康检查

apiVersion: v1
kind: Pod
metadata:
  name: web-application
spec:
  containers:
  - name: web
    image: nginx:latest
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /healthz
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 5
    readinessProbe:
      httpGet:
        path: /readyz
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 5

场景2:慢启动应用

apiVersion: v1
kind: Pod
metadata:
  name: slow-start-app
spec:
  containers:
  - name: java-app
    image: my-java-app:latest
    startupProbe:
      httpGet:
        path: /health
        port: 8080
      failureThreshold: 30
      periodSeconds: 10
    livenessProbe:
      httpGet:
        path: /health
        port: 8080
      periodSeconds: 5
    readinessProbe:
      httpGet:
        path: /ready
        port: 8080
      periodSeconds: 5

最佳实践

1. 合理的配置策略
livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 15    # 给应用足够的启动时间
  periodSeconds: 10          # 合理的检查频率
  timeoutSeconds: 3          # 适当的超时时间
  failureThreshold: 3        # 避免因瞬时故障重启
2. 探针端点设计
  • 使用轻量级的健康检查端点
  • 避免在探针检查中执行复杂操作
  • 确保健康检查端点不依赖外部服务
  • 为不同的探针使用不同的端点

故障排查技巧

常见问题及解决方案

1. 容器频繁重启

# 查看容器重启历史
kubectl describe pod 
# 检查存活探针配置
kubectl get pod  -o yaml | grep -A 10 livenessProbe

2. Pod 未就绪

# 检查就绪探针状态
kubectl describe pod 
# 手动测试就绪端点
kubectl exec  -- curl http://localhost:8080/health

总结

Kubernetes 的健康探针机制为容器化应用提供了强大的自我修复和流量管理能力:

  • Startup Probe:保护慢启动应用,避免在启动期间被误杀
  • Liveness Probe:确保故障应用能够自动恢复
  • Readiness Probe:保证流量只被路由到准备就绪的应用

合理配置这三种探针,可以显著提高应用的可用性和可靠性,是生产环境部署的必备实践。

posted on 2025-10-25 18:47  slgkaifa  阅读(5)  评论(0)    收藏  举报

导航