How to Use HAProxy to Set Up Http load balancing on ubuntu

This article explains , how to use HAProxy for load balancing user requests in ubuntu or debian based systems .

Hello all. Today we will see about HAProxy and an example configuration for load balancing.

Before getting into it,

What is HAProxy?

    HAProxy -> High Availability Proxy. It's an Open Source load balancer which can handle any TCP/HTTP connections.

Where to use HAProxy?   

   Let's say you have multiple servers for your application. You want to split requests between servers. There you can use HAProxy as your Load Balancer which splits your requests between servers.

Haproxy Overview

How to get HAProxy?

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

For other system users refer here

Hope You have HAProxy in your system. Now Let's get into session.

What we gonna do now?

   We are gonna have two servers and use HAProxy to split requests between two servers.

save this code as  server1.py


import BaseHTTPServer


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

def do_GET(s):
    s.send_response(200)
    s.send_header("Content-type", "text/plain")
    s.end_headers()
    s.wfile.write("Hello From Server 1")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class(('localhost', 8001),MyHandler)
    httpd.serve_forever()

Run this as python -m server1

Save this below code as server2.py


import BaseHTTPServer

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

def do_GET(s):
    s.send_response(200)
    s.send_header("Content-type", "text/plain")
    s.end_headers()
    s.wfile.write("Hello From Server 2")

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class(('127.0.0.1', 8002),MyHandler)
    httpd.serve_forever()

Run this as python -m server2

Step 1: Check server1 and server2 running or not

open your favourite browser and check these url

    http://localhost:8001/

    http://localhost:8002

which results
  Hello From Server 1 and  Hello From Server 2

Step 2: Write/change haroxy.cfg to load balance these two servers

In debian/ubuntu  based systems config will be in  /etc/haproxy/haproxy.cfg

   sudo nano /etc/haproxy/haproxy.cfg

add these lines into your config


frontend localnodes
bind 127.0.0.1:9000
mode http
default_backend nodes

backend nodes
mode http
balance roundrobin
option forwardfor
server web01 127.0.0.1:8001 check
server web02 127.0.0.1:8002 check

listen stats
bind 127.0.0.1:1936
mode http
stats enable
stats uri /
stats hide-version
stats auth test:test

Restart haproxy by

   sudo service haproxy restart

Note: You can have as many servers as you can . Just for example i have two servers.

Step 3: Explanation for config lines added

Here we need to know two blocks 

  • frontend
  • backend

frontend -> defines the listening port of HAProxy.


frontend localnodes
bind 127.0.0.1:9000
mode http
default_backend nodes

Here our HAProxy listens on HOST  127.0.0.1 and port 9000.
mode -> http defines http requests and we define our backend to process our requests .

nodes -> backend name, you can use any name.

localnodes -> our frontend name you can use any name.

Now let's see about backend


backend nodes
mode http
balance roundrobin
option forwardfor
server web01 127.0.0.1:8001 check
server web02 127.0.0.1:8002 check

server web01 127.0.0.1:8001 check

In above line, 

   server -> keyword necessary

   web01 -> our identification , 127.0.0.1:8001 -> server ip and port
  mode -> http necessary .  check -> plays an import role here,  ping server at specific interval and checks its up or down and updates stats page. 

Step 4: Check HAProxy load balancing by

  http://127.0.0.1:9000

which results
 Hello From Server 1

By default HAProxy splits request by  round-robin and assigns requests between two servers

Now just do one thing

Just stop one server by  Cntrl + C on  python -m server1

now hit  http://127.0.0.1:9000

which results
  Hello From Server 2

when ever server goes down HAProxy knows that,  allocates requests to only active servers.

This is good right. Yeah pretty good for load balancing stuff.

Step 5: Now check haproxy stats by  

open in browser  http://127.0.0.1:1936/
as given in our conf

 username: test
 password: test

Haproxy Stats

which displays detailed info of all servers. Cool stuff right!

That's it. Thanks for reading . These entire experiment available in  here

Happy Hacking !

Leave a comment

(Note: Comments are moderated)