Upstream的健康检查

APISIX的健康检查使用lua-resty-healthcheck实现,你可以在upstream中使用它。

下面是一个检查检查的例子:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/index.html",
  4. "plugins": {
  5. "limit-count": {
  6. "count": 2,
  7. "time_window": 60,
  8. "rejected_code": 503,
  9. "key": "remote_addr"
  10. }
  11. },
  12. "upstream": {
  13. "nodes": {
  14. "127.0.0.1:1980": 1,
  15. "127.0.0.1:1970": 1
  16. },
  17. "type": "roundrobin",
  18. "retries": 2,
  19. "checks": {
  20. "active": {
  21. "http_path": "/status",
  22. "host": "foo.com",
  23. "healthy": {
  24. "interval": 2,
  25. "successes": 1
  26. },
  27. "unhealthy": {
  28. "interval": 1,
  29. "http_failures": 2
  30. },
  31. "req_headers": ["User-Agent: curl/7.29.0"]
  32. },
  33. "passive": {
  34. "healthy": {
  35. "http_statuses": [200, 201],
  36. "successes": 3
  37. },
  38. "unhealthy": {
  39. "http_statuses": [500],
  40. "http_failures": 3,
  41. "tcp_failures": 3
  42. }
  43. }
  44. }
  45. }
  46. }'

监控检查的配置内容在checks中,checks包含两个类型:activepassive,详情如下

  • active: 要启动探活健康检查,需要在upstream配置中的 checks.active 添加如下配置项。

    • active.http_path: 用于发现upstream节点健康可用的HTTP GET请求路径。
    • active.host: 用于发现upstream节点健康可用的HTTP请求主机名。

    healthy的阀值字段:

    • active.healthy.interval: 健康的目标节点的健康检查间隔时间(以秒为单位),最小值为1。
    • active.healthy.successes: 确定目标是否健康的成功次数,最小值为1。

    unhealthy的阀值字段:

    • active.unhealthy.interval: 针对不健康目标节点的健康检查之间的间隔(以秒为单位),最小值为1。
    • active.unhealthy.http_failures: 确定目标节点不健康的http请求失败次数,最小值为1。
    • active.req_headers: 其他请求标头。数组格式,可以填写多个标题。
  • passive: 要启用被动健康检查,需要在upstream配置中的 checks.passive 添加如下配置项。

    healthy的阀值字段:

    • passive.healthy.http_statuses: 如果当前HTTP响应状态码是其中任何一个,则将upstream节点设置为 healthy 状态。否则,请忽略此请求。
    • passive.healthy.successes: 如果upstream节点被检测成功(由 passive.healthy.http_statuses 定义)的次数超过 successes 次,则将该节点设置为 healthy 状态。

    unhealthy的阀值字段:

    • passive.unhealthy.http_statuses: 如果当前HTTP响应状态码是其中任何一个,则将upstream节点设置为 unhealthy 状态。否则,请忽略此请求。
    • passive.unhealthy.tcp_failures: 如果TCP通讯失败次数超过 tcp_failures 次,则将upstream节点设置为 unhealthy 状态。
    • passive.unhealthy.timeouts: 如果被动健康检查超时次数超过 timeouts 次,则将upstream节点设置为 unhealthy 状态。
    • passive.unhealthy.http_failures: 如果被动健康检查的HTTP请求失败(由 passive.unhealthy.http_statuses 定义)的次数超过 http_failures次,则将upstream节点设置为 unhealthy 状态。