Search code examples
load-balancinghaproxyhigh-availabilityfailover

HaProxy - group tcp and http hosts dependent of each other


I have the following scenario:

Haproxy is running in front of my two groups of servers:

  • two http servers (active / backup)
  • two tcp servers (active / backup)

I now want to fail over from the active sides to the backup ones of ANY of the active services goes down (fail over HTTP and TCP at the same time).

Is there any way to do so in HAproxy? I so far was only able to fail over to one of them depending on the protocol but not both. Can these be grouped?

i was wondering if the can be done via ACLs and things like the fe_conn directive


Solution

  • I think haproxy's nbsrv works here. If your nbsrv count, number of healthy instances, falls below desired amount on EITHER pool switch both pools to the backup backend. Otherwise just use the default pool. Here is an example verified on 1.5.18 but should work fine on newer versions:

    defaults all
      timeout connect 30s
      timeout client 30s
      timeout server 30s
      mode http
    
    # http frontend
    frontend http *:80
      # use the backup service if EITHER service is down
      acl use_backup nbsrv(http_service) lt 1
      acl use_backup nbsrv(tcp_service) lt 1
      use_backend http_service_backup if use_backup
      default_backend http_service
    
    # tcp frontend
    frontend tcp_10000 *:10000
      mode tcp
      # use the backup service if EITHER service is down
      acl use_backup nbsrv(http_service) lt 1
      acl use_backup nbsrv(tcp_service) lt 1
      use_backend tcp_service_backup if use_backup
      default_backend tcp_service
    
    backend tcp_service
      mode tcp
      # main tcp instance here
      # can also include backup server here with backup directive if desired
      server tcp-service1 tcp-service1.local:10000 check
    
    backend tcp_service_backup
      mode tcp
      # backup tcp instance here
      server tcp-service2 tcp-service2.local:10000 check
    
    backend http_service
      # main http instance here
      # can also include backup server here with backup directive if desired
      server http-service1 http-service1.local:80 check
    
    backend http_service_backup
      # backup http instance here
      server http-service2 http-service2.local:80 check    
    

    See https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#nbsrv for more nbsrv details.