HAProxy - use_backend 如果可用

bla*_*ist 4 haproxy

有没有办法使用use_backendACL 匹配,但是,如果后端不可用(停机、维护等),则使用默认值?

例如:

    # 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
Run Code Online (Sandbox Code Playgroud)

在上述情况下,如果培根和奶昔后端没有可用的服务器,是否会使用 web-app-cluster?

谢谢

nba*_*ari 5

是的,这是可能的,例如,你可以使用这样的东西:

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
...
Run Code Online (Sandbox Code Playgroud)

注意使用 nbsrv([<backend>]) : integer

文档

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".
Run Code Online (Sandbox Code Playgroud)

在这篇 HAproxy 帖子中查看更多示例:故障转移和最坏情况管理与 HAProxy