We’ll cover the steps on how to Install HAProxy on Debian. Apart from showcasing the installation steps of HAProxy, we’re also going to explain basics of HAProxy, how it works and also how to configure HAProxy too.
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.
Other than that, HAProxy can also run as a ssl passthrough which is used to encrypt all the incoming traffic on your infrastructure which is covered in this post(using certbot and let’s encrypt ssl.) 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.
How to install HAProxy on Debian
HAProxy is located in the Debian based repositories so the installation process is pretty simple and in order to install HAProxy on Debian run the following commands:
sudo apt install haproxy sudo systemctl enable haproxy sudo systemctl start haproxy
How to configure HAProxy on Debian
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.
To summarize the article – we demonstrated the steps how to install HAProxy on Debian server and also covered the steps how to configure HAProxy in multiple working configurations in order to achieve High availability.
This was just couple of examples how it’s configured most commonly. If you’re also interested in how to add SSL to your HAProxy, that is covered on this post. Hope the post is useful to you.
Thank you for your time…