24.2 C
Guru Unix /Linux and GNU supporter

How to install and configure HAProxy loadbalancer on Ubuntu 18.04

A load balancer goes about as the “traffic cop” sitting before your servers and steering customer asks for over all servers fit for satisfying those solicitations in a way that boosts speed and limit use and guarantees that nobody server is exhausted, which could debase execution. In the event that a solitary server goes down, the load balancer diverts activity to the staying on the web servers. At the point when another server is added to the server gathering, the heap balancer naturally begins to send demands to it.

To install and configure HAProxy loadbalancer on Ubuntu 18.04 we will consider three system as follows

1. One is web server1>IP Address:>Hostname:system1.osrader.com
2. Two is web server2>IP Address:>Hostname:system2.osrader.com
3. HAproxy loadbalancer>IP Address:>Hostname:loadbalancer.osrader.com

Step 01: Make sure that system1 and system2 has apache2 installed. Please use below command for Ubuntu 18.04 to install apache2 for both system1 and system2.

- Advertisement -
$ sudo apt-get update
$ sudo apt-get install apache2
$sudo ufw app list (to check list of application profiles)



$sudo ufw allow 'Apache'
$sudo ufw status (for verifying)
$sudo systemctl status apache2 (to check service status)

Now type system1 and system2 IP address on web browser and visit apach2 default web page.

You can change default index.html file from /var/www/html.
You can also change /etc/hosts file to visit web page by host name, like I did.






Step 02:

We will now configure loadbalancer machine. At first we will install HAProxy by executing below commands

$ sudo add-apt-repository ppa:vbernat/haproxy-1.7
$ sudo apt-get update
$ sudo apt-get install haproxy

Step 03:

Edit haproxy default configuration file /etc/haproxy/haproxy.cfg and start configuration.

$ vim /etc/haproxy/haproxy.cfg

At the end of the file we will add folowing information

frontend Local_Server
mode http
default_backend webserver

backend webserver
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server system1.osradar.com
server system2.osradar.com

Step 04:

To verify the configuration

$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg

It will show output : Configuration file is valid

Step 05:

Restart HAproxy

$ sudo service haproxy restart

Now type system ip) on web browser and refresh 2/3times. You will see HAProxy loadbalancer is working.





Thank you.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article