Configuring Loadbalancer HaProxy On AWS EC2 Instance Using Ansible

Sathvika Kolisetty
6 min readSep 5, 2020

--

Problem Statement

Statement: Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

♦️ Provision EC2 instances through ansible.

♦️ Retrieve the IP Address of instances using the dynamic inventory concept.

♦️ Configure the web servers through the ansible role.

♦️ Configure the load balancer through the ansible role.

♦️ The target nodes of the load balancer should auto-update as per the status of web servers.

What is Ansible?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

What is HaProxy LoadBalancer?

HAProxy is a 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. It is written in C and has a reputation for being fast and efficient (in terms of processor and memory usage).

What is EC2 in AWS?

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction.

Prerequisites

  • Ansible installed
  • python3 with boto, botocore and boto3 libraries installed(To do anything related to AWS one must install the above-mentioned libraries)

If you are not familiar with how to install ansible just use the below commands to install ansible on your Virtual machine

yum install python3 
python3 -V

After the confirmation just run the following command

pip3 install ansible

After the confirmation just run the following command

/etc/ansible/ansible.cfg

Just to make sure ansible configuration file is created or not run the following

1. Provisioning EC2 instance

We are going to create Ec2 instance accessible over ssh from our IP only. For that, we will need to create an EC2 key pair.

Create an EC2 key pair and save the private key to file.

Our first step is to let Ansible create a new EC2 key pair. We register the output and then we can write the private_key contents into a local pem file in the current directory.

ansible-playbook key_gen.yml

We can see that key-pair have created in AWS

Now, We have to create a playbook and then we can create the instances for webserver and for the load balancer and attach this key-pair for login into the Instances. For this, we will write a playbook and run it by using the localhost. and save ansible-playbook as aws.yml that help us to provision 3 webservers and 1 load balancer instances on the top of AWS. Determine information about the default VPC and its subnets. Determine our public IP address and create a security group allowing ssh access from our IP address (only). Create an EC2 instance in the selected subnet and associated with the security group, and we’ll update our inventory with the new host. Create one more playbook for storing AWS access key ID and secret access key.

load balancer
for webserver

2. Dynamic Inventory In Ansible

Ansible inventory is a collection of IP addresses and groups upon which all the commands and module run. We can say its something like an IP database.

We can’t go manually and fetch the IP Address we use automation to save our time and to develop quickly. Here we have to use a dynamic Inventory Concept to fetch the IP Address.

https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.

Now we will download this file and make this executable and set the environmental variable as mentioned above then if we will run this file so we can see that we can Dynamically get the IP address of the Ec2 instance we also need one more file that is ec2.ini file

We have to update our configuration file so that our inventory can be dynamically updated. For this, we will have to change the ansible Configuration file and update the location of our private key so when the Ip will be fetched we can also ping and perform our tasks.

Now ansible will automatically run the ec2.py file and retrieve the IP from the provided credentials of AWS EC2 instance. In order to connect to AWS EC2 instance, In general, we have to provide the username and password of a particular account but now we are using AWS instance so in this case, we have to provide the private key. So we have to provide the details in the ansible configuration file.

3.Creating roles

Now we have to create roles for the configuration of haproxy into the Load-Balancer instance and httpd into the Web-Servers instances.

Use the following command for creating roles load balancer and webserver

ansible-galaxy init <rolename>

Configuring Haproxy service inside the load balancer role

Handler

HaProxy.cfg file

Configuring httpd server inside the webserver role

We have to configure webserver At this stage, we’ve got all our tasks set up inside the webserverrole and our roles/webserver/tasks/main.yml

We can create a playbook in the root project directory note that we specify hosts as webserver and load balancer for the AWS infrastructure tasks.

The webserver is configured in all the 3 hosts

We can see that load balancer is configured as well

After successful configuration, we can see that webpages are running on the webserver.

Every time you refresh you will get different IP and the load on a web server is distributed with Load Balancer

Thanks for reading...

--

--