Kubernetes学习之旅-健康检查
·
Kubernetes健康检查详解:保障应用稳定性的核心机制
什么是健康检查?
健康检查是Kubernetes确保应用持续可用性和可靠性的核心机制。它通过定期探测应用状态,自动处理故障实例,实现应用的自我修复和零停机部署。
为什么需要健康检查?
传统部署的痛点
应用进程存在但服务不可用
故障需要人工发现和干预
部署更新时可能中断服务
无法自动处理依赖服务准备状态
Kubernetes的解决方案
自动故障检测:持续监控应用健康状态
自我修复:自动重启故障容器
流量管理:只将流量路由到健康实例
部署安全:确保新版本完全就绪后再接收流量
健康检查的三种类型
1. Liveness Probe(存活探针)
作用:检测应用是否正常运行,失败时重启容器
yaml
apiVersion: v1 kind: Pod metadata: name: liveness-example spec: containers: - name: web-server image: nginx:1.21 livenessProbe: httpGet: path: /healthz port: 8080 httpHeaders: - name: Custom-Header value: Awesome initialDelaySeconds: 15 # 容器启动后等待时间 periodSeconds: 10 # 检查频率 timeoutSeconds: 5 # 超时时间 failureThreshold: 3 # 失败阈值 successThreshold: 1 # 成功阈值
2. Readiness Probe(就绪探针)
作用:检测应用是否准备好接收流量,失败时从Service端点移除
yaml
readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5 timeoutSeconds: 3 failureThreshold: 1 successThreshold: 1
3. Startup Probe(启动探针)
作用:检测应用是否成功启动,保护慢启动应用
yaml
startupProbe: httpGet: path: /healthz port: 8080 failureThreshold: 30 # 最多尝试30次 periodSeconds: 10 # 每10秒检查一次
健康检查的三种探测方式
1. HTTP GET探测
yaml
httpGet: path: /health port: 8080 scheme: HTTPS # 可选:HTTP或HTTPS httpHeaders: - name: Authorization value: Bearer token123
适用场景:Web服务、REST API
2. TCP Socket探测
yaml
tcpSocket: port: 9090
适用场景:数据库、缓存、非HTTP服务
3. Exec命令探测
yaml
exec: command: - cat - /tmp/healthy
适用场景:自定义健康检查逻辑、文件存在性检查
完整健康检查配置示例
yaml
apiVersion: v1 kind: Pod metadata: name: complete-health-check spec: containers: - name: app image: my-app:1.0.0 ports: - containerPort: 8080 # 启动探针:保护慢启动应用 startupProbe: httpGet: path: /healthz port: 8080 failureThreshold: 30 periodSeconds: 10 # 存活探针:检查应用是否存活 livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 30 # 等待启动探针完成 periodSeconds: 10 timeoutSeconds: 5 failureThreshold: 3 # 就绪探针:检查应用是否就绪 readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5 timeoutSeconds: 3 failureThreshold: 1 successThreshold: 1 # 资源限制 resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
健康检查参数详解
关键参数配置
| 参数 | 描述 | 默认值 | 建议 |
|---|---|---|---|
initialDelaySeconds |
容器启动后等待时间 | 0 | 根据应用启动时间设置 |
periodSeconds |
检查频率 | 10 | 根据业务敏感性调整 |
timeoutSeconds |
探测超时时间 | 1 | 设置合理的超时时间 |
failureThreshold |
失败阈值 | 3 | 根据网络稳定性调整 |
successThreshold |
成功阈值 | 1 | 就绪探针可设置为2 |
参数配置策略
yaml
# 快速失败型应用 livenessProbe: periodSeconds: 5 failureThreshold: 1 timeoutSeconds: 2 # 稳定型应用 livenessProbe: periodSeconds: 30 failureThreshold: 3 timeoutSeconds: 10 # 网络敏感型应用 livenessProbe: periodSeconds: 10 failureThreshold: 5 # 更高的容错 timeoutSeconds: 3
实际应用场景
Web服务健康检查
yaml
livenessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 5
数据库健康检查
yaml
livenessProbe: exec: command: - /bin/sh - -c - 'pg_isready -h localhost -p 5432' initialDelaySeconds: 60 periodSeconds: 30 readinessProbe: tcpSocket: port: 5432 initialDelaySeconds: 5 periodSeconds: 10
缓存服务健康检查
yaml
livenessProbe: exec: command: - redis-cli - ping initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: tcpSocket: port: 6379 initialDelaySeconds: 5 periodSeconds: 5
最佳实践指南
1. 分层健康检查设计
yaml
# 轻量级存活检查 livenessProbe: httpGet: path: /health/liveness # 只检查基础功能 port: 8080 periodSeconds: 30 # 全面就绪检查 readinessProbe: httpGet: path: /health/readiness # 检查所有依赖 port: 8080 periodSeconds: 5
2. 优雅终止配置
yaml
# 配合preStop钩子实现优雅终止 lifecycle: preStop: exec: command: ["/bin/sh", "-c", "sleep 30; nginx -s quit"]
3. 资源隔离配置
yaml
# 为健康检查分配独立资源 resources: requests: memory: "32Mi" cpu: "10m" limits: memory: "64Mi" cpu: "50m"
调试与故障排除
常用诊断命令
bash
# 查看Pod状态和事件 kubectl describe pod/my-pod # 查看容器重启历史 kubectl get pod/my-pod -o jsonpath='{.status.containerStatuses[0].restartCount}' # 检查健康检查配置 kubectl get pod/my-pod -o jsonpath='{.spec.containers[0].livenessProbe}' # 查看容器日志 kubectl logs my-pod --previous # 查看前一个容器的日志
总结
Kubernetes健康检查是保障应用稳定性的关键机制:
三层探测机制:启动、存活、就绪探针各司其职
多种探测方式:HTTP、TCP、Exec满足不同场景需求
自我修复能力:自动重启故障容器,确保应用可用性
智能流量管理:只将流量路由到健康实例
部署安全保障:支持零停机部署和滚动更新
更多推荐


所有评论(0)