Load Balancer on AWS

Divyanshu Sharma
4 min readMar 18, 2021

What is a load balancer?

Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool

A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If a single server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.

The client approaches a load balancer which takes request and passes it to the system containing the real website. These systems are also known as the Backend server.

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 spread requests across multiple servers. It is written in C and has a reputation for being fast and efficient.

Now we will set up it in AWS Cloud.

Launch the instances namely controller node, load balancer, webserver. Install the ansible in controller node by these commands:

dnf install python3

pip3 install ansible

The first step is to create a connection between all the systems. For the same, you can opt for the following ways -

  1. Transfer the AWS key in the controller node through WinSCP so that it can access the accounts.
  2. Create ssh password and set up the ssh configuration file.

We opt for the second method :

Install the sshpass using <yum install sshpass -y> . Set the password using <passwd ec2-user>. Make the changes in the inventory

Now we know the basics let's start to create the playbook which will perform the required task…

Create a controller node that will have ansible installed in it. It will hold the playbooks and the required prerequisites. Install the proxy software in the system using yum.

yum install haproxy

Create a workspace/directory inside the controller node. Use the mkdir command for the same.

Now shift the proxy configuration file to the workspace using the following command.

scp /etc/haproxy/haproxy.cfg controllernode_ip:<directory name>

Make the following configuration in haproxy file

Create the following playbook

Explanation :

Explanation :

The playbook runs two plays :

  1. The load balancer: Using the package module install haproxy software. Now using the template module we copy the created haproxy.cfg file to the load balancer. Next start the service.
  2. The web server: Using the package module install httpd and the PHP software. Next copy the file with PHP code to the webserver . Lastly, start the service.
  3. The become keyword following every task aids in providing the authentication of the remote system so that you can execute the tasks.

Run the playbook

Run the playbook using the following command.

ansible-playbook -v <playbookname>

Check the configuration :

public_ip_loadbalancer:8080

--

--