DEV Community

Cover image for Load Balancing with HAProxy
Ajisafe Oluwapelumi
Ajisafe Oluwapelumi

Posted on

Load Balancing with HAProxy

In this post, we will discuss how to set up load balancing with HAProxy. Load balancing is an essential technique for distributing traffic across multiple servers to optimize resource usage, ensure high availability, and reduce downtime. HAProxy is a popular and powerful load balancer that provides advanced features and flexibility to handle various types of traffic.

A graphic on how Load Balancing works

Installation

First, we need to install HAProxy on our server. On Ubuntu, we can use the following command to install HAProxy:

sudo apt update
sudo apt install haproxy
Enter fullscreen mode Exit fullscreen mode

Configuration

After installing HAProxy, we need to configure it to handle incoming traffic. The default configuration file is located at /etc/haproxy/haproxy.cfg.

Two key sections of our configuration file are frontend and backend. The frontend section defines how HAProxy will receive traffic. The backend section defines how HAProxy will distribute traffic to the servers.
Here is how our configuration would look like:

frontend http-traffic
    bind *:80
    default_backend servers

backend servers
    balance roundrobin
    server SERVER1 IP_ADDRESS:PORT check
    server SERVER2 IP_ADDRESS:PORT check
Enter fullscreen mode Exit fullscreen mode

In this example, our frontend specifies that HAProxy will listen on port 80 for incoming HTTP traffic, we also specify our default backend to be servers which implies that all http traffic will be directed to servers.

For the backend section, we specify that HAProxy will balance traffic between two servers using the round-robin algorithm. We also specify the IP addresses and ports of the servers and the check option to ensure that HAProxy only sends traffic to servers that are healthy.

Once we have configured HAProxy, we can start the service:

sudo service haproxy start
Enter fullscreen mode Exit fullscreen mode

Testing

To test our load balancing setup, we can use curl to send HTTP requests to the HAProxy server:

$ curl -sI 10.0.0.3 # 10.0.0.3 is our load balancing server
HTTP/1.1 200 OK
server: nginx/1.18.0 (Ubuntu)
date: Tue, 04 Apr 2023 03:31:36 GMT
content-type: text/html
content-length: 13
last-modified: Tue, 04 Apr 2023 01:11:07 GMT
etag: "643b-d"
x-served-by: server1
accept-ranges: bytes

$ curl -sI 10.0.0.3 # 10.0.0.3 is our load balancing server
HTTP/1.1 200 OK
server: nginx/1.18.0 (Ubuntu)
date: Tue, 04 Apr 2023 03:31:39 GMT
content-type: text/html
content-length: 13
last-modified: Tue, 04 Apr 2023 01:07:19 GMT
etag: "644b-d"
x-served-by: server2
accept-ranges: bytes
Enter fullscreen mode Exit fullscreen mode

Upon running the curl command twice, we can see that traffic is being distributed to server1 and server2, indicating that the load balancing mechanism (roundrobin) is functioning correctly.

We also do not have to install Nginx on our load balancer since HAProxy has built-in support for managing HTTP and TCP traffic.

Top comments (0)