ANSIBLE - CREATING LOAD BALANCER ON AWS USING ANSIBLE
Apurv Waghmare
6k+ & Growing Linkedin Family|| DevOps Specialist at Amdocs || Docker || Kubernetes || 1X AWS || 2X Azure || Ansible || Terraform || Jenkins ||SAFe 6 certified
Hello guys !! Back with another article, In this article you will find how we can create a load balancer on AWS using Ansible.
What is 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.
So lets start this task.
Create 5 ec2 Instances. Here i am taking mixture of t2.micro and t3.micro. You can take any instance type. But be sure take t3.micro for Ansible Node. In which you have to Install Ansible.
1 - - Ansible Node (Controller Node) -- t3.micro
1 - - LoadBalancer -- t2.micro
3 - - Webserver -- t2.micro
here i have used ansible to launch ec2 instances. code for launching ec2 instance using Ansible.
Below is the figure you will understand better. ??
Now we have to first create a role inside ControllerNode. Create two roles one for LoadBalancer and another for Webservers.
Set the webservers and loadbalancers inside ansible hosts.
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 loadbalancer ---> role created for LoadBalancer
Also create a directory to manage hosts files. Here i have created
below are file which i have created handlers for both loadbalancer and webserver
mkdir /etc/ansible/roles/projects
Also you need to set the path of role inside ansible configuration file (ansible.cfg).
Now we can start writing the playbook inside projects directory.
- hosts: WebServer roles: - role: webserver - hosts: LoadBalancer roles: - role: loadbalancer
After that we have to configure webserver of each instance. We have write the tasks in tasks folder and handlers in handlers folder.
now we can write the tasks inside main.yml.
- name: "install httpd" package: name: "httpd" state: present - name: "copy the content" copy: src: "/etc/ansible/roles/index.html" dest: /var/www/html/ - name: "restart httpd" service: name: "httpd" state: started
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 8080. also you 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.
backend app balance roundrobin {% for host in groups['WebServer'] %} server app1 {{host}}:80 check {% endfor %}
Now go to configuration file of haproxy -- > /etc/haproxy/haproxy.cfg and copy the haproxy.cfg and paste inside /etc/ansible/roles/loadbalancer/templates. You can also give the location of configuration file of haproxy.
Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/projects.From here we can run the play book.
Now we can check the weather our load balancer is working or not. Take public IP of load balancer with port 8080 (binding port)
As You can see we have created the load balancer using Ansible on AWS.
I hope you had liked the article. Thank you.