What is HAProxy & how to get the most from it?

Why we need Load Balancers?

What is a load balancer?

Load balancing consists in aggregating multiple components in order to achieve a total processing capacity above each component’s individual capacity, without
any intervention from the end user and in a scalable way. This results in more operations being performed simultaneously by the time it takes a component to
perform only one. — From HAProxy doc

How does it work?

Prerequisite

Downloading

wget 'http://www.haproxy.org/download/2.2/src/haproxy-2.2.3.tar.gz'
tar xf haproxy-2.2.3.tar.gz

Installing

make clean
make -j $(nproc) TARGET=linux-glibc \
USE_OPENSSL=1 USE_ZLIB=1 USE_LUA=1 USE_PCRE=1 USE_SYSTEMD=1
sudo make install
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 \
USE_LUA=1 USE_PCRE2=1 USE_PCRE2_JIT=1 USE_SYSTEMD=1 \
USE_THREAD=1 USE_STATIC_PCRE2=1 USE_LIBCRYPT=1 USE_GETADDRIFO=1 \
USE_TFO=1 USE_NS=1
apt install libopenssl
apt install zlib1g-dev
apt install libcrypt-dev

How to run it?

Configuration file

1. Command-line arguments

sudo haproxy -f <config_file> -c  # validate config file first
sudo haproxy -f <config_file> # then run the server
haproxy -h  # showing a list of available options

2. global section

Proxy is what receives & handles traffic according to the defined configurations.

Photo by John Adams on Unsplash

3. Proxies

listen, backend & frontend

“listen” acts as both frontend and backend in which it is accessible by the user and it forwards the traffic to the server as well.

Photo by Jason Hogan on Unsplash

Systemd Integration

sudo systemctl daemon-reload  # reread the systemd-wide services
sudo systemctl enable --now haproxy.service

Statistical Report

One of my previous HAProxy configured servers statistical reports

Conclusion

Acknowledgment

Resources

Backend Developer | DevOps Engineer | A Cool Mentor | Linkedin: https://linkedin.com/in/meysamazad