What is HAProxy and how to install and configure in Linux

What is HAProxy and how to install and configure in Linux

What is HAProxy?

HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. This is useful in cases where too many concurrent connections over-saturate the capability of a single server. Instead of a client connecting to a single server which processes all the requests, the client will connect to an HAProxy instance, which will use a reverse proxy to forward the request to one of the available endpoints, based on a load-balancing algorithm.

Types of Load Balancing

Now that we understand the basic components that are used in load balancing, let’s get into the basic types of load balancing.

No Load Balancing

A simple web application environment with no load balancing might look like the following:

No load balancingIn this example, the user connects directly to your web server, at xyz.com and there is no load balancing. If your single web server goes down, the user will no longer be able to access your web server. Additionally, if many users are trying to access your server simultaneously and it is unable to handle the load, they may have a slow experience or they may not be able to connect at all.

Layer 4 Load Balancing

The simplest way to load balance network traffic to multiple servers is to use layer 4 (transport layer) load balancing. Load balancing this way will forward user traffic based on IP range and port (i.e. if a request comes in for http://xyz.com/anything, the traffic will be forwarded to the backend that handles all the requests for xyz.com on port 80).

Here is a diagram of a simple example of layer 4 load balancing:

L4 load balancingThe user accesses the load balancer, which forwards the user’s request to the web-backend group of backend servers. Whichever backend server is selected will respond directly to the user’s request. Generally, all the servers in the web-backend should be serving identical content–otherwise the user might receive inconsistent content. Note that both web servers connect to the same database server.

Layer 7 Load Balancing

Another, more complex way to load balance network traffic is to use layer 7 (application layer) load balancing. Using layer 7 allows the load balancer to forward requests to different backend servers based on the content of the user’s request. This mode of load balancing allows you to run multiple web application servers under the same domain and port.

Here is a diagram of a simple example of layer 7 load balancing:

L7 load balancing

To Know more about Load balancing Refer How to Use Loadbalancer

High Availability

The layer 4 and 7 load balancing setups described before both use a load balancer to direct traffic to one of many backend servers. However, your load balancer is a single point of failure in these setups; if it goes down or gets overwhelmed with requests, it can cause high latency or downtime for your service.

A high availability (HA) setup is an infrastructure without a single point of failure. It prevents a single server failure from being a downtime event by adding redundancy to every layer of your architecture. A load balancer facilitates redundancy for the backend layer (web/app servers), but for a true high availability setup, you need to have redundant load balancers as well.

Different load balancing algorithms

Configuring the servers in the backend section allows HAProxy to use these servers for load balancing according to the roundrobin algorithm whenever available.

The balancing algorithms are used to decide which server at the backend each connection is transferred to. Some of the useful options include the following:

  • Roundrobin: Each server is used in turns according to their weights. This is the smoothest and fairest algorithm when the servers’ processing time remains equally distributed. This algorithm is dynamic, which allows server weights to be adjusted on the fly.
  • Leastconn: The server with the lowest number of connections is chosen. Round-robin is performed between servers with the same load. Using this algorithm is recommended with long sessions, such as LDAP, SQL, TSE, etc, but it is not very well suited for short sessions such as HTTP.
  • First: The first server with available connection slots receives the connection. The servers are chosen from the lowest numeric identifier to the highest, which defaults to the server’s position on the farm. Once a server reaches its maxconn value, the next server is used.
  • Source: The source IP address is hashed and divided by the total weight of the running servers to designate which server will receive the request. This way the same client IP address will always reach the same server while the servers stay the same.

Installing HAProxy

Before start installing search, the package availability using

# apt search haproxy

Once you see the package available, install it using,

# apt install haproxy

Afterwards, you can double check the installed version number with the following command.

# haproxy -v

To enable HAProxy to be started on bootup

# vim /etc/default/haproxy
#---- Set the ENABLED option to 1

And if you wish to change the location of haproxy configuration file, replace CONFIG value with appropriate config path.

Configuring HAProxy

Setting up HAProxy is too straightforward. Basically, all you need to do is tell HAProxy what kind of connections it should be listening for and where the connections should be relayed to.

Once you install you can find the default configuration on /etc/haproxy/haproxy.cfg with some default settings.

Replace the <server name> with whatever you want to call your servers on the statistics page and the <private IP> with the private IPs for the servers you wish to direct the web traffic to. And in this configuration, you can alter/delete the lines which is not required or need to modify. For more option please refer Official Documentation On HAProxy.

Once you done with configuration, you can test the config file syntax using following command,

# haproxy -c -f /etc/haproxy/haproxy.cfg

Once you for the configuration is valid, you can start the haproxy service.

# systemctl start haproxy

To restart

# systemctl restart haproxy

To stop

# systemctl stop haproxy