How to Setup HAProxy for Load Balancing On Ubuntu 16.04

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

HAProxy (High Availability Proxy) is an intelligent software solution that offers load balancing and high level of uptime performance for TCP and HTTP based applications.

It is used by world’s highly trafficked websites including; Twitter, Tumblr, Amazon Web Service and GoDaddy to spread incoming requests across multiple endpoints.

HAProxy works by distributing concurrent connections to multiple backend servers based on a load balancing algorithm. Written in C programming language, the software has been in use since 2000 and has a good reputation in regards to memory and CPU usage

In this guide, we will show you how to use HAProxy on your Ubuntu 16.04 Alibaba Cloud Elastic Compute Service (ECS) to prevent unplanned outage caused by software problems, human error, network error and environmental issues.

Prerequisites

  1. A valid Alibaba Cloud account (sign up now for a free trial)
  2. 3 ECS instances running Ubuntu 16.04 Operating System
  3. A non-root user that can perform sudo privileges on all 3 instances

We will be using one ECS instance as the frontend and two more as endpoints where the load is going to be distributed. We will use Alibaba Cloud ECS instance private IP addresses for the two endpoints.

We will also require the public IP address for the frontend server for accessing your web application or website. We will still need to connect to all 3 instances via public IP addresses through SSH to install all required applications.

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

  1. haproxy-server : public IP address 198.18.0.1
  2. backend-server1 : private IP address 172.16.0.1, public IP address 198.18.0.1
  3. backend-server2 : private IP address 172.16.0.2, public IP address 198.18.0.2

Also, you need to create a security group that allows the following ports for the servers:

  1. HaProxy-server : Port 80 http and port 32600 for statistics
  2. backend-server1 : Port 8080 for http
  3. backend-server2 : Port 8080 for http

Step 1: Configuring haproxy-server (Frontend)

SSH to the first ECS instance using its Public IP address. This is where we are going to install HaProxy Server.

Before we do this, we need to update the package information index using the command below:

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

Press Y and hit Enter when you are prompted to confirm the installation.

Step 2: Configuring HaProxy

When HaProxy is installed, a standard configuration file is created at /etc/haproxy/haproxy.cfg. We will need to edit this file to do some changes using a nano editor:

The file should like this before any edits are done:

The global section lists different parameter such as user and the group under which HAproxy runs. The defaults section handles login and error related issues. These two sections should work by default.

However, the file does not contain any load balancing information and we need to create the frontend and backend settings for our servers.

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:

Roundrobin specifies the balance algorithm that we want the server to use. Forwardfor option instructs the load balancer to forward client IP address to the endpoints. Http-request header allows us to forward the port and protocol made by the client.

Option httpchk checks the health of the endpoint before forwarding requests. The last two lines specify the hostname and private IP address of the backend servers. You should obtain the private IP addresses of the backend servers from the Alibaba ECS console.

We will also add stats settings using the below entries:

The bind parameter specifies the port that you want to use when retrieving the stats on your HaProxy server. You should allow access this port under the security group of your ECS instance. At the end of the file, there is an option to enter a username and password for login to the statistics web page. Enter a strong value for the password

At the end, your /etc/haproxy/haproxy.cfg should be as follows:

Remember to replace the backend server private IP address with the actual private IP addresses for your Alibaba ECS instances.

Then, restart HaProxy server to reload the changes:

Step 3: Configuring the First Backend Server (backend-server1)

Next, login to the first backend server and change the hostname to backend-server1 using the command below:

Change the one line to:

Then, edit the hosts file:

Add a second line with the IP address 127.0.1.1 and the name of the new host:

Press CTRL + X , Y and Enter to save the file.

Reboot backend-server1

Wait for a few moments, SSH back to backend-server1 and update the package information list:

Then, install Apache web server using the command below:

Change Apache listening port to port 8080:

Look for the line

And change it to:

Press CTRL + X , Y and Enter to save the file.

Then open the default Apache virtual host file and change the port to 8080:

At the beginning of the file, look for the line:

And change it to

Restart Apache:

Next we need to create a sample website on the root of the website.

We first delete the default index.html that ships with Apache:

We can now create a test web page for our server:

Copy paste the content below and save the file :

Step 4: Configuring the Second Backend Server (backend-server2)

We need to configure our second backend server just like we have done for the first server. We start by changing the hostname

Change the one line to:

Then, edit the hosts file:

Add a second line with the IP address 127.0.1.1 and the name of the new host

Press CTRL + X, Y and Enter to save the file.

Reboot backend-server2:

Wait for a few moments, SSH back to backend-server2 and update the package information list

Then, install Apache web server using the command below:

Change Apache listening port to port 8080:

Look for the line:

And change it to:

Press CTRL + X, Y and Enter to save the file.

Then, open the default Apache virtual host file and change the port to 8080.

At the beginning of the file, look for the line:

And change it to:

Restart Apache

Next we need to create a sample website on the root of the website.

First, delete the default index.html that ships with Apache:

We can now create a test web page for our server

Copy paste the content below and save the file

Step 5: Testing the Configuration

We now have the correct environment for High Availability and load balancing on our Alibaba server. We can now visit our HaProxy server to see if the load is going to be distributed to our backend servers in a balanced manner.

On a web browser, type the public IP address of the server where you installed HaProxy:

You should see the below web page:

Try refreshing the page and see if the load balancer is going to send the request to the second server

If you see Backend Server 2 is working on the browser, it means the Roundrobin algorithm was able to forward the request to the second server.

Congratulations, you now have a high availability configuration for your website or web application.

Step 6: Reviewing HaProxy Stats

You can visit HaProxy stats page by typing the public IP address of HaProxy server followed by “:32600”. That is the port that we specified on the HaProxy configuration file and as indicated above, it must be opened on the security group associated with your ECS instance.

Log in using the username and password that you specified and you should see the below stats web page:

Conclusion

On this guide, we have taken you through the steps of configuring HAProxy server on your Alibaba Cloud ECS running Ubuntu 16.04. We have setup two web servers and demonstrated that load balancing is working as expected. You can now upload your website or application file and even connect the backend servers to your database to create a fully working load balanced HTTP service for your web application.

To learn more about load balancing on Alibaba Cloud, visit www.alibabacloud.com/product/server-load-balancer

Reference:https://www.alibabacloud.com/blog/how-to-setup-haproxy-for-load-balancing-on-ubuntu-16-04_594052?spm=a2c41.12125120.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store