Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!
By: Arnab Saha
Tags: AWS, Haproxy, Ansible, Load Balancer, Redhat Linux

!! 𝗛𝗲𝗹𝗹𝗼 all!!

Welcome to my article based on TASK-3 of RH-294 -Linux Automation using Ansible (RHCE8) Training

🔰 TASK DESCRIPTION:

🔅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.

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 let’s start this task.

Create 5 ec2 Instances. Here I am taking a 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.

An image guide for better understanding.

Now we have to first create a role inside ControllerNode. Create two roles one for LoadBalancer and another for Webservers.

Set the webservers and load balancers 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

mkdir /etc/ansible/roles/projects

Here a complete picture of the above explanation looks like.

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: web

roles:

- role: webserver - hosts: LoadBalancer roles:

- role: lbserver

After that, we have to configure the webserver of each instance. We have written the tasks in the tasks folder and handlers in the 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 the 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['web'] %}

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/lbserver/templates. You can also give the location of configuration file of haproxy.

- name: "install HAPROXY"

package:

name: "haproxy"

state: present

- name: "copy HAPROXY configuration files to LoadBalancer"

template:

src: "haproxy.cfg"

dest: /etc/haproxy/

notify: lb restart

- name: "restart HAPROXY"

service:

name: "haproxy"



state: "started"

Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/projects.From here we can run the playbook.

ansible-playbook playbook name

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 the load balancer using Ansible on AWS is in action.

Summary: One-Click Instance Launched, Web Servers provisioned and Load Balancer ready!

If you find it useful please upvote it.

Connect with me in Linkedin | Github