Configuration Of Haproxy Using Ansible....
✨Article Description:-
In this Article we are going to see how to 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.
💥What is HAPROXY 🤔
HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database). It is used in many high-profile environments, including: GitHub, Imgur, Instagram, and Twitter.
💥What is load balancer 🤔
Load balancing ensures the availability, uptime and performance of your servers, websites and applications during traffic spikes. It’s especially important for companies with multiple data centers and high traffic websites or apps.
Load balancing is a performance optimization tactic that provides fault-tolerance by splitting incoming tasks across multiple computing resources, thereby preventing any one resource from becoming overloaded.
Load balancing aims to achieve optimal resource usage, maximize server stability, and prevent individual components from becoming overloaded. It divides the amount of work that a server has to do between two or more servers, allowing more work to get done in the same amount of time.
* Let's Start the particle ---->
In this particle by using Ansible, we are going to configure the haproxy server to handle traffic of webserver on the top of AWS instance .
first launch instances in AWS & use those instances for control node & manage node. So here we choosing instance1 as a control node & instance 2,3,4 as a manage node.
At control node side------->
Step1 :- First we have to install python3 at control node side because ansible is built on python language.
command to install python3:- yum install python3
Step2 :- Install ansible at control node side
command to install ansible:- pip3 install ansible
Here we can see our ansible is installed successfully
Step3 :- Create a Inventory file for manage nodes. here we creating inventory file using host group concept so we created two host group one for load balancer i.e "lbserver" & other for back-end servers i.e "backendserver"
Step4 :- Create a ansible configuration file & add some rules for defaults & Privilege escalation so that after login we can give the root power to ec2 instance.
Step5 :- Now let's check the host list and connectivity with manage node by using following command
here we can see we have three hosts & also we have great connectivity with them
* If we want to make our load balancer server more dynamic i.e after new backend server come up haproxy configuration file will automatically update the ip's of backend server then we have to do some changes in haproxy configuration file by using some jinja frame- work ------>
Step6 :- Now let's create a ansible playbook for manage nodes. Here we are writing a playbook by using "roles" . so let's know about Roles--------->
💥Roles :-
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. 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. The breaking of playbook allows you to logically break the playbook into reusable components.
Each role is basically limited to a particular functionality or desired output, with all the necessary steps to provide that result either within that role itself or in other roles listed as dependencies.
Roles are not playbooks. Roles are small functionality which can be independently used but have to be used within playbooks. There is no way to directly execute a role. Roles have no explicit setting for which host the role will apply to.
Step7 :- Create a roles for "backendserver" & "lbserver" using following command
ansible-galaxy init backendserver ansible-galaxy init lbserver
Step8 :- role for load balancer (lbserver)---->
To write a role for lbserver follow the following path & write a role inside the main.yml file
Step9 :- role for back-end server (backendserver)----->
To write a role for backendserver follow the following path & write a role inside the main.yml file
Here we create roles for both load balancer server & webserver
Step10 :- Now we have to call this two roles into our main playbook. So Create a main playbook to call a both roles i.e "lbserver" & "backendserver"
Here I am using below path to create a main playbook--->
Step11 :- Run the main playbook using the following command:-
" ansible-playbook <playbook_name.yml>"
Here our playbook run successfully,
Now lets check our haproxy working good or not....To check this give the Ip of haproxy /load balancer server to client & check whether the webpage is accessible or not...
Here 13.234.31.166 is a ip of my load balancer server & we can clearly see using this ip we can successfully access the webpage.
THANKS FOR READING.....😊