CREATING LOAD BALANCER ON AWS USING ANSIBLE Roles
Surayya Shaikh
1x RedHat Certified | ARTH LEARNER | RHCE | Kubernetes | DevOps | Docker | Linux | Python | AWS
Hello guys !! Back with another article, In this article, you will find how we can create a load balancer on AWS using Ansible Roles...
Task Description:-
?? Create an Ansible role for Apache to configure Httpd WebServer.
?? Create another Ansible role for loadbalancer to configure HAProxy LB.
?? We need to combine both of these roles controlling web server versions and solving the challenge for host IP's addition dynamically over each Managed Node in HAProxy.cfg file.
What is Ansible ROLE???
- As we develop larger and complex playbooks, we often discover opportunities where we can reuse the same code from playbooks. But, in larger playbooks, many imported files and handlers may present, and copy whole content may not be a good call!
- Ansible Role provides a way to manage the play and enhance the chances of reusability of the code. We can bundle the play in a directory structure in a standard manner. So, copying the role is as simple as copying files and directories.
- The role enables the playbook to be highly scalable?? and easily sharable and a place with batter management.
Ansible Galaxy is a centralized repository by the community where a huge amount of pre-created roles are available and can be freely downloaded and used. multiple time tested roles are available here.
So let's start this task...
Create 3 ec2 Instances. Here I am taking type t2.micro. You can take any instance type. And my controller node is on VirtualBox.
1 — — Ansible Node (Controller Node) — VirtualBox
1 — — LoadBalancer — t2.micro
2 — — Webserver — t2.micro
Now we have to first create a role inside ControllerNode. Create two roles one for LoadBalancer and another for Webservers.
The default location for the roles is /etc/ansible/roles
create a role folder mkdir /etc/ansible/roles all the roles will be inside this roles folder.
ansible-galaxy init WebServer ---> role created for webserver ansible-galaxy init LbServer ---> role created for LoadBalancer
Also, create a directory/workspace to manage host files. Here I have created.
mkdir /etc/ansible/roles/haproxyWS
Here a complete picture of the above explanation looks like.
Also, you need to set the path of the role inside the Ansible configuration file (ansible.cfg).
Set the webservers and loadbalancers inside ansible hosts inventory file...
After this check that the managed nodes pingable or not...
Now we can start writing the playbook inside /etc/ansible/roles/haproxyWS directory.
After that, we have to configure webserver of each instance. We have to write the tasks in the tasks folder and handlers in the handlers folder.
Now we can write the tasks inside /etc/ansible/roles/WebServer/tasks/main.yml.
Here we have completed with configuring webserver. You can also use Handlers to restart apache server. Now we have to configure the Load Balancer. First install haproxy inside Controller node.
yum install haproxy -y ----> it will install haproxy
Then you need to change the port number binding. You can use any port eg 1234. Here I have used default port 5000. also, you need to provide the 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. As Ansible supports python jinja2 conventions, use the loop that prints the IP address of the servers with the pre-defined group keyword of Ansible. Mention the port (80 default) of the httpd service.
This haproxy.cfg file put in /etc/ansible/roles/LbServer/templates folder.
And tasks for LbServer Role is as follow...
Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/haproxyWS folder From here we can run the playbook.
ansible-playbook playbook name
Now we can check whether our load balancer is working or not. Take public IP of load balancer with port 5000 (binding port).