JDev
JDev

Reputation: 1832

In HAProxy, Can I switch backends on the basis of health checks?

Consider we have two backends say backend_1 and backend_2.

I have a requirement to always move traffic to backend_1 and use backend_2 only if backend_1 servers health checks fail.

Is it possible in HAProxy?

Upvotes: 0

Views: 1117

Answers (1)

tbielaszewski
tbielaszewski

Reputation: 1094

In haproxy keyword backend is a group of servers. So solution depends on what you need.
If you mean backend in general sense, as servers then backup keyword is the answer:

frontend foo
  bind *:80
  default_backend bar

backend bar
  option allbackups
  server bar1 10.0.0.1:80 check
  server bar2 10.0.0.2:80 check
  server bar3 10.0.1.1:80 check backup
  server bar4 10.0.1.2:80 check backup

I added 4 servers to backend and option allbackups to make it behave similar to the next example.

If you mean backend in haproxy sense as group of servers then nbsrv will be useful:

frontend front
  bind *:80
  acl is_foo_alive nbsrv(foo) -m int gt 0
  use_backend foo if is_foo_alive
  use_backend bar

backend foo
  server foo1 10.0.0.1:80 check
  server foo2 10.0.0.2:80 check

backend bar
  server bar1 10.0.1.1:80 check
  server bar2 10.0.1.2:80 check

nbsrc(foo) tells us how many servers are up in backend foo, so if we get more than 0, then we use backend foo. If not, we use next rule, which is use_backend bar.

Those solutions are similar, but may differ depending on how backends differ (e.g. manipulation of HTTP headers, HTTP authentication, etc.) and whether you use session stickiness.

Upvotes: 2

Related Questions