Configuring Haproxy Load Balancer over AWS EC2 instances with the help of Ansible Roles and dynamically adding each host IP’s in the managed node using Jinja Template
Problem Statement
Task Description 📃
🔅Create an ansible role to configure Httpd Webserver.
🔅Create another ansible role to configure Haproxy LB.
🔅We need to combine both of these roles controlling webserver versions and solving challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.
🔅Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.
🔅Configure the setup over AWS using instance over there.
Some Pre-requisites
What is a load balancer?
Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.
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 spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient.
What is AWS EC2?
Amazon Elastic Compute Cloud is a part of Amazon.com’s cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers on which to run their own computer applications.
What is an Ansible Role?
In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. … Roles are not playbooks. Roles are small functionality which can be independently used but have to be used within playbooks.
What is Ansible , Ansible Playbook and Ansible Inventory?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.
An Ansible playbook is a blueprint of automation tasks — which are complex IT actions executed with limited or no human involvement. Ansible playbooks are executed on a set, group, or classification of hosts, which together make up an Ansible inventory.
The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. The inventory file can list individual hosts or user-defined groups of hosts.
What is Control Node and Managed Node in Ansible?
A control node is a Linux server that has Ansible installed on it and is used for managing remote hosts or nodes. These remote systems are known as Managed Hosts or Managed nodes. Ansible is only needed to be installed on the control node and not the managed hosts.
What is Jinja Template in Ansible?
Jinja2 templates are simple template files that store variables that can change from time to time. When Playbooks are executed, these variables get replaced by actual values defined in Ansible Playbooks. This way, templating offers an efficient and flexible solution to create or alter configuration file with ease.
Solution Steps
Step 1:- Launch4 Redhat AWS EC2 instances on the AWS cloud .
Step 2:- Login into your controller node and create a role for loadbalancer and the web server using the command:-
ansible-galaxy init lbserver
ansible-galaxy init webserver
Step3:- Edit the Ansible Inventory file with the list of hosts public IP and Configuration file with the roles location and inventory location like:-
Inventory File:-
Configuration File:-
Step 4:- Create a user named geetu in all the managed node with the command:-
useradd geetu
Now, create an ssh key using the command:-
ssh-keygen
Then copy this key to all the systems using the command:-
ssh-copy-id geetu@IP
Step 5:- Install Haproxy software on the controller node. Edit its configuration file , using the Jinja2 template like:-
So, that managed node’s IP is dynamically added from the Ansible inventory file to the HAProxy.cfg file of the load balancer as soon as a new managed node is added in the inventory.
Copy HAProxy.cfg file with the above changes in the template directory of the lbserver role directory.
Step 6:- Write the following code in the playbook (main.yml) present in the tasks directory of the lbserver role directory:-
Also, write the following code in the playbook (main.yml) present in the handlers directory of the lbserver role directory:-
Step 7:- Create an index.php file with the following code to be displayed on our client’s browser, when accessing the content on the 3 web servers via the load balancer.
Step 8:- Write the following code in the playbook (main.yml) present in the tasks directory of the webserver role directory:-
Step 9:- Now, create a playbook to run our roles like:-
Step 10:- Now, lets execute our roles using the playbook via the command:-
ansible-playbook playbook.yml
Step 11:- Let us check whether the softwares are installed and the services are running or not, in our managed nodes:-
Hence, jinja2 template worked well with our ansible inventory file.
Hence, Haproxy is successfully installed and running in my lbserver.
Hence, my 3 apache webservers are active and running successfully with private IPs
172.31.32.81
172.31.36.184
172.31.47.156
Step 12:- Now, lets access the content deployed by our web servers on the internet through the load balancer’s public IP and its binding port number 8080 on our web browser. Here, my load balancer’s public IP is 13.234.117.246
Refresh the page to view content from different web servers
Hence, our content is successfully deployed and the load balancer is also working fine, by balancing the load among the three backend servers successfully.