跳到主要内容
版本:3.10.x

配置上游健康检查

健康检查对于维护服务可用性至关重要。通过监控上游节点的健康状态,网关可以自动将不健康节点从负载均衡池中移除,并仅将流量重定向到健康节点。

API7 企业版支持两种健康检查:

  • 主动健康检查:网关会定期向上游节点发送探测请求,以验证其状态。
  • 被动健康检查:网关会监控真实客户端请求的响应,以检测故障。

前提条件

  • API7 企业版实例正在运行。
  • 已创建网关组,并且网关实例正在运行。
  • Dashboard Token

启动示例上游服务

为了进行本地验证,请在主机上启动两个示例上游服务:

docker run -d --name tm-health-1 -p 18083:80 kennethreitz/httpbin
docker run -d --name tm-health-2 -p 18084:80 kennethreitz/httpbin

以下示例假设网关可以通过 192.168.215.1 访问主机。

工作流概览

网关会发送主动探测并监控真实流量,以检测不健康节点。流量只会被路由到标记为健康的节点。

配置主动健康检查

主动健康检查允许网关即使在没有客户端流量时也能检测故障。

# 1. 创建包含两个上游节点和主动健康检查的服务
curl -k "https://localhost:7443/apisix/admin/services/upstream-with-health-check?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "upstream-with-health-check",
"upstream": {
"type": "roundrobin",
"nodes": [
{
"host": "192.168.215.1",
"port": 18083,
"weight": 100
},
{
"host": "192.168.215.1",
"port": 18084,
"weight": 100
}
],
"checks": {
"active": {
"type": "http",
"http_path": "/status/200",
"timeout": 1,
"concurrency": 10,
"healthy": {
"interval": 2,
"successes": 1,
"http_statuses": [200]
},
"unhealthy": {
"interval": 1,
"tcp_failures": 2,
"timeouts": 3,
"http_failures": 3,
"http_statuses": [429, 404, 500, 502, 503, 504]
}
}
}
}
}'

# 2. 创建用于验证流量的路由
curl -k "https://localhost:7443/apisix/admin/routes/health-check-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "health-check-route",
"methods": ["GET"],
"paths": ["/status/200"],
"service_id": "upstream-with-health-check"
}'

配置被动健康检查

被动健康检查通过监控真实流量模式来补充主动健康检查,用于检测主动探测可能遗漏的故障。

# 更新服务,为主动健康检查添加被动健康检查
curl -k "https://localhost:7443/apisix/admin/services/upstream-with-health-check?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "upstream-with-health-check",
"upstream": {
"type": "roundrobin",
"nodes": [
{
"host": "192.168.215.1",
"port": 18083,
"weight": 100
},
{
"host": "192.168.215.1",
"port": 18084,
"weight": 100
}
],
"checks": {
"active": {
"type": "http",
"http_path": "/status/200",
"healthy": {
"interval": 30,
"successes": 1,
"http_statuses": [200]
},
"unhealthy": {
"interval": 30,
"http_failures": 3,
"tcp_failures": 2,
"timeouts": 3,
"http_statuses": [429, 404, 500, 502, 503, 504]
}
},
"passive": {
"healthy": {
"http_statuses": [200, 201],
"successes": 1
},
"unhealthy": {
"http_statuses": [404, 500, 502, 503],
"http_failures": 3,
"tcp_failures": 2,
"timeouts": 3
}
}
}
}
}'

被动健康检查是响应式的,需要一定量的客户端流量才能检测故障。

组合主动和被动健康检查

为了获得最高可靠性,建议同时使用主动和被动健康检查。

验证配置

  1. 模拟故障:停止某个后端上游节点,例如 docker stop tm-health-1
  2. 观察网关:等待健康检查根据配置的阈值检测到故障。
  3. 检查可用性:向路由发送请求。只要至少一个节点健康,网关就应继续返回 200 OK
  4. 检查健康状态:从网关容器中查询 Control API:
docker exec gateway sh -lc 'curl -s http://127.0.0.1:9090/v1/healthcheck || wget -qO- http://127.0.0.1:9090/v1/healthcheck'

在本地验证环境中,健康状态会在上游路径下报告,例如 /apisix/upstreams/upstream-with-health-check

while true; do curl -s -o /dev/null -w '%{http_code}\n' "http://127.0.0.1:9080/status/200"; sleep 1; done

故障排除

  • 健康检查未生效:检查后端是否存在 http_path,并确认 hostport 配置正确。
  • 防火墙问题:确保网关节点有权限访问上游节点的健康检查端点。
  • 等待时间:如果节点没有立即被移除,请检查 intervalhttp_failures/successes 设置。
  • DNS 问题:如果上游节点使用主机名,请确保网关节点可以解析这些主机名。

后续步骤