Ansible Blog-3

Deploying ⚖Load Balancer & Cluster of 🌐Web Server on ☁AWS using ANSIBLE

ANUPREET DUBE
Automation with RedHat Ansible
6 min readApr 26, 2021

--

📝Article Overview

Thi article will see how to create an Automated WebServer infrastructure using Ansible as the Automation Engine.

🛠 We will be using the following tools :

  • Apache webserver — For webserver deployment
  • Ansible — as an Infrastructure Automation engine
  • EC2 Service from AWS — To launch and configure OS for Web server deployment
  • HAProxy — For High Availability Load-Balancing

⚙ Infrastructure Description

Final Infrastructure

💡 EC2 instances configured with Apache web server will be launched on AWS Cloud as the backend. Another EC2 instance, configured with HAProxy, will work as the Load Balancer for the Backend servers. The whole environment will be created using Ansible.

🔰STEPS

  • Provision (Launch) EC2 instances on AWS through ansible.
  • Retrieve the IP Address of instances using the dynamic inventory concept.
  • Configure the webserver on 3 of the 4 AWS Instances through the Ansible Role.
  • Configure the load balancer on 1 AWS Instances through the ansible role.
  • The target nodes (Backend Webservers) of the load balancer should be auto-updated/Registered as per the status of web servers.

🔷Solution

📌 The initial steps for configuring Ansible Controller Node and AWS CLI have been explained in detail in my previous article :

🌐WebServer Deployment on Cloud ☁ using Ansible

(Refer to the above article till Step — 5️⃣)

◼ Continued From Step-6 ➡

6️⃣ Create a Playbook to Launch AWS EC2 Instance via Ansible

To make things easy, we can first create all the resources required for launching an instance from the WEB UI of AWS and then use them in the Playbook

So, we can first create and/or Retrieve the ID of the following resources from AWS Management Console :

  • AMI ID — The ID of the base image we want to use for creating an instance. AMIs are pre-created and we can directly use anyone among the Free-Tier
  • VPC ID — Virtual Private Cloud, a way to isolate resources in AWS
  • Subnet — To further isolate resources in a VPC. Can be Public/Private Subnet
  • Region and AZ — Geographical location of the DataCenter from which we wish to Retrieve the resources
  • Key-Pair — To log in to the instance for configuring it later. Download this Key-pair in BaseOs for Attaching this to instance later.
  • Vault Details- For access key and secret key of AWS IAM User
  • Security Group — To secure the instance by controlling the Ingress/Egress

We will be needing 2 separate SGs :

1. SG1 : For Backend Webservers

Ingress/Egress Rule — Port 80 → TCP , Port 22 → SSH

2. SG2 : For HAProxy Load Balancer

Ingress/Egress Rule — Port 8080 → TCP , Port 22 → SSH

Now, we can use the above information to create a playbook for launching EC2 instance :

gedit   webplaybook.yml
Webplaybook.yml Part-1
Webplaybook.yml Part-2

The playbook will do 2 Tasks :

  1. Launch 3 EC2 instances on AWS Cloud (will be configured as Backend Servers in future)
  2. Launch a separate EC2 instance on AWS cloud (will be configured as Load Balancer using HAProxy in future)

💡 The vault name used here is for the Vault we created in Step-3 to store the AWS IAM User credentials through which Ansible will access AWS Cloud.

7️⃣ Execute the Playbook to create an EC2 instance

ansible-playbook   --vault-id   <UniqueID>@prompt   webplaybook.yml

Provide the Password of Vault created in Step-3

💡We can verify the infrastructure through the AWS Management Console and matching the IP of the instances with the IP Displayed in the Playbook Verbose.

AWS EC2 Dashboard

Also, since we used the concept of Dynamic Inventory, the IPv4 Public IP of EC2 instance will be added to the Ansible Inventory on the fly:

ansible  all  --list-hosts

So, our instances are successfully launched on AWS Cloud through an Ansible Playbook.💯💯

🔰STEP-2: Configuring the Instances as Web Servers and Load Balancer

We will be creating Ansible Roles to achieve the desired environment.

1️⃣ Group the TO-BE Backend Servers and Load Balancer Separately

We want to configure 3 of the 4 instances as a webserver and the 4th instance as a Load Balancer, hence we need to create separate groups in the inventory :

Inventory

2️⃣ Create 2 Ansible Roles (for Webserver and Load Balancer)

Create 2 Roles in the Default Roles’ directory of Ansible (as mentioned in Config File of Ansible):

ansible-galaxy   init   webserver_role
ansible-galaxy init haproxylb_role

3️⃣ Configure the Role for Backend Web Servers

1. Write the tasks for configuring a web server in the following file:

gedit  webserver_role/tasks/main.yml
Webserver Role

The 3 main Tasks involved in configuring Apache Web Server are:

  1. Installation of Apache Web Server Software: HTTPD using Package module of Ansible
  2. Starting the Httpd web service using Service Module of Ansible
  3. Write/Copy the code of HTML Webpage in the Document Root of Apache Web Server: /var/www/html via Copy Module of Ansible

2. A Simple testing code for an HTML Page (index.html) could be :

<body bgcolor="green">
WebPage Deployment Successful !!

Put the above HTML Code as a Template in /webserver_role/template/index.html

4️⃣ Configure the Role for Load Balancer ⚖

1. Write the tasks for configuring a Load Balancer in the following file:

gedit  haproxylb_role/tasks/main.yml
HAProxy LB Role

2. Create Handlers to restart the service post an update.

gedit  haproxylb_role/handlers/main.yml

3. Create a Template for Haproxy Load Balancer :

  • Install HAProxy Package in Controller Node
  • Copy the default config file of HAProxy from /etc/haproxy/haproxy.cfg to /haproxylb_role/templates/ directory of Ansible Role for Load Balancer
  • Add the following at the End of the config file:
{%   for ip in groups['backendservers']   %} 
server app1 {{ ip }}:80 check
{% endfor %}

5️⃣ Create a Playbook for these Roles

To use the above-created roles, we need to create a playbook :

gedit   webdeploy.yml

Specify the name of the roles in this playbook:

- hosts: backendservers
roles:
- webserver_role
- hosts: loadbalancer
roles:
- haproxylb_role

Execute the playbook :

ansible-playbook    webdeploy.yml

On successful execution of the Playbook, we can see the Webpage in the Browser by typing the following URL :

http://<Public IP Of Load Balancer>:8080

The 3 Backend Servers will be connected alternatively depending on the Client Traffic and the Algorithm being used by the Load Balancer.

💡To check this, we can write an HTML code for the Backend Servers which would also display their IPs in the Web Page.

🙌🏻 Hence, the webpage is successfully deployed on EC2 Instances configured as Backend WebServers, and a Load Balancer configured using HAProxy, in AWS Cloud through Ansible Playbook !!

--

--