How to Install HAProxy Load Balancer on Ubuntu 18.04 Bionic Beaver

Install HAProxy Load Balancer on Ubuntu 18.04

In this article, we will have explained the necessary steps to install and configure HAProxy Load Balancer on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

HAProxy is an open-source Linux tool that provides high availability load balancing and proxy services for TCP and HTTP-based network applications. Fue to its easy integration into existing architectures, suitability for high-traffic websites, extreme reliability, and focus on upwards compatibility, it is shipped by default by most mainstream Linux distros.

Install HAProxy on Ubuntu

Step 1. Network Details

For the sake of simplicity, we will assume the following IP addresses and hostnames for the instances:

  • haproxy-server : public IP address
  • backend-server1 : private IP address, public IP address
  • backend-server2 : private IP address, public IP address

Step 2. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

Step 3. Install HaProxy on Ubuntu 18.04 LTS.

HaProxy is available on the Ubuntu software repository, so we can install it using the package manager by running the command below:

Step 4. Load Balancing Configuration with HAProxy.

Now edit haproxy default configuration file /etc/haproxy/haproxy.cfg and start configuration:

So towards the end of the file, add the content below:

The bind parameter tells HaProxy to listen to port 80 for connections. At the end of the text, we have specified ourwebsiteendpoint as the directive where our endpoints are located. We can now go ahead and add the backend configuration details as follows:

Now if you want you can enable Haproxy statistics by adding following configuration in HAProxy configuration file:

Step 5. Final HAProxy Configuration File.

The final configuration file may look like below:

Then, restart HaProxy server to reload the changes:

Step 6. Testing the Configuration.

At this stage, we have full functional HAProxy setup. At each web server node I have a demo index.html page showing servers hostname, So we can easily differentiate between servers web pages.

Now access port 80 on IP (as configured above) in the web browser and hit refresh. You will see that HAProxy is sending requests to backend server one by one (as per round robin algorithm).

That’s all you need to do to install HAProxy Load on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.