LOAD BALANCING WITH HAPROXY
Task Description
12.1 Use Ansible playbook to Configure Reverse Proxy i.e., HAproxy and update its configuration file automatically on each time new Managed node (Configured with Apache Webserver) join the inventory.
12.2 Configure the same setup as 12.1 over AWS using instance over there.
What is Ansible?
- Ansible is simple open-source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.
- Ansible uses playbook to describe automation jobs, and playbook uses quite simple language i.e., YAML (It’s a human-readable data serialization language & is commonly used for configuration files but could be used in many applications where data is being stored) which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML — It is in human readable form).
- Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through SSH (by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.
How Ansible Works..?
In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.
Ansible works by connecting to nodes (clients, servers, or whatever you’re configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.
What is HAproxy ?
HAProxy is a free, amazingly fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for extremely high traffic web sites and powers quite a number of the world’s most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the internet.
Let us start with the practical :
? Pre-Requisites:
1.Installed boto/boto3 in controller node
2.AWS account
3.Installed Ansible
4. Installed HAproxy in controller node
Here I’m checking the ansible version :
command : ansible — version
Now we have to go at managed node.. and check the IP of the managed node . When we can get the IP of managed node then go to the controller node and create the inventory file . After that create the configuration file by using the command.
Now we have to go at managed node.. and check the IP of the managed node . When we can get the IP of managed node then go to the controller node and create the inventory file . After that create the configuration file by using the command
Now we have to go at managed node.. and check the IP of the managed node . When we can get the IP of managed node then go to the controller node and create the inventory file . After that create the configuration file by using the command.
command : vim /etc/ansible/name_of_file.cfg
Now Create the inventory to all the hosts :
Command : vim file_name.txt
Check the connectivity between the controller node and target nodes using the following command:
Command : ansible all -m ping
Create an Ansible playbook for performing the practical using the following command add the following code in that file and save it :
Command : vim file_name.yml
Then we have to create the variable file to make it dynamic :
Command : vim var_file_name.yml
Create haproxy.cfg file using following command add the following code in that file and save it :
Here I am using jinja2 embedded code for dynamically fetch or register the new webserver IP with HAproxy loadbalancer.
Command : vim haproxy.cfg
Create index.php file add the following code in the file and save it:
Command : vim index.php
Now, we have run the Ansible Playbook by using following command :
Command : Ansible-playbook file_name.yml
Now, let’s check the outcome by visiting the HAproxy server IP with HAproxy port number .
Here have you notice , the IP is switching even though the IP address which I am using at browser is same. This proves that the HAproxy is working fine.
Now the challenge is to configure HAproxy automatically when a new IP is added in the inventory.
So now, let’s add one more target node in the webserver group. For this, open the inventory file and add a new target node.
Previously, we only had 2 target nodes, but now, we have 3.
Now Let’s check the connectivity between the controller node and target nodes using the following command:
Command : ansible all -m ping
Let’s run the playbook and check if the HAproxy server automatically adds the new target node in its configuration file or not.
Here, as you can see, the tasks are changed for the new target node. Now, let us visit the HAproxy once again and check if it has recognized and updated its configuration file with the new target node or not.
From the above output , you can see that 3 IPs are switching, which means that the third target node is also automatically added in the configuration file of HAproxy.
Thanks!!
Former SDE Intern @Raja Software Labs, Pune
3 年Great Explanation !!!