Introduction to Load Balancing with HAProxy

Introduction to Load Balancing with HAProxy

Written by Bobby Iliev on Jun 12th, 2019 Views Report Post

HAProxy is great solution for load balancing as it does not require lots of resources, it is scalable, easy to setup and fairly simple to manage. In this guide I'll show you how to install and configure HAProxy and load balance 2 Apache Web Servers.

The distro of my choice is CentOS 7 but you can use any distro you like.

I will go though the following:

  • What is HAProxy
  • Installation
  • Configuration
  • Quick demo
  • Statistics Report

What is HAProxy

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. For more information take a look at their official website: http://www.haproxy.org/

Installation

Thanks to its popularity HAProxy is available in the default CentOS repos. For the sake of simplicity I will use the version available in the default repo, though you need to keep in mind that this might not be the latest release of HAProxy.

In this case all you need to run is:

yum -y install haproxy

After that you can start the service and enable it:

systemctl start haproxy
systemctl enable haproxy

If you are planning to build a proper load balanced solution, I would recommend installing HAProxy on a separate box.


Configuration

You can find the HAProxy config file at: /etc/haproxy/haproxy.cfg

To keep things simple I would only use the following standard rules:

frontend http_front
   bind *:80
   stats uri /haproxy?stats
   acl url_admin path_beg /admin
   use_backend admin_back if url_admin
   default_backend http_back

backend http_back
   balance roundrobin
   server  :80 check
   server  :80 check

backend admin_back
   server  :80 check

In my case, thanks to the ACL rule named url_admin, all connections that match the /admin rule would be forwarded to the admin_back backend. That way I would make sure that all /admin connections would be forward to my admin server. You can of course adjust that.

HAProxy is very flexible so you could add lots of rules and different backend servers depending on your setup and requirements.

After making the configurations, save the file and restart HAProxy with the next command.

systemctl restart haproxy

The restart is super quick and should not cause any major downtime.

With this configuration our setup would look something like this:


Demo

To make things more clear, here's an example of how the above would work:

      • HAProxy server would be: bobby.haproxy
      • Server 1 would be called: bobby.server1
      • Server 2 would be called: bobby.server2

      Here's what happens when you make 10 http requests to the HAProxy server:

      You can see that the connections are being load balanced between the two web servers. That way if one of the web servers goes down for any reason (maintenance, hardware issue, upgrade etc.) your website would still be up and running from the second web server.

      Thanks to the ACL url_admin rule that we have, here's what happens when I make 10 http connections to my /admin page:


      Statistics Report

      HAProxy comes with very handy builtin Statistics Report, you will be able to see the status of your backend servers, their uptime, the number of handled connections and more.

      You just need to visit the status URL specified in your config, in my case I've set that to: /haproxy?stats

      You would see a similar page:


      Conclusion

      This is pretty much all that I wanted to cover here in this introduction article.

      I would not get into the different types of load balancing and etc. but I might cover those in future articles.

      If you have any questions, feel free to reach out to me!

      Comments (0)