HAProxy is a free & open source solution for High availability and load balancing, it can also be used for proxying TCP & HTTP based applications. HAProxy can be installed and configured on Linux, Solaris & FreeBSD. HAProxy is best recommended solution for the websites which has huge traffic as it improves performance & reliability of the server by means of load balancing the servers & using its high availability capabilities
HAProxy is used by a number of most popular websites including GitHub, Bitbucket, Stack Overflow, Reddit, Tumblr, Twitter and it is also used in the OpsWorks product from Amazon Web Services.In this tutorial, we will learn to install & use Haproxy on Debian 9 & also for Ubuntu 16.04 server. We will first start with the installation of Haproxy on Debian 9,
Installation of HAProxy on Debian 9
Currently the latest stable version of haproxy supported on Debian 9 is 1.8, we will be installing that only. Firstly we need to enable the backports repository on Debian 9, to do so use the following command,
[root@localhost ~]# echo deb http://httpredir.debian.org/debian stretch-backports main | tee /etc/apt/sources.list.d/backports.list
Once its done, we will enable a dedicated repository with the following command,
[root@localhost ~]# curl https://haproxy.debian.net/bernat.debian.org.gpg | apt-key add - [root@localhost ~]# echo deb http://haproxy.debian.net stretch-backports-1.8 main | tee /etc/apt/sources.list.d/haproxy.list
Now we can install haproxy using the repositories added above,
[root@localhost ~]# apt-get update [root@localhost ~]# apt-get install haproxy -t stretch-backports\*
This will install the haproxy on the Debian 9 system, we will now discuss the installation on Ubuntu 16.04 systems.
Installation of HAProxy on Ubuntu 16.04
HAProxy 1.8 is also the latest version for Ubuntu 16.04. Ubuntu has dedicated PPA available for installing, install them using the following commands,
[linuxtechi@localhost ~]$ sudo apt-get install software-properties-common [linuxtechi@localhost ~]$ sudo add-apt-repository ppa:vbernat/haproxy-1.8
After the repositories have been added, install haproxy 1.8 with the following command,
[linuxtechi@localhost ~]$ sudo apt-get update [linuxtechi@localhost ~]$ sudo apt-get install haproxy
Now we have haproxy ready. For purpose of this tutorial, we will configure HAProxy to load balance the requests from a mail server & a web server.
Load balancing a Mail server with HAProxy
For this example, we will be using two SMTP servers with the IP adresse 192.168.1.10 & 192.168.1.20 respectively.
The IP address for haproxy server is 192.168.1.100.
We will now make the changes to main HAProxy configuration file i.e. ‘/etc/haproxy/haproxy.cfg‘ and then make the configuration changes for load balancing the two SMTP servers,
[linuxtechi@localhost ~]$ sudo vi /etc/haproxy/haproxy.cfg global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy user haproxy group haproxy maxconn 1024 daemon defaults log global mode tcp option tcplog option dontlognull frontend front_smtp bind *:25 mode tcp default_backend back_smtp backend back_smtp mode tcp balance roundrobin server smtp1 192.168.1.10:25 check server smtp2 192.168.1.20:25 check
here most of the options have been set by default, main things to change here are ‘frontend‘ & ‘backend‘ sections.
frontend front_smtp
bind *:25
mode tcp
default_backend back_smtp
Here we have defined a name for frontend i.e. front_smtp & have asked to take all the request from port 25 with bind parameter, laslty we have mentioned the backend section with the name ‘back_smtp’ where all the requests will be distributed.
backend back_smtp
mode tcp
balance roundrobin
server smtp1 192.168.1.10:25 check
server smtp2 192.168.1.20:25 check
In this section, we have established a name for backend i.e. back_smtp & mode for the transmission will be tcp load balancing method to be used is ‘roundrobin‘. Other load balancing methods that can be used are Weighted round robin, Dynamic round robin algorithm,Least connection algorithm, Source. Lastly we have mentioned the server addresses for both SMTP servers.
Once the changes have been made, save the file & restart the haproxy service to implement the changes,
[linuxtechi@localhost ~]$ sudo service haproxy restart
Our Haproxy server is now ready to work as load balancer for mail server. Now rather than using the SMTP server addresses, we need to use the server address for HAPROXY i.e. 192.168.1.100:25 for haproxy loadbalancing to work.
To check out if the load balancing for our smtp server is working we can use telnet,
Output of telnet command should be something like below:
[linuxtechi@localhost ~]$ telnet 192.168.1.100 25 Trying 192.168.1.100.. Connected to 192.168.1.100. Escape character is ‘^]’. 220 smtp1.linuxtechi.com ESMTP Postfix
Now again run the telnet command,
[linuxtechi@localhost ~]$ telnet 192.168.1.100 25 Trying 192.168.1.100.. Connected to 192.168.1.100. Escape character is ‘^]’. 220 smtp2.linuxtechi.com ESMTP Postfix
Notice the change in the server of the mail servers in both the outputs, which shows that the load balancing is working fine. Now let’s discuss a harpoxy example for load balancing a web server.
Load balancing a Web server with HAProxy
Same file will be edited ‘/etc/haproxy/haproxy.cfg’. We will be using two web servers (192.168.1.10 & 192.168.1.20) on backend. IP address for the haproxy server is 192.168.1.100.
Open the haproxy main configuration file and make the following changes,
[linuxtechi@localhost ~]$ sudo vi /etc/haproxy/haproxy.cfg global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy user haproxy group haproxy maxconn 1024 daemon defaults log global mode http option httplog option dontlognull frontend www-http bind *:80 mode http default_backend apache backend apache mode http balance roundrobin server web1 192.168.1.10 server web2 192.168.1.20
Save the file & restart the haproxy service to implement the changes,
[linuxtechi@localhost ~]$ sudo service haproxy restart
To test out if the load balancing is working, we can place two different web pages on both server. Like for example, we can modify index.html on Webserver 1,
[linuxtechi@web1 ~]$ sudo vi /var/www/html/index.html This is WEB SERVER 1
& similarly on Web server 2, we can edit the index.html to say,
[linuxtechi@web2 ~]$ sudo vi /var/www/html/index.html This is WEB SERVER 2
Don’t forget to restart the web service to implement the changes that have been made.
Now, use the haproxy IP address, 192.168.1.100 & access it using a web browser.
Every time we refresh the browser, we should the alternating web pages from both web servers.
This shows that our webserver load balancing also working fine. With this we end our tutorial on how to install & use HAProxy on Debian 9 & Ubuntu 16.04. Please feel free to send in your queries or suggestions using the comment box below