Consul 的健康检查机制是确保服务可用性的关键功能,它通过多种检查方式监控服务状态,并在服务不可用时及时通知。
健康检查类型
1. Script 检查
通过执行脚本或命令来检查服务健康状态:
json{ "check": { "id": "script-check", "name": "Script Health Check", "args": ["/usr/local/bin/check_script.sh"], "interval": "10s", "timeout": "5s" } }
脚本返回 0 表示健康,非 0 表示不健康。
2. HTTP 检查
通过 HTTP 请求检查服务端点:
json{ "check": { "id": "http-check", "name": "HTTP Health Check", "http": "http://localhost:8080/health", "method": "GET", "header": { "Authorization": ["Bearer token"] }, "interval": "10s", "timeout": "5s", "tls_skip_verify": false } }
HTTP 状态码 2xx 表示健康,其他表示不健康。
3. TCP 检查
通过 TCP 连接检查服务端口:
json{ "check": { "id": "tcp-check", "name": "TCP Health Check", "tcp": "localhost:3306", "interval": "10s", "timeout": "5s" } }
成功建立连接表示健康。
4. gRPC 检查
通过 gRPC 调用检查服务:
json{ "check": { "id": "grpc-check", "name": "gRPC Health Check", "grpc": "localhost:9090", "grpc_use_tls": true, "interval": "10s", "timeout": "5s" } }
5. Docker 检查
检查 Docker 容器状态:
json{ "check": { "id": "docker-check", "name": "Docker Health Check", "docker_container_id": "abc123", "shell": "/bin/bash", "script": "curl -s http://localhost:8080/health", "interval": "10s" } }
6. TTL 检查
基于 TTL(Time To Live)的检查,服务需要定期更新状态:
json{ "check": { "id": "ttl-check", "name": "TTL Health Check", "ttl": "30s" } }
服务需要定期调用 API 更新状态:
bashcurl -X PUT http://localhost:8500/v1/agent/check/pass/ttl-check
健康检查状态
Consul 定义了以下健康状态:
- passing:健康,服务正常运行
- warning:警告,服务可能有问题但仍可用
- critical:严重,服务不可用
- maintenance:维护模式,服务暂时不可用
检查参数配置
核心参数
- interval:检查间隔时间,如 "10s"、"1m"
- timeout:检查超时时间
- failures_before_critical:连续失败多少次后标记为 critical
- successes_before_passing:连续成功多少次后标记为 passing
- deregister_critical_service_after:服务 critical 多久后自动注销
高级参数
json{ "check": { "id": "advanced-check", "name": "Advanced Health Check", "http": "http://localhost:8080/health", "interval": "10s", "timeout": "5s", "failures_before_critical": 3, "successes_before_passing": 2, "deregister_critical_service_after": "5m", "status": "passing", "notes": "Custom health check" } }
服务级健康检查
健康检查可以与服务注册绑定:
json{ "service": { "name": "web", "port": 8080, "check": { "id": "web-check", "http": "http://localhost:8080/health", "interval": "10s" } } }
多个健康检查
一个服务可以有多个健康检查:
json{ "service": { "name": "web", "port": 8080, "checks": [ { "id": "web-http", "http": "http://localhost:8080/health", "interval": "10s" }, { "id": "web-disk", "script": "/usr/local/bin/check_disk.sh", "interval": "30s" } ] } }
健康检查 API
查询健康检查
bash# 查询所有检查 curl http://localhost:8500/v1/health/state/any # 查询 passing 状态的检查 curl http://localhost:8500/v1/health/state/passing # 查询特定服务的检查 curl http://localhost:8500/v1/health/checks/web
手动更新检查状态
bash# 标记为 passing curl -X PUT http://localhost:8500/v1/agent/check/pass/ttl-check # 标记为 warning curl -X PUT http://localhost:8500/v1/agent/check/warn/ttl-check # 标记为 critical curl -X PUT http://localhost:8500/v1/agent/check/fail/ttl-check
最佳实践
- 合理设置检查间隔:太频繁会增加负载,太长会影响故障检测速度
- 设置适当的超时时间:避免因网络延迟导致误判
- 使用多个检查:从不同角度验证服务健康状态
- 配置故障阈值:避免因临时故障频繁切换状态
- 监控检查本身:确保健康检查机制正常工作
故障处理
当服务健康检查失败时:
- 自动注销:服务从服务列表中移除
- 负载均衡调整:流量不再路由到不健康的服务
- 告警通知:可以通过 Consul Watch 或外部监控系统触发告警
- 自动恢复:服务恢复后自动重新注册
Consul 的健康检查机制灵活且强大,能够满足各种场景下的服务监控需求。