HAproxy + AWS + Ansible

Deploy Load Balancer on AWS through ANSIBLE!

What is Load Balancing?

The Task We are going to perform is :

♦️ 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.

We are going to create environment like this :

So, Let’s Get started…

pip3 install boto

Create one playbook to launch the instances in AWS → I have used 2 webservers from which loadbalancer will be routing the traffic.)

1-Loadbalancer and 2-Webserver ---> Managed Nodes(MN)
Run the Playbook → ansible-playbook instance.yml

All Nodes are launched Successfully.

Now, We have to retrieve the Public IP of the instances dynamically in the inventory file.

So, Download the ec2.py and ec2.ini files.

 wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

To make the files executable, change the file permissions.

chmod +x ec2.py
chmod +x ec2.ini

Update the first line of the ec2.py file, which will tell the exact location of interpreter to execute the python script.

#!/usr/bin/python3

Now, Provide the AWS access credentials like this :

AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXXXX

We have fetched Public IP of MN → ansible all — list-hosts

Create two ansible roles one for LoadBalancer and another for Webservers.

ansible-galaxy init loadbalancer  ---> role created for loadbalancer 
ansible-galaxy init webserver ---> role created for webserver

Also We, need to set the path of role inside ansible configuration file (ansible.cfg)

roles_path=/Ansible_Task_3

Now, we can write the tasks inside tasks/main.yml file to configure the loadbalancer.

- name: INSTALL HAPROXY SERVER
package:
name: haproxy
state: present
- name: COPY HAPROXY CONF FILE TO LB
template:
dest: “/etc/haproxy/haproxy.cfg”
src: “haproxy.cfg.j2”
notify: LBrestart
- name: START THE SERVICE
service:
name: haproxy
state: started

write handlers inside inside handlers/main.yml

- name: LBrestart
service:
name: haproxy
state: restarted

First install haproxy at the Controller Node

yum install haproxy -y      ---->  It will install haproxy

Inside the configuration file of haproxy (haroxy.cfg.j2) We need to change the port number binding, to which our client is going to request for the webpages. You can use any port No. Here, I have used Port 8080.

Also We need to provide the public IP of all the instances with Port 80 . We have used Jinja Template to extract the hostname of each ec2 instances dynamically by using the predefined variable “tag_Name_web”.

backend app
balance roundrobin
{% for i in groups[‘tag_Name_web’] %}
server app{{ loop.index }} {{ i }}:80 check
{% endfor %}

Copy the haproxy.cfg.j2 file and paste it into the /Ansible_Task_3/loadbalancer/templates directory.

Now, Write the tasks inside tasks/main.yml file of webserver role to configure the Webservers.

- name: INSTALL HTTPD PACKAGE
package:
name: httpd
state: present
- name: COPY THE FILE
template:
dest: "/var/www/html/"
src: "index.html"
notify: restart
- name: START THE SERVICE
service:
name: httpd
state: started

Now, We can wirte the main playbook to run all the tasks.

- hosts: tag_Name_LoadBalancer
roles:
- role: loadbalancer
- hosts: tag_Name_web
roles:
- role: webserver

We are good to run the playbook now!

ansible-playbook setup.yml

Check the working of your Loadbalancer by using its Public IP and its Port No.

13.127.23.104:8080      ---> This is for my case

The final output will look like this :

Finally, Our LoadBalancer has been deployed successfully.🤘

Thank You For Reading!😇

Started Learning New Technologies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store