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

๐—›๐—ฒ๐—น๐—น๐—ผ Friends !! Welcome you all to my article on Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud 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: Deployment of Haproxy Load Balancer and Multiple Web Servers on AWS Cloud 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: 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

Spread the love

Leave a Comment

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