HAProxy Load balancer configuration
HAProxy Load balancer configuration
We’ll cover the HAProxy load balancer configuration. HAProxy can run with round-robin instructions and with in failover mode, but also it can run with both at the same time, assuring the HA(High Availability). Aside running HAProxy in round-robin mode only, some HA scenarios require failover where you want to have a backup server or servers for the main servers which serve a website or an app for a more reliable HA environment and with easier maintenance. In this post, the instructions and HAProxy configuration are covered for scenarios where you install HAProxy directly on a server(bare metal or VM), in other words – not in a container.
Round-robin algorithm is the set of instructions to divide the traffic equally(in this example), meaning that, if you have two servers – both servers will receive traffic equally or in other words, round-robin will make sure that both servers get equal amount of traffic and that way making sure that servers nor the bandwidth are not overloaded.
Like mentioned earlier, in failover mode, you assign to HAProxy which server will be the primary server, which is going to receive traffic and also a backup server which is going to takeover the receiving traffic if the primary server goes down. Meaning, when the primary server goes down, the backup server takes over and replaces the primary server until it comes back online thus reducing the downtime to a minimum.
This is also useful when server maintenance is needed to be done, so when you run update & upgrade on one server or it needs to be rebooted, the other server will be up and running and will takeover the role as a primary server and continue to serve the site/app.
HAProxy load balancer configuration
The IP addresses in the configuration bellow are used as an example. The configuration is pretty straight-forward. In the HAProxy cfg file add configuration like this right bellow the default configuration:
frontend haproxy bind 10.0.51.119:80 mode http default_backend test_site backend test_site mode http server testserver1 10.0.51.121:80 check inter 5s rise 3 fall 2 server testserver2 10.0.51.120:80 check backup inter 5s rise 3 fall 2
Frontend part of the configuration is the HAProxy. Bind HAProxy with it’s IP address and port number(service port). In this example, we are using port 80 for HTTP. If you’re HAProxy is exposed publicly on the internet, the use the public IP address of HAProxy.
Define for haproxy frontend that it has a backend, where we configure and add servers.
In this example, we added two servers for the haproxy backend. Testserver 1 is the primary server and the testserver2 is the backup server. We’ll also clarify the added parameters on the servers:
server testserver1 10.0.51.121:80 check inter 5s rise 3 fall 2 server testserver2 10.0.51.120:80 check backup inter 5s rise 3 fall 2
On both servers, we added the server online check intervals. For the testserver1, if it becomes unresponsive after 5 seconds with 3 checks(3 check at the 5 second interval), it will be flagged as offline and backup server will takeover. If the testserver1 comes back online, it will takeover after 2 checks.
The same check parameters are added for the backup server.
HAProxy has the round-robin instructions already integrated within, so in the backed part of the config it’s only necessary to add the config line – balance roundrobin.
Like in the configuration bellow:
frontend haproxy bind 10.0.51.119:80 mode http default_backend test_site backend test_site balance roundrobin mode http server testserver1 10.0.51.121:80 check inter 5s rise 3 fall 2 server testserver2 10.0.51.120:80 check backup inter 5s rise 3 fall 2
Failover + round-robin
We mentioned that HAProxy can be configured with round-robin algorithm and to have a failover server as well. For this setup it’s recommended to have more servers, the primary servers on which to load balance the incoming traffic and a separate failover(backup) server to takeover if the primary servers go down. The configuration for this kind of setup would like something like this:
backend test_site balance roundrobin mode http server testserver1 10.0.51.121:80 check inter 5s rise 3 fall 2 server testserver2 10.0.51.120:80 check inter 5s rise 3 fall 2 server testserver3 10.0.51.121:80 check backup inter 5s rise 3 fall 2
So, in this config, we added a third server, which we assigned in HAProxy to act as a backup server, while the first two will act as primary servers and HAProxy will load balance traffic on them.
This is was the HAProxy load balancer configuration. This was just couple of examples how to configure it. Hope the post is useful to you.
Thank you for your time…