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

What are Load Balancer?🤔

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks.

What is HAproxy?🤔

HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database).

What is Ansible?🤔

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.

  • 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 will be launching Ec2 Instances in the AWS cloud. For this, we will be writing a playbook but before that, we have to create a Security Group in AWS Cloud.

- 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}}"
ansible-vault create --vault-id Abhi@prompt AWS_Instance.yml
ansible-playbook --vault-id Abhi@prompt <playbook name>
  • 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.
  • ec2.py
  • ec2.ini
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
ansible all --list-hosts
ansible all -m ping
ansible-galaxy init webserver
ansible-galaxy init loadbalancer
- 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
  • 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.
- 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
- name: haproxy restart
service:
name: "haproxy"
state: restarted
- hosts: Webserver
become: yes
roles:
- role: webserver
- hosts: Loadbalancer
become: yes
roles:
- role: Loadbalancer
ansible-playbook <file_name.yml>

--

--

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