Tim
Tim

Reputation: 8606

How to share health checks between multiple backends?

I have two backends in order to maintain two sets of rules in my application. One for the main website accessed via the browser (cookied sessions), and one for the API (stateless and authenticated by an API key).

The config looks like this (greatly simplified).

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

As shown, both backends use the same servers, except that the API need not set a cookie. There is (or will be) much more to separate the logic, such as rate limiting rules.

My question is how can I avoid duplicating the health checks? It's waste of resources to ping the same endpoint twice for each server. I would like to let the web backend do the health check and mark the api backend as up or down along with it. Is this possible? Or perhaps there's a better way to separate the back ends but keep common functions.

Upvotes: 5

Views: 2665

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179084

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

This goes in the server line.

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

This is from 1.6, but later versions should be the same. The feature exists in 1.5 and 1.4, also. It may exist in previous versions, but those are unmaintained and you shouldn't be using them.

Upvotes: 5

Related Questions