如何在多个后端之间共享健康检查?

How to share health checks between multiple backends?

我有两个后端以便在我的应用程序中维护两套规则。一个用于通过浏览器访问的主要网站(cookie 会话),另一个用于 API(无状态并通过 API 密钥进行身份验证)。

配置如下所示(大大简化)。

defaults
    option httpchk HEAD /ping

backend web
    cookie _srv insert indirect
    server srv1 10.0.0.1:80 cookie srv1 check inter 10s
    server srv2 10.0.0.2:80 cookie srv2 check inter 10s

backend api
    stick-table type string len 50 expire 1d store http_req_cnt
    stick on url_param(key)
    server srv1 10.0.0.1:80 check inter 10s
    server srv2 10.0.0.2:80 check inter 10s

如图所示,两个后端使用相同的服务器,只是 API 不需要设置 cookie。有(或将有)更多的东西来分离逻辑,例如速率限制规则。

我的问题是如何避免重复健康检查?为每个服务器 ping 同一个端点两次是浪费资源。我想让 web 后端进行健康检查,并将 api 后端与其一起标记为启动或关闭。这可能吗?或者也许有更好的方法来分离后端但保留通用功能。

track [<proxy>/]<server>

This option enables ability to set the current state of the server by tracking another one. It is possible to track a server which itself tracks another server, provided that at the end of the chain, a server has health checks enabled. If is omitted the current one is used. If disable-on-404 is used, it has to be enabled on both proxies.

http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#5.2-track

这在 server 行中。

backend api
    server srv1 10.0.0.1:80 track web/srv1
    server srv2 10.0.0.2:80 track web/srv2

这是1.6的,以后的版本应该是一样的。 该功能也存在于 1.5 和 1.4 中。它可能存在于以前的版本中,但是那些没有维护,你不应该使用它们。