Haproxy failover single backup server for several nodes

I have several small server and one big server that contain mirror of that several small server. Let’s call it Server A, Server B, Server C and Server D for group of small server, and the big server will have Server X as name.

I’m drawing this diagram to explain the setup.

I want to redirect user to Server X if one of Server A to D is down/unreachable, once the server is gone up the user would have been redirected back to their corresponding server. Below is an example of desired scenario.

  • John search up www.example.com hosted on Server B.
  • Haproxy redirect John to Server B.
  • John is doing something on www.example.com on server B.
  • Server B is down ! John got redirected to Server X.
  • John is doing something on www.example.com on server X.
  • Server B is up ! John got redirected back to server B from Server X.

I want to do the same setup with all other server (Server A to D), hence I only needed a failover and not load balancer.

I’m pretty sure this is possible with haproxy, but I haven’t find a way to do this setup. Can anyone who knowledgeable give me a way to perform this setup.

I hope my point is clear, TIA

Anyone can help ?


In your backend servers definition, you can use Server X as backup :

backend backend-A
    server server-A server-a:80 check
    server server-X server-x:80 check backup

This way, if server A goes down, server X wil be used. When server A goes UP, requests will be sent to it.

What about another server ? Do I need to create different backend for each server ?

hello @Liso ,
I think you need to create two backends , one with the four servers ( A-D ) and one with only server X inside the backend blocks like so:

backend fourofthem
    mode http
    balance roundrobin
    server A ip-for-A check
    server B ip-for-B check
    server C ip-for-C check
    server D ip-for-D check

backend justx
    mode http
    balance roundrobin
    server X ip-for-X check

Now , in your frontend you may specify an acl that checks for the number of servers in backend “fourofthem” and redirect your traffic to backend “justx” if the number of servers is less than four
and justx is alive.

The frontend could look like this:

frontend thefrontend
    acl atleastonedown nbsrv(fourofthem) lt 4
    acl x_is_still_alive nbsrv(justx) ge 1
    use_backend justx if atleastonedown x_is_still_alive
    default_backend fourofthem

As soon as the one backend of “fourofthem” goes down the first acl will return true and if justx is alive as well , it will get the traffic. Otherwise all traffic will end in your default backend “fourofthem”.

For reference nbsrv() receives a string input argument, which is the backend name and returns the number of alive servers in that backend.

I hope this solves your problem

don’t think that this will work. as soon one of the four servers is down ALL traffic will be routed to serverX.
you can setup a backup server. i am not sure if the backup server will receive requests as soon one of the main servers is down. and i am not sure if the stickyness will route the requests back to the original server (user-request to serverB, serverB is down, user-request to serverX, as soon as serveB is up again the user is back on serverB), i could image that then the load-balancing routing is done new. maybe you have to check this with a sample setup?

I misunderstood the goal, i thought @Liso wanted to send all traffic to X if any one server failed.

I that case then in my example if we add all 4 servers AND server X in justx backend it will work as expected.

If any server in the default backend fails , the frontend will send the traffic to justx backend.
server B would be failed, so checks for it fail , so all requests are servers by A C D and X.

backend justx
    mode http
    balance roundrobin
    server X ip-for-X check
    server A ip-for-A check
    server B ip-for-B check
    server C ip-for-C check
    server D ip-for-D check