代码之家  ›  专栏  ›  技术社区  ›  bladefist

haproxy-如果可用,请使用后端

  •  3
  • bladefist  · 技术社区  · 7 年前

    有没有办法利用 use_backend 与ACL匹配,但如果后端不可用(down、maint等),则使用默认值?

    例如:

        # 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
    

    在上面的例子中,如果bacon和milkshake的后端没有可用的服务器,那么要使用web app集群吗?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   nbari    7 年前

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

    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
    ...
    

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

    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".
    

    检查此haproxy post中的更多示例: failover and worst case management with HAProxy