HAProxy (Load Balancer) Configuration Using Ansible on AWS
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).
Task Description :
Deploy a Load Balancer on my local system and multiple Web Servers on AWS instances through Ansible!
- Provision EC2 instances through ansible.
- Retrieve the IP Address of instances using the dynamic inventory concept.
- Configure the web servers through the ansible role.
- Configure the load balancer through the ansible role.
- The target nodes of the load balancer should auto-update as per the status of web servers.
So lets begin the task …..
Launching EC2 instance using Ansible and setting up dynamic inventory
In this blog I have configured Ec2 instance using Ansible . You dont need to follow whole blog , just follow upto setting up of dynamic inventory.
Updating Inventory File
This file contains public IP,location f private key and connection type.
Updating Ansible Config File
Listing all host
Create a folder which contains all the roles
ansible-galaxy init webserver ---> role created for webserveransible-galaxy init lbserver ---> role created for LoadBalancer
Structure of Role Folder (make sure to set the path of role inside ansible configuration file (ansible.cfg)).
Writing main.yml that will run all roles
Now configuring webserver
Writing the tasks inside main.yml of webserver folder.
vim webserver/tasks/main.yml
Now configuring loadbalancer
Writing the tasks inside main.yml of lbserver folder.
vim lbserver/tasks/main.yml
There is a file that is called “haproxy.cfg”. This particular file is working as a template that is used to configure the haproxy dynamically according to the requirement. So we have to make some changes in this file .
We need to change the port number binding. You can use any port eg 1234. Here i have used 8080. Also we need to provide the public ip of all the instances with 80 port. To give Ip randomly here we can use Jinja Template to extract the hostname of each ec2 instance.
Running Playbook
Before Running the playbook, we should check whether the nodes are connected or not with the Control node. For this, we need to run the following command to check
ansible all -m ping
This ad-hoc command will check the host whether it is connected or not and give the output “pong” if the nodes are connected successfully or pingable.
The following output can be seen
18.191.192.66| SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.139.69.93| SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.21.159.243 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
Great! It’s working fine…
Now, it’s time to run the playbook by using the following command
ansible-playbook main.yml#############Go to the Load Balancer Ip and port nohttp://<load_balancer_IP>:><port_no>/<web_page>
http://<load_balancer_IP>:>8080/index.php ####in my case
I am excited to announce the launch of my new Udemy course, “Apache Airflow Bootcamp: Hands-On Workflow Automation.” This comprehensive course is designed to help you master the fundamentals and advanced concepts of Apache Airflow through practical, hands-on exercises.
You can enroll in the course using the following link: [Enroll in Apache Airflow Bootcamp](https://www.udemy.com/course/apache-airflow-bootcamp-hands-on-workflow-automation/?referralCode=F4A9110415714B18E7B5).
I would greatly appreciate it if you could take the time to review the course and share your feedback. Additionally, please consider sharing this course with your colleagues who may benefit from it.