Tuesday, March 19, 2024
HomeTutorialsSetup Load Balancing with HAProxy, Nginx and Keepalived in Linux

Setup Load Balancing with HAProxy, Nginx and Keepalived in Linux

In the conventional method of hosting a server or website, the server is hosted through a single HTTP server. When the clients hit on the server, they are allowed on the server. But, what happens when multiple users, even more; thousands of clients, hit the site at a time for some query? What will happen if the server crashes? How will the single server balance the load? To answer all these questions, we can use the term ‘Load balancing’. If you’re looking for authentic tools for managing traffic of your server, you can definitely setup the HAProxy, Nginx, and Keepalived on Linux for load balancing.

Fundamentals of HAProxy, Nginx and Keepalived


The Nginx is well known for its load balancing and proxy services. In load balancer servers, the clients connect to the server through a load balancer instead of directly connecting with the server. Using the Nginx, HAProxy, and Keepalived works fine for load balancing in Linux. When a server crashes, the load balancer connects the clients to another server that is online.

The HAProxy is a free and open-source load balancing tool for both HTTP and TCP web servers. It is written in the C programming language and licensed under the GNU public license. It is highly completable and easy to setup for load balancing in Linux. The term HAProxy stands for High Availability proxy tool. You can deploy the HAProxy tool to improve the server performance, availability, and load balancing.

The Keepalived tool acts as the middle man between two servers that can redirect the client from one server to another server when any of them are down. The Keepalived works in a virtual router redundancy protocol for load balancing and decreasing the server failure issue. Using the HAProxy is good for load balancing and making the server always up.

Hence, there is no guaranty that the HAProxy will always stay alive. Here comes the Keepalived for backing up your HAProxy. It can distribute the client’s request to remotely located servers and handle layer-4, layer-7 (transport and application layer) load balancing. Moreover, the HAProxy can handle access control lists, backend, and frontend terminologies.

Load balancing with HAProxy, Nginx, and Keepalived in Linux


Since we have already talked a lot about the HAproxy, Nginx, and the Keepalived tool for Linux, so let’s jump into the tutorial. In this post, we will see how to set up load balancing with HAProxy, Nginx, and Keepalived in Linux. We will need fundamental server-level knowledge and the basic ability to use Linux to go with this post. 

Step 1: Install Nginx on Linux for Load Balancing


Since we will see the methods of making our server more powerful, load balancing, and more client handling, installing the Nginx HTTP web server would be the first step to begin the process. Installing the Nginx web server is easy and straightforward, and you can use the following commands to install the Nginx web server on your Linux machine.

  • Install Nginx web server on Ubuntu/Debian Linux
sudo apt update
sudo apt install nginx

install Nginx on Linux

  • Execute the following commands to install Nginx on Fedora/Red Hat Linux
yum -y install nginx

After installing the Nginx server, we will configure, active, and start the server on your machine. Please go through this post to see how to get started with the Nginx server on Linux.

Step 2: Install the HAproxy Load Balancer in Linux


After installing and configuring the Nginx server on your Linux machine, it might work fine as a server on your machine or network. But, as we are here trying to make a load-balancing server, we will now install and configure the HAproxy tool on our system.

1. Install and Configure HAproxy on Ubuntu for Load Balancing


Before you install the HAProxy on your system, you might want to check if the HAProxy services are already in your system or not.

sudo apt show haproxy

Now, add the HAProxy repository to your system through the PPA packages. Once the PPA pulls up the repository, update your system repo.

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

Finally, Please run the following commands on your Ubuntu/Debian Linux system to get the HAProxy on the system. After installing the HAProxy, please check the HAProxy version to ensure that it has been installed correctly on your machine.

install HAproxy on Ubuntu

sudo apt install -y haproxy
haproxy -v

haproxy version

Once the installation of the HAProxy is done on your Linux machine, we will now edit the configuration script for setting up the HAProxy with our server. Here, we will edit a few HAProxy configuration scripts, and please be careful while you edit those scripts and make a backup for those files to restore the default setup if something goes wrong.

First, run the following command on your terminal shell with root access to edit the HAProxy configuration script. Here, I’m using the Nano script editor tool, and you can use any of your favorite tools.

sudo nano /etc/haproxy/haproxy.cfg

haproxy configuration file

Now, copy and paste the following script lines inside the file, then save and close the file. The following script lines define the frontend and the backend status with HAProxy. Please input your server details in the server name, IP, and other credential fields.

frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back

backend http_back
balance roundrobin
server :80 check
server :80 check

Now, run the following command on the terminal shell to edit and configure the HAproxy script. 

sudo nano /etc/haproxy/haproxy.cfg

Please use the following configuration script to set up the HAproxy settings. 

frontend http_front
bind *:80
stats uri /haproxy?stats
acl url_blog path_beg /blog
use_backend blog_back if url_blog
default_backend http_back

backend http_back
balance roundrobin
server :80 check
server :80 check

backend blog_back
server :80 check

When the configuration is done, you may now restart the HAProxy tool on your Linux machine by running the following system control command with root access.

sudo systemctl status haproxy
sudo systemctl restart haproxy

You can now run the command mentioned below with your server’s address to check the server’s status.

https:///haproxy?stats

2. Install and Configure HAproxy on Fedora


Installing the HAProxy load balancing tool on Fedora Linux is kind of the same as installing it on Debian/Ubuntu systems. First, update the system repository, then run the DNF command to install the HAProxy tool on your Linux machine.

yum -y update
yum -y install haproxy

install HAproxy on Linux

Once the installation ends up, run the following command to make a backup of the configuration script before making any changes.

cd /etc/haproxy/
mv haproxy.cfg haproxy.cfg_bac

You may now create a new HAProxy configuration script by running the following touch command given below. Then edit the script with the following Nano command.

touch haproxy.cfg
nano haproxy.cfg

You can copy and paste the following configuration script, then save and exit the file.

global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

#frontend
#---------------------------------
frontend http_front
bind *:80
stats uri /haproxy?stats
default_backend http_back

#round robin balancing backend http
#-----------------------------------
backend http_back
balance roundrobin
#balance leastconn
mode http
server webserver1 10.13.211.169:80 check # ip_address_of_1st_centos_webserver
server webserver2 10.13.211.158:80 check # ip_address_of_2nd_centos_webserver

After adding the script into the configuration file, we will now enable, start, and check the status of the HAProxy tool on our Fedora Linux.

systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy

You can also check whether the HAProxy is performing well on your system or not by pulling the HAProxy status through your web browser.

https://10.13.211.194/haproxy?stats

The following cURL commands will also return the in-depth server status along with the HAProxy status.

curl 10.13.211.194
curl 10.13.211.194

Step 3: Install Keepalived on Linux


Since we already have discussed Keepalived, so here we are directly going through the installation process of Keepalived on Linux systems. Here we will see methods of installing and configuring the Keepalived tool on Fedora and Debian Linux.

1. Install and configure Keepalived on Ubuntu/Debian


To install the Keepalived load balancing tool on Ubuntu and other Debian Linux systems, please execute the following command to get the build-essential tools on your systems. Then browse the home directory and run the wget command supplied below to download the compressed Keepalived file on the filesystem.

sudo apt-get install build-essential libssl-dev
cd ~
wget https://www.keepalived.org/software/keepalived-1.2.19.tar.gz

setup Load balancing in Linux keepalived tool

When the download finishes, please use the following tar command to extract the file, then browse the extracted directory through the CD command.

tar xzvf keepalived*
cd keepalived*

You may now run the following commands to install the Keepalived tool on your Ubuntu system.

./configure
make
sudo make install

When the installation ends, it’s time to edit the configuration for setting up the Keepalived with our server. Please run the following command given below to edit the configuration script. 

sudo nano /etc/init/keepalived.conf

When the script opens, copy and paste the following script provided below.

description "load-balancing and high-availability service"

start on runlevel [2345]
stop on runlevel [!2345]

Now, run the mkdir command to create a new configuration script for the Keepalived tool, then populate it with the configuration script.

sudo mkdir -p /etc/keepalived
sudo nano /etc/keepalived/keepalived.conf

Use the following script lines for filling up the Keepalived configuration file.

vrrp_script chk_haproxy {
script "pidof haproxy"
interval 2
}

vrrp_instance VI_1 {
interface eth1
state MASTER
priority 200

virtual_router_id 33
unicast_src_ip primary_private_IP
unicast_peer {
secondary_private_IP
}


}

Now, edit the Keepalived configuration script from the etc directory and add the following lines into your script.

sudo nano /etc/keepalived/keepalived.conf

Script lines that we will need to put into the configuration file.

vrrp_script chk_haproxy {
script "pidof haproxy"
interval 2
}

vrrp_instance VI_1 {
interface eth1
state BACKUP
priority 100

virtual_router_id 33
unicast_src_ip secondary_private_IP
unicast_peer {
primary_private_IP
}

authentication {
auth_type PASS
auth_pass password
}

track_script {
chk_haproxy
}

notify_master /etc/keepalived/master.sh
}

After configuring the scripts for Keepalived, we will now create a secondary load balancer configuration script and populate the script with the necessary script lines.

sudo nano /etc/keepalived/keepalived.conf

Run the nano command to edit the script, and populate the script with the lines given below. Once the file is updated, save and close the script.

vrrp_script chk_haproxy {
script "pidof haproxy"
interval 2
}

vrrp_instance VI_1 {
interface eth1
state BACKUP
priority 100

virtual_router_id 33
unicast_src_ip secondary_private_IP
unicast_peer {
primary_private_IP
}

authentication {
auth_type PASS
auth_pass password
}

track_script {
chk_haproxy
}

notify_master /etc/keepalived/master.sh
}

2. Install and Configure Keepalived on Fedora


Installing the Keepalived tool on Fedora and Red Hat Linux systems is pretty similar to installing it to Debian distributions. First, you will need to update your system repository, then run the following yum command to install the Keepalived tool.

yum update
yum install -y keepalived

setup Load balancing in Linux keepalived

When the installation ends up, we will now edit the Keepalived configuration script for adjusting the server settings with Keepalived. Before we make any changes, we will make a backup for the Keepalived configuration script.

Run the following commands on your shell one by one to make a backup file, create a configuration file, and edit the configuration script. After configuring the tool, we will use it to setup our load balancing server in Linux.

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bac
touch /etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf

When the configuration script opens, please populate the file with the script lines supplied below. You may need to make changes to the script file according to your server’s IP, port, name, and other details.

global_defs {
notification_email {
www.bytesbuzz.com
ubuntupit@gmail.com
}
notification_email_from thunderdfrost@gmail.com
smtp_server 10.13.211.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0 #put your interface name here. [to see interface name: $ ip a ]
virtual_router_id 51
priority 101 # 101 for master. 100 for backup. [priority of master> priority of backup]
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 #password
}
virtual_ipaddress {
10.13.211.10 # use the virtual ip address.
}
}

After configuring the Keepalived scripts, please execute the following system control commands with root access on your terminal shell to start, enable, and check the status of Keepalived on your Linux machine.

systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived

If everything goes right, the following command will return the server status for all your IP locations that will ensure that your Linux server has the complete setup for load balancing with high availability.

$ while true; do ; curl 10.13.211.10 ; sleep 1; done;

Final Words


If you’re a system admin, you know how important it is to make your server always live and accessible from clients across the world. Mostly, if your server is a busy one, enabling a load balancing mechanism is recommended.

It can make the server fast and accessible with tons of hits at a time. In the entire post, I’ve gone through the fundamentals of HAProxy, Keepalived, and Nginx. I’ve illustrated the concept and methods on how to setup HAProxy, Nginx, and Keepalived for load balancing in Linux.

Please share it with your friends and the Linux community if you find this post useful and informative. You can also write down your opinions regarding this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

You May Like It!

Trending Now