Deploy A Load Balancer And Multiple Web Servers On Aws Instance Through Ansible

This article will help you to know that How we can launch HAPROXY Loadbalancer and Multiple WEBSERVERS on the top of AWS Cloud with the help of ANSIBLE.

What are Load Balancer?🤔

What is HAproxy?🤔

What is Ansible?🤔

What we are going to do throughout this article?

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

Prerequisite

  • Ansible should be installed in the system.
  • Boto3 library should be installed in the system to work with aws through ansible.
  • Should have an AWS account.
  • Should have basic knowledge of AWS.

Now, we can start

We can also do this through ansible only but here we will be doing it manually.

For this go to your AWS account and search for Ec2 service. After that click on Security Groups on the left side. Here on the top, you will find the Create Security Group option click that.

Give Security group name, Description and in InBound rules and OutBound rules allow these rules.

Now, Let’s create a Playbook for the ec2 instance.

This playbook will launch One LoadBalancer and Three WebServers according to our need we can change this limit. In this Playbook, we will be using an ansible vault for storing our AWS credentials.
secret.yml is the name of our ansible vault file.

playbook

- hosts: all
vars_files:
- secret.yml
tasks:
- name: "LoadBalancer"
ec2:
key_name: "Abhinav"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 1
instance_tags:
Name: LoadBalancer
vpc_subnet_id: "subnet-c3feb38f"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-0776e54adea8e690e"
aws_access_key: "{{ access_Key }}"
aws_secret_key: "{{ secret_Key }}"
- name: "WebServer"
ec2:
key_name: "Abhinav"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 3
instance_tags:
Name: WebServer
vpc_subnet_id: "subnet-c3feb38f"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-0776e54adea8e690e"
aws_access_key: "{{access_Key}}"
aws_secret_key: "{{secret_Key}}"

Now, let’s create an ansible vault to store the AWS access key and secret key.

ansible-vault create --vault-id Abhi@prompt AWS_Instance.yml

Now, let’s run this playbook and see whether our paybook works or not.
To run the playbook

ansible-playbook --vault-id Abhi@prompt <playbook name>

Let’s move to the AWS portal look at our 3 backend servers and 1 load balancer has been launched.

Now, we have to create an inventory of these instances so that we can work on them. We can fetch the IP of all instances in two ways either in the static way going to each instance finding their IP and then updating it in the Inventory file or using Dynamic inventory.

In this article, we will be using Dynamic inventory.

To create a dynamic inventory -

  • Create a directory with any name.
  • Here we are working on AWS Ec2 Instance therefore we will be downloading python scripts for AWS created by the Ansible team.

We have to download two files

  • ec2.py
  • ec2.ini

To download both the files

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

Now, we have to change these files a little bit

In our system we have python3 installed so, we have to change a python interpreter from python python3 in ec2.py file.

Now, let’s check that if it is working or not by running

ansible all --list-hosts

If all things were correct we will see all the IP addresses of our launched instances in AWS.

Now, we will be adding all these IP’s to our static Inventory file.

For this, we will need the private key which will be useful to connect with the ec2 instance.

The user will depends on instance type I have used RedHat AMI therefore user will be ec2-user only.

We will be creating two groups in our static inventory file one for LoadBalancer and the other for WebServer.

Now, let’s check that if we have connectivity with all the instances or not.
To check connectivity we will ping each instance. For this command is

ansible all -m ping

We have connectivity with all our instances.

Now, let’s move to further steps.

Till here we have launched instances over the cloud and fetched their IP’s dynamically.

Next, we will be installing HAPROXY and HTTPD in the launched instances.

For this, we will be creating two roles load balancer and webserver.

Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.

For creating roles

ansible-galaxy init webserver
ansible-galaxy init loadbalancer

First, we will be configuring our WebServers.

Go to the tasks folder inside the webserver role and open main.yml.
Here we will be install httpd, will copy the content, and then start the HTTP server.

For this, we will be writing a playbook.

- name: install httpd
package:
name: "httpd"
state: present
- name: copy content
copy:
content: "hi from {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: sevice restart
service:
name: "httpd"
state: started

Now, We will configure LoadBalancer.

Open loadbalancer role then go to task folder and open main.yml.
Here we will install haproxy, configure haproxy.cfg file and finally start service.

First let’s see how to edit haproxy.cfg file.

We have to configure haproxy.cfg file also through which our load balancer will come to know about our backend servers.

For this we will copy the haproxy.cfg file from the internet, edit it and then copy it to our load balancer.

We need to store the haproxy.cfg file inside this “templates” folder.

Changes to be made in the file:

  • In haproxy.cfg file in line number 68 the port no should be “8080”.
  • In line 88, we need to mention our backend servers with IP address.

Here, we are updating this file dynamically therefore we will use Loops here.

Now, we will write playbook for LoadBalancer.

Playbook for load balancer

- name: install httpd
package:
name: "httpd"
state: present
- name: copy content
copy:
content: "hi from {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: sevice restart
service:
name: "httpd"
state: started

Now, open the handlers folder and edit the main.yml file.

- name: haproxy restart
service:
name: "haproxy"
state: restarted

We have to tell ansible that we are using roles in our ansible file for this we have to add a variable in ansible.cfg file.

roles_path = path of your role folder

Just create a playbook for running our roles on inventory groups.

- hosts: Webserver
become: yes
roles:
- role: webserver
- hosts: Loadbalancer
become: yes
roles:
- role: Loadbalancer

Just run the playbook and we are good to go.

To run the playbook

ansible-playbook <file_name.yml>

Done.

Now check that all the things were correct or not.

Our httpd is installed on all the web servers. Content is copied in the folder and service is also started.

Also, our load balancer is configured now. Haproxy installed, haproxy.cfg file copied, and service started.

To check the output

Copy the public IP address of LoadBalancer and paste it in the browser with port number(8080).

Our LoadBalancer is balancing the traffic.

--

--

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