HAProxy - use_backend if it's available
Asked Answered
S

1

6

Is there a way to utilize use_backend with an ACL match, but, in the case the backend is unavailable (down,maint,etc), then use the default?

For example:

    # Define hosts
    acl host_bacon hdr(host) -i ilovebacon.com
    acl host_milkshakes hdr(host) -i bobsmilkshakes.com

    ## figure out which one to use
    use_backend bacon_cluster if host_bacon
    use_backend milshake_cluster if host_milkshakes
    default_backend web-app-cluster

In the case above, if the bacon and milkshake backends have no available servers, to fall and use web-app-cluster?

Thanks

Spartan answered 19/7, 2018 at 17:20 Comment(0)
L
7

Yes, it is possible, for example, you could use something like this:

acl host_bacon hdr(host) -i ilovebacon.com
acl host_milkshakes hdr(host) -i bobsmilkshakes.com

# check if bacon & milk ok
acl bacon_cluster_down nbsrv(bacon_cluster) lt 1 
acl milks_cluster_down nbsrv(milshake_cluster) lt 1 

# use default web-app if backon & milk down
use_backend web-app-cluster if bacon_cluster_down
use_backend web-app-cluster if milks_cluster_down

use_backend bacon_cluster if host_bacon
use_backend milshake_cluster if host_milkshakes

default_backend web-app-cluster
...

Notice the use of nbsrv([<backend>]) : integer

From the docs:

Returns an integer value corresponding to the number of usable servers of
either the current backend or the named backend. This is mostly used with
ACLs but can also be useful when added to logs. This is normally used to
switch to an alternate backend when the number of servers is too low to
to handle some load. It is useful to report a failure when combined with
"monitor fail".

Check for more examples in this HAproxy post: failover and worst case management with HAProxy

Layard answered 22/7, 2018 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.