HAProxy – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Thu, 31 Dec 2020 16:33:19 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 Install HAProxy to Configure Load Balancing Server on Debian 10 https://linuxhint.com/install_haproxy_configure_load_balancing_server_debian_10/ Wed, 30 Dec 2020 16:06:22 +0000 https://linuxhint.com/?p=83336

Load-balancing is the most common practice of distributing incoming web traffic among multiple back-end servers. This makes the application highly available even if some of the servers go down for some reason.  Load Balancing increases the efficiency and reliability of a web application. HAProxy load-balancer is used for the same purpose. It is the most widely used load-balancer in industries. As per the official website, HAProxy is used by leading companies like AWS, Fedora, Github, and many more.

HAProxy or  High Availability Proxy provides high availability and proxying solution. It is written in C and works at network and application layers of the TCP/IP model. The best thing is that it has a free community edition, and it is an open-source application. It works on Linux, FreeBSD, and Solaris operating systems. The enterprise edition is also there, but it has a price tag.

In this guide, we will see How to Install HAProxy and Configure the Load Balancing Server on Debian 10.

Prerequisites:

  1. “sudo” access to all the machines and basic knowledge of running commands in Linux terminal.
  2. Private IP addresses added to load-balancer and backend servers.
  3. Debian 10 Operating System installed on all machines.

Installing HAProxy on Debian 10

For our guide, we will assume the following IP address configuration :

  1. HAProxy load-balancer 10.0.12.10
  2. Web server1: IP Address: 10.0.12.15
  3. Web server2: IP Address: 10.0.12.16

Step 1. Update Debian System repository and packages

First, run the below commands on all systems to update software packages to the latest one.

$ sudo apt update

$ sudo apt upgrade -y

Step: 2 Install Nginx on back-end servers

Prepare your back-end servers by installing Nginx web server on each. You can also choose to install other web servers like apache.

To install Nginx, run the following commands on each back-end server in your environment:

$ sudo apt install nginx

Step: 3 After Nginx is installed on your back-end servers, start the service, as shown below:

$  sudo systemctl start nginx

TIP: We can also manage the nginx web server using the below command:

$ sudo /etc/init.d/nginx “option”

option: start reload restart status stop

Step: 4 Create custom index pages in the web folder of each Nginx web server. This will help us to distinguish which back-end server is serving the incoming requests.

On each web server, perform the following tasks:

Backup the original index file using the following command:

$ sudo cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.orig

Add custom text to the index.html file. We are adding the IP address of each web server.

For web server 1:

$ sudo echo "Web server 1: 10.0.12.15"  | sudo tee /usr/share/nginx/html/index.html

For web server 2:

$ sudo echo "Web server 2: 10.0.12.16"  | sudo tee /usr/share/nginx/html/index.html

You can also use vi editor if you feel more comfortable with that. This is shown below:

$ sudo vi /usr/share/nginx/html/index.html

When the file is opened, enter the text and save the file.

Open the default virtual host file in the “/etc/nginx/sites-available/” directory.

$ sudo nano /etc/nginx/sites-available/default

Now inside the server block, change the root directive from “/var/www/html” to “/usr/share/nginx/html”.

To check the Nginx configuration, run the following command:

$ sudo nginx -t

Step 5: Now restart the service using the command:

$ sudo systemctl restart nginx

You can check the status of nginx using the following command:

$ sudo systemctl status nginx

Step: 6 To install HAProxy on Debian 10 (Buster), run the following command on the load-balancer.

$ sudo apt install haproxy -y

Tip: Once HAProxy is installed, you can manage HAProxy via an init script. For this, set the “enabled” parameter to 1 in “/etc/default/haproxy” as shown below:

$ sudo vi /etc/default/haproxy

ENABLED=1

Now the following option can be used with an init script:

$ sudo service haproxy “option.”

option: start reload restart status stop

Step: 7 Now configure HAProxy load-balancer by editing the haproxy default configuration file, i.e. “/etc/haproxy/haproxy.cfg”. To edit this file, run the following command

$ sudo vi /etc/haproxy/haproxy.cfg

Tip: Please backup the original file so that in case something goes wrong, we will be all safe. To perform the backup, use the following command:

$ sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig

Now go to the end of the file and edit the following information:

frontend Local_Server

bind 10.0.12.10:80

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 web1 10.0.12.15:80

server web2 10.0.12.16:80

Note: Do not forget to change the IP addresses in the above file to the one you have added to your web servers.

Step: 8 Verify the configuration syntax of the above file with the following command:

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

If everything goes right, it will show an output like: “Configuration file is valid.” If you get any error in the output, recheck your configuration file and verify it again.

Step: 9 Now restart the HAProxy service to apply the  changes

$ sudo service haproxy restart

Testing The Configuration

Now it is time to see if our setup is working properly. Enter load-balancer system IP on a web browser (In our case, it is 10.0.12.10) and refresh the page continuously for 2-4 times to see if HAProxy load-balancer is working properly. You should see different IP addresses or whatever text you have entered in the index.html file when you continue to refresh the page multiple times.

Another way to check is to take one web server offline and check if another web server is serving the requests.

That’s all for now! Try experimenting with HAProxy to learn more about how it works. For e.g., you can try:

  • Integrating different web server beside nginx.
  • Changing the load-balancing algorithm to something other than round-robin.
  • Configuring HAProxy health check to determine if a back-end server is working or not.
  • Applying sticky sessions to connect a user to the same back-end server.
  • Using HAProxy stats to get insights about the traffic on servers.

HAProxy has extensive documentation available for both the HAProxy community edition and HAProxy enterprise version. Explore this documentation to get more insights into improving the performance and reliability of your server environment.

This guide has been successfully performed on Debian 10(Buster). Try to install HAProxy on other Debian based distros like Ubuntu, Linux Mint etc. Please do not forget to share this guide with others.

]]>
How to Setup HAProxy as Load Balancer for Nginx in CentOS 8 https://linuxhint.com/setup_haproxy_load_balancer_centos8/ Sat, 28 Dec 2019 04:37:04 +0000 https://linuxhint.com/?p=52418 High Availability Proxy, also abbreviated as HAProxy is a lightweight and fast load balancer which also doubles up as a proxy server. As a load balancer, it plays a crucial role in distributing incoming web traffic across multiple web servers using certain criteria. In doing so, it ensures high availability and fault tolerance in the event there are too many concurrent requests which may overload a single web server.

HaProxy is used by popular sites such as Tumblr, GitHub, and StackOverflow. In this guide, we will take you through the installation of HAProxy in a setup of webservers that are powered using Nginx.

Lab Setup

3 instances of CentOS 7 servers as shown

Hostname           IP addresses

load_balancer      3.17.12.132
server_01          3.19.229.234
server_02          3.17.9.217

Step 1: Edit the /etc/hosts file for the load balancer

To start off, log into the load balancer system and modify the /etc/hosts file to include the hostnames and IP addresses of the two web servers as shown

$ vim /etc/hosts
3.19.229.234   server_01
3.17.9.217     server-02

Once done, save the changes and exit the configuration file.

Now head out to each of the web servers and update the /etc/hosts file with the IP address and hostname of the load balancer

3.17.12.132   load-balancer

Thereafter, confirm  that you can ping the load balancer from server_01

And likewise from server_02

Also, make sure, you can ping the servers from the load balancer.

Perfect ! all servers can communicate with the load balancer!

Step 2: Install and configure HA Proxy on the load balancer

Because HA Proxy is readily available from CentOS official repository, we are going to install it using the yum or dnf package manager.

But as always, update the system first

# yum update

Next, install HA Proxy as shown

# yum install haproxy

Upon successful installation,  navigate to the haproxy directory.

# cd /etc/haproxy

Best practice requires us to back up any configuration file before making any modifications.  So Backup the haproxy.cfg file by renaming it.

# mv haproxy.cfg  haproxy.cfg.bak

Next, proceed and open the configuration file

vim haproxy.cfg

Ensure you make the modification as shown

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log         127.0.0.1 local2     #Log configuration
 
chroot      /var/lib/haproxy
pidfile     /var/run/haproxy.pid
maxconn     4000
user        haproxy             #Haproxy running under user and group "haproxy"
group       haproxy
daemon
 
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
 
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode                    http
log                     global
option                  httplog
option                  dontlognull
option http-server-close
option forwardfor       except 127.0.0.0/8
option                  redispatch
retries                 3
timeout http-request    10s
timeout queue           1m
timeout connect         10s
timeout client          1m
timeout server          1m
timeout http-keep-alive 10s
timeout check           10s
maxconn                 3000
 
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen haproxy3-monitoring *:8080                #Haproxy Monitoring run on port 8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats                             #URL for HAProxy monitoring
stats realm Haproxy\ Statistics
stats auth Password123: Password123#User and Password for login to the monitoring dashboard
stats admin if TRUE
default_backend app-main                    #This is optionally for monitoring backend
 
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend main
bind *:80
option http-server-close
option forwardfor
default_backend app-main
 
#---------------------------------------------------------------------
# BackEnd round robin as balance algorithm
#---------------------------------------------------------------------
backend app-main

balance roundrobin                         #Balance algorithm

option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost
#Check the server application is up and healty - 200 status code

server server_01 3.19.229.234:80 check               #Nginx1

server server_02 3.17.9.217:80 check                 #Nginx2

Be sure to modify the web servers hostname and IP addresses as indicated in the last two lines. Save the changes and exit.

The next step will be to configure Rsyslog to be able to log HAProxy statistics.

# vim /etc/rsyslog.conf

Make sure you uncomment the lines below to allows UDP connections

$ModLoad imudp
$UDPServerRun 514

Next, proceed and create a new configuration file  haproxy.conf

# vim  /etc/rsyslog.d/haproxy.conf

Paste the following lines, save and exit

local2.=info   /var/log/haproxy-access.log   #For Access Log
local2.notice  /var/log/haproxy-info.log     #For Service Info - Backend, loadbalancer

For the changes to take effect restart the rsyslog daemon as shown:

# systemctl restart rsyslog

Then start and enable HAProxy

# systemctl start rsyslog
# systemctl enable rsyslog

Verify that HAProxy is running

# systemctl status rsyslog

Step 3: Install  and configure Nginx

 Now, the only part remaining is the installation of Nginx. Log into each of the servers and first update the system packages:

# yum update

Next install  EPEL (Extra Packages for Enterprise Linux)

# yum install epel-release

To install Nginx, run the command:

# yum install nginx

Next, start and enable Nginx

# systemctl start nginx
# systemctl enable nginx

We are then going to modify the index.html file in both cases in order to demonstrate or simulate how the load balancer is able to distribute web traffic across both servers.

For server_01

# echo "server_01. Hey ! Welcome to the first web server" > index.html

For server_02

# echo "server_02. Hey ! Welcome to the second web server" > index.html

For the changes to be effected, restart Nginx

# systemctl restart nginx

Step 4: Testing if the load balancer is working

We are finally at the point where we want to see if the configuration is working. So log into the load balancer and execute the curl command repeatedly

# curl 3.17.12.132

You should get alternating output on the terminal showing the value of index.html  from server_01 and server_02

Now let’s test using a web browser. Browse your load balancer’s IP address

http://load-balancer-IP-address

The first page will display content from any of the web servers


Now refresh the webpage and check to see if it displays content from the other web server

Perfect ! The load balance is distributing IP traffic equally between the two web servers !
This wraps up this tutorial on how you can install as well as configure HAProxy on CentOS 8. Your feedback will be much appreciated. ]]>