Are you trying to install HAproxy on Ubuntu ?
This guide is for you.
HAproxy is an open-source and lightweight package that offers high availability and load balancing for TCP and HTTP based programs.
It distributes the load among the web and application servers.
HAproxy is available for nearly all Linux distributions. It is a widely used load balancer that is popular for its efficiency, reliability, and low memory and CPU footprint.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to configure HAproxy on their Servers.
In this context, we shall look into how to install and configure HAproxy on a Ubuntu system.
Here, We have setup three machines.
We will install HAproxy on one server and the Apache web servers on two servers. Our HAproxy server will then act as a load balancer and will distribute the load among Apache web servers.
The same procedure is also valid for Debian and Linux Mint distributions.
We will be using three Ubuntu servers; all on the same network.
The details of our servers are as follows:
i. Hostname: HAproxy, IP address: 192.168.72.157 (Frontend server).
ii. Hostname: web-server1, IP address: 192.168.72.158 (Backend servers).
iii. Hostname: web-server2, IP address: 192.168.72.159 (Backend servers).
Note: You must have sudo privileges on all the servers.
We will configure one machine as a load balancer and the other two as web servers. The HAproxy server will be our front-end server that will receive the requests from the users and forward them to the two web servers. The web servers will be our Backend servers that will receive those forwarded requests.
Here, we will setup two web servers (web-server1 and web-server2) as our backend servers.
Perform the following steps in your web server.
Make sure to replace the hostnames and IP addresses with the relevant IP addresses and hostnames of your web servers.
a. On web-server1, edit the /etc/hosts file:
$ sudo nano /etc/hosts
b. Then add the hostname entry for HAproxy server as follows:
hostname-of-HAproxy IP-address-of-HAproxy
In our case, it would be:
HAproxy 192.168.72.157
a. Now install Apache web server using the following command in Terminal:
$ sudo apt install apache2
b. Then enable and start the Apache service using the below commands in Terminal:
$ sudo systemctl enable apache2
$ sudo systemctl start apache2
c. Create an index file for web-server1 using the below command in Terminal:
$ echo "<H1>Hello! This is webserver1: 192.168.72.158 </H1>" | sudo tee /var/www/html/index.html
d. If a firewall is running on your system, you will need to allow Apache traffic through it:
$ sudo ufw allow 80/tcp
e. Then reload the firewall configurations:
$ ufw reload
f. Now try accessing the site in your web browser by typing http:// followed by either the IP address or the hostname of your web server:
http:// hostname-or-IP-address
g. Alternatively, you can also use the curl command to test the webpage:
$ curl <hostname-or-IP-address>
Perform the below steps in your second web server. Make sure to replace the hostnames and IP addresses with the relevant IP addresses and hostnames of your web servers.
a. In web-server2, edit the /etc/hosts file:
$ sudo nano /etc/hosts
b. Then add the hostname entry for HAproxy server as follows:
HAproxy 192.168.72.157
a. Now install Apache web server using the following command in Terminal:
$ sudo apt install apache2
b. Then enable and start the Apache service using the below commands in Terminal:
$ sudo systemctl enable apache2
$ sudo systemctl start apache2
c. Then, Create an index file for web-server2 using the below command in Terminal:
$ echo "<H1>Hello! This is webserver2: 192.168.72.159 </H1>" | sudo tee /var/www/html/index.html
d. Allow Apache in the firewall:
$ sudo ufw allow 80/tcp
e. Then, reload firewall configurations:
$ ufw reload
f. Now try accessing the site in your web browser by typing http:// followed by either the IP address or the hostname.
http:// hostname-or-IP-address
g. Alternatively, you can use the curl command to test the webpage:
$ curl <hostname-or-IP-address>
Now our Apache web servers are ready.
Here, we will set up an HAproxy load balancer for our web servers.
This HAproxy server will act as a frontend server and accepts incoming requests from clients.
On the HAproxy server (192.168.72.157), perform the below steps to setup load balancer.
a. Edit the /etc/hosts file using the below command in Terminal:
$ sudo nano /etc/hosts
b. Add the following hostname entries for both Apache web servers along with its own hostname:
192.168.72.157 HAproxy
192.168.72.158 web-server1
192.168.72.159 web-server2
c. Now save and close the /etc/hosts file.
Now in this step, we will be installing the HAproxy on one of our Ubuntu server (192.168.72.157).
a. To do so, update apt using the following command in Terminal:
$ sudo apt-get update
b. Then update packages using the below command:
$ sudo apt-get upgrade
c. Now install HAproxy using the following command in Terminal:
$ sudo sudo apt install haproxy
d. Once the installation of the HAproxy server is finished, you can confirm it using the below command in Terminal:
$ haproxy -v
This will show you the installed version of HAproxy on your system which verifies that the HAproxy has been successfully installed.
In the following section, we will configure HAproxy as a load balancer. To do so, edit the /etc/haproxy/haproxy.cfg file:
$ sudo nano /etc/haproxy/haproxy.cfg
Append the following lines in the haproxy.cfg file replacing the IP addresses with your own IP addresses.
The frontend web-frontend in the above configuration lines tells HAproxy to listen to incoming requests on port 80 of 192.168.72.157 and then forward them to backend servers configured under the backend web-backend. While configuring, replace the IP addresses with the relevant IP addresses of your web servers.
With HAproxy monitoring, you can view a lot of information including server status, data transferred, uptime, session rate, and so on.
i. To configure HAproxy monitoring, append the following lines in the configuration file located at /etc/haproxy/haproxy.cfg:
listen stats
bind 192.168.72.157:8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats
stats realm Haproxy\ Statistics
stats auth linuxapt:linuxapt #Login User and Password for the monitoring
stats admin if TRUE
default_backend web-backend
The above configuration enables the HAproxy "stats" page using the stats directive and secures it with http basic authentication using the username and password defined by the stats auth directive.
ii. Once you are done with the configurations, save and close the haproxy.cfg file.
iii. Now verify the configuration file using the below command in Terminal:
$ haproxy -c -f /etc/haproxy/haproxy.cfg
iv. Now to apply the configurations, restart the HAproxy service:
$ sudo systemctl restart haproxy.service
This will stop and then start the HAproxy service.
v. To check the status of the HAproxy service, the command would be:
$ sudo systemctl status haproxy.service
The active (running) status in the output shows that the HAproxy server is enabled and running fine.
a. In order to start the HAproxy server, the command would be:
$ sudo systemctl start haproxy.service
b. In order to stop the HAproxy server, the command would be:
$ sudo systemctl stop haproxy.service
c. In case you want to temporarily disable the HAproxy server, the command would be:
$ sudo systemctl disable haproxy.service
d. To re-enable the HAproxy server, the command would be:
$ sudo systemctl enable haproxy.service
Before testing the HAproxy setup, make sure you have connectivity to web servers.
From your HAproxy server, ping both web servers either by their IP addresses or hostnames.
$ ping hostname-or-ip-address
The output shows that the HAproxy server can reach both web servers.
Now in your HAproxy server, open any web browser and type http:// followed by the HAproxy server IP address which in our case is 192.168.72.157.
http://192.168.72.157
The HAproxy server will alternatively send the request to both web servers in a round-robin method.
You can test this by reloading the webpage a few times.
You can also use the hostname in place of the HAproxy server IP address.
You can also use the curl command in Linux to test the HAproxy setup.
i. Open the Terminal and type curl followed by the IP address or the hostname of the HAproxy server.
$ curl 192.168.72.157
or
$ curl HAproxy
ii. Run the curl command a few times and you will see the response alternating between both web servers.
Instead of running the commands several times, you can also run the following one-line script to test the HAproxy server:
$ while true; do curl 192.168.72.157; sleep 1; done
To access the HAproxy monitoring page, type http:// followed by the IP address/hostname of HAproxy server and port 8080/stats:
http://192.168.72.157:8080/stats
or
http://HAproxy:8080/stats
An authentication box will appear.
Enter the username and password you have configured earlier in the configurations and then press OK.
Then, you will see the statistics report for our HAproxy server.
This article will guide you on how to #install and configure #HAproxy load balancer on the #Linux system.
You will learn the basic setup and configuration of HAproxy as a load balancer for #Apache web servers. Also, we looked at some commands for managing the HAproxy server. In the end, we tested the load balancing through the browser and the #curl command.
HAProxy is free, open source #software that provides a high availability #load #balancer and proxy server for #TCP and HTTP-based applications that spreads requests across multiple servers.
To Deploy on Linux :
1. Download the source code of HAProxy.
2. Unzip the file into the desired location tar xvzf haproxy-1.8-dev1.
3. Compile the source code.
4. Create config file haproxy.cfg with the configuration details.
5. Start the haproxy with ./haproxy -f haproxy.
6. Access the status page on http:localhost:9999/stats.