Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

How to deploy Haproxy load balancer on AWS using Ansible

𝗛𝗲𝗹𝗹𝗼 Friends !! Welcome you all to my article on How to deploy Haproxy load balancer on AWS using Ansible. An overview of Ansible and AWS Services will also be present. Finally How can we utilize Ansible Scripts or Playbooks with AWS and Load Balancer for webserver deployment? We are also utilizing or building Ansible Playbook to create this application, as well as installing the Haproxy Load Balancer.

DESCRIPTION OF THE TASK: How to deploy Haproxy load balancer on AWS using Ansible

  • Using ansible, provision EC2 instances.
  • Make the dynamic inventory idea, you may get the IP addresses of instances.
  • Using the ansible role, configure the web servers.
  • make the ansible role, configure the load balancer.
  • The load balancer’s target nodes should auto-update based on the state of web servers.
  • ⏩ We will deploy one HAPROXY load balancer on the AWS cloud and integrate it with numerous webservers operating on the AWS cloud in this work. Using Ansible Automation, the whole setup and configuration was completed in a single click.

Now let’s see how Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

COMPLETION OF THE TASK:
1) For AWS Cloud, everyone who contacts them is considered a client.

We’ll also use Ansible to start an instance on AWS Cloud, but how will Ansible communicate with AWS Cloud and launch the instance? Ansible uses Python libraries to setup everything behind the scenes. As a result, for AWS Cloud Python, there is a package called boto that connects Ansible to the AWS API too. Β 

To install boto library use the command –>
pip3 install boto
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
2) To use Ansible to deploy a load balancer and webservers on AWS Cloud, we also utilize the Ansible ec2 module, which instructs Ansible to start an ec2 instance for the load balancer and webservers.

Playbook for Deploying Webserver Instances –>

Ansible Playbook
Running the webserver instance playbook –>
ansible-playbook --ask-vault-pass playbook_name
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Ansible Playbook
Ansible Playbook
Playbook for Deploying Load Balancer Instance –>
Webserver
Running the Load Balancer Instance Playbook –>
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
βœ… Now we can also see all the Instances are successfully launched through AWS GUI EC2 Console.
AWS Instances
3) After the instances have been successfully launched, we must also obtain their public IP in order to configure them as a webserver and load balancer using Ansible. We’re also going to leverage the Dynamic Inventory idea to automatically obtain the target IP.

We utilize some python script code to generate the Dynamic Inventory, which can connect to the AWS cloud and retrieve the public IPs of active Instances.

Download the python scripts ec2.py and ec2.ini to dynamically get the IP address.

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Now we also have to make these scripts files executable by changing the file permissions using command –>

chmod +x ec2.py

chmod +x ec2.ini
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

As Ansible, you can also now connect to these python script files and navigate to the AWS Cloud and jobs. These scripts files needed to export various variables, which are listed below.

export EC2_INI_PATH='ec2.ini_file_path'

export AWS_ACCESS_KEY_ID='aws_access_key'

export AWS_SECERT_ACCESS_KEY='aws_secret_key'
4) Finally, we use the dynamic Inventory to dynamically obtain the IP of all Instances without the need for an Ansible Inventory file. Now we need to build an Inventory file for Ansible (/etc/myhosts.txt), in which I construct two groups named webserver and loadbalancer, which list the IP addresses that we acquired using dynamic Inventory.
Loadbalancer
Checking the connectivity of controller node with all target Instances using command –>
ansible -m ping all
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Checking the list of all target hosts using command –>
ansible --list-hosts all
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
5) In Ansible World, creating Roles for various activities is always a smart practise in terms of effective administration and testing.

Ansible Roles are similar to work space folders that contain various files such as variables, tasks, and so on. As for our task, we must use the following command to create two separate roles, one for setting the webserver and the other for configuring the HAPROXY Load Balancer.

ansible-galaxy init role_name

We have to provide the path of destination where we created the roles to ansible configuration file .

Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

We can see all the roles we created using the command –>

ansible-galaxy list
Ansible galaxy
6) Now, let’s go to the heart of our problem: the HAPROXY Load Balancer.

We need to deploy the HAPROXY load balancer service on the AWS Instance we established, but there are a few adjustments we need to make to the HAPROXY cfg file first. To do this, I first installed the HAPROXY software on the controller node and made the following changes to the cfg file.

To install HAPROXY software use command ->

yum install haproxy -y
Changing some part in HAPROXY cfg file –>
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

We update the frontend main field to 8080 to bind and expose the HAPROXY load balancer to the public world, as seen in the above haproxy.cfg file. The load balancer must first know their back end servers, target instances, or nodes in order to do load balancing.

Since we changed the backend app field, the targets we configured as AWS web server instances should now automatically register with the HAPROXY load balancer.

This also allows us to work more quickly because we don’t have to manually register the instances with the HAPROXY load balancer.

7) To complete our responsibilities, we must also now employ the roles we defined. We also utilize the role we defined called webserver to configure web server on the instances we run on AWS. The tasks folder in this role contains the main.yml file. We must also write the task for installing and launching httpd services on instances in the main.yml file.

Ansible Command

8) We also utilize the same role we generated named lb for HAPROXY load balancer setup on AWS Instance created. For configuring HAPROXY load balancer on AWS Instance, we must write the task in the main.yml file located in the tasks folder.

Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

Handler file for HAPROXY Load Balancer –>

Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

9) Finally, the roles for the web server and the HAPROXY load balancer have been correctly set. Now we also develop a task3.yml playbook that handles the configuration of the web server and HAPROXY Load Balancer on AWS Instances.

Script

One click the entire setup of deployment ofΒ HAPROXY Load Balancer and Web serverΒ on AWS done as shown below .

ansible-playbook playbook_name
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Haproxy

10) Now go to the AWS GUI Console and login to the Load Balancer Instance using SSH to test if Ansible correctly configured the HAPROXY services.

AWS  ec2 Cli

Finally we also see that HAPROXY services are properly configured and are in running status . Besides It Going inside haproxy.cfg file we can see that all the web server Instances we launch using Ansible has registered as Target Nodes or Target Instances for HAPROXY load balancer .

Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

11) At last we can also see the load balancing of web server Instances by HAPROXY Load Balancer using public ip of Load Balancer Instance .

Haproxy
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible
Loadbalancing
Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

Related Articles: How to deploy Haproxy load balancer on AWS using Ansible

Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud Using Ansible

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top