Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE!

Hello connections!!Here is my another task of Ansible!

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 Ansible:- Ansible is an automation tool which is mainly written in Python language and it is used for Linux automation. We can also use it for configuring the web server and the Haproxy server on the AWS cloud.

No alt text provided for this image

Firstly we need to create a User using the IAM services of AWS then we will get the access key and secret key now we have to provide these things to our local machine.

Here we are going to configure everything in Mumbai Region(ap-south-1)

export AWS_ACCESS_KEY_ID='xxxxxxxxxxxxxxx'
export AWS_SECRET_ACCESS_KEY='xxxxxxxxxxxxxxxxxxxxxxx'
export AWS_REGION = 'ap-south-1'

Creating EC2 Instance:-

EC2 stands for Elastic cloud computing which is a service of AWS through which we can create the instance(like Virtual Machine) running in the AWS data centers.

Now we will write the playbook for instance creation.

No alt text provided for this image
No alt text provided for this image

Here we are going to launch three instances.In two instances we will configure httpd server and run our site and One instance will be used as a Haproxy server.

- hosts: "localhost"
  vars:
       myos:
         - "LB1"
         - "web1"
         - "web2"
  tasks:


  - name: "provisioning OS on AWS using Ansible"


    ec2:


       key_name: "myawskey"


       instance_type: "t2.micro"


       image: "ami-052c08d70def0ac62"


       wait: yes


       count: 1


       instance_tags:


          Name: "{{ item }}"


       vpc_subnet_id: "subnet-f90e3391"


       assign_public_ip: yes


       region: "ap-south-1"


       state: present


       group_id: "sg-012a5e5d7582997de"

        
       aws_access_key: "xxxxxxxxx"

       aws_secret_key: "xxxxxxxxxxxxxx"


    loop: "{{ myos }}"

to run this playbook we will use this command:-

ansible-playbook instances.yml
No alt text provided for this image

Now if we go to the AWS console at https://console.aws.amazon.com it will show us the instances launched using Ansible Playbook

No alt text provided for this image

Fetching Dynamic Inventory:-

Inventory is the collection of IP addresses means its the IP database.To fetch the inventory we need to install two packages using pip3.

pip3 install boto
pip3 install boto3

In this Dynamic World, we can't go manually and fetch the IP Address we use automation to save our time and to develop quickly. Here we have to use a dynamic Inventory Concept to fetch the IP Address.

https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.

Now we will download this file and make this executable and set the environmental variable as mentioned above then if we will run this file so we can see that we can Dynamically get the IP address of the Ec2 instance we also need one more file that is ec2.ini file

https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.ini

now we will make both these files executables:-

No alt text provided for this image

Now we can run ansible all --list-hosts command to see the hosts

No alt text provided for this image

Now we need to give some more information in the ansible configuration file so that we can configure the webserver and Haproxy server.

No alt text provided for this image
[defaults]
inventory= /mydb
host_key_checking=False
command_warnings=false
private_key_file= /home/yogesh/myawskey.pem
ask_pass = false
remote_user= ec2-user


[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false 


Now we can check if All Instances are Pingeable or not

No alt text provided for this image

What is ROLE:- Ansible role is a good way to manage the playbooks. For example, we need to configure the webserver in 5 instances and we also need to configure the database server in the same instances so we can just change the code and do the same task using the role.

No alt text provided for this image

We can create a role manually and give the path in the ansible configuration file or we can use the commands:-

ansible-galaxy init webserver

ansible-galaxy init loadbalancer
No alt text provided for this image

What is HAPROXY Server:- HAProxy is a load balancing server which is used to balance the load between the cloud instances.We need to install the HAProxy software to configure the system as a loadbalancer.

No alt text provided for this image

In Loadbalancing the client connects to load balancer IP and it will balance the load between the n numbers of web-server.

Let's create the code for the HAProxy server:-

No alt text provided for this image

This is the tasks file:-

---
# tasks file for loadbalancer
- name: "install HAPROXY"
  package:
     name: haproxy
     state: present
- name: "copy HAPROXY configuration files to LoadBalancer"
  template:
     src: "/root/Ansible/LB/haproxy.cfg"
     dest: /etc/haproxy/
  notify: lbrestart
- name: "start the Haproxy"
  service:
     name: haproxy
     state: started

We also need to write one handler because when we change any code on the webserver we change anything in instances so we have to restart the HAProxy server.

No alt text provided for this image
---
# handlers file for loadbalancer
- name: "lbrestart"
  service:
     name: haproxy
     state: restarted

We need to modify the configuration file of the HAProxy server. We are using the Dynamic IP concepts so we need to update the IP addresses dynamically in the configuration file:-

No alt text provided for this image

Configuring the Apache webserver:-

We also need to configure the Apache Httpd server on the instances.

No alt text provided for this image

we will write the following code in the webserver>tasks file :-

No alt text provided for this image
---
# tasks file for webserver
- name: "install httpd"
  package:
    name: "httpd"
    state: present
- name: "copy the content"
  copy:
    src: "/etc/ansible/roles/index.html"
    dest: /var/www/html/
  notify: restart
- name: "restart httpd"
  service:
    name: "httpd"
    state: started

No alt text provided for this image

This is the code we have to write to handle the uploaded code if we change anything in the web files so we want it should restart the httpd server.

---
# handlers file for webserver
- name: "restart"
  service:
      name: httpd
      state: restarted

Now we need to create one more Ansible Playbook where we have to mention on which groups we want to run which role.

No alt text provided for this image
- hosts: "LoadBalancer"
  roles:
  - role: loadbalancer






- hosts: "WebServer"
  roles:
  - role: webserver
                       

Now we will run the Ansible PlayBook:-

No alt text provided for this image
No alt text provided for this image

Now we can check by visiting the IPAddress:8080

No alt text provided for this image

Thank you so much for visiting this Article.


























Ashwani S.

Senior DevOps Engineer at Alepo

4 年

Amazing

Amit Sharma

CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure

4 年

Congratulations ???

要查看或添加评论,请登录

Yogesh kumar Prajapati的更多文章

  • Microsoft automates to achieve more with Red Hat Ansible Automation Platform

    Microsoft automates to achieve more with Red Hat Ansible Automation Platform

    Hello, Connections! In this article, we are going to talk about what is Redhat Ansible and How Microsoft is using…

  • NETFLIX ON AWS

    NETFLIX ON AWS

    Netflix is the world’s leading internet television network, with more than 100 million members in more than 190…

  • Big DATA

    Big DATA

    Big Data refers to the amount of huge data that is Coming day by day and large data can not be stored and processed by…

  • Integrating RDS with k8s

    Integrating RDS with k8s

    Hello Connections Welcome to this article! Task Description:- Deploy the WordPress application on Kubernetes and AWS…

  • GCP automation using Terraform

    GCP automation using Terraform

    Hello connections! Welcome this article,we are going to create a Infrastrcture as code. Task Description:- 1.

    5 条评论
  • Automating Cloud infrastructure using terraform (task-4)

    Automating Cloud infrastructure using terraform (task-4)

    Hello, connections! In this article, we will create a highly secure environment in which our site will running in the…

  • Deploy Web Server on AWS through ANSIBLE!

    Deploy Web Server on AWS through ANSIBLE!

    ??Provision EC2 instance through ansible. ??Retrieve the IP Address of instance using dynamic inventory concept.

    4 条评论
  • Automation using Ansible on Docker

    Automation using Ansible on Docker

    Task description:- Write an Ansible PlayBook that does the following operations in the managed nodes: ?? Configure…

  • Creating website using Terraform with all security appliances

    Creating website using Terraform with all security appliances

    Task Description:- Statement: We have to create a web portal for our company with all the security as much as possible.…

  • Automating web deployment on AWS using Terraform(Task2)

    Automating web deployment on AWS using Terraform(Task2)

    Hello Connections ! Welcome to this Article.In this article i am gonna show you how to deploy an web application on top…

社区洞察

其他会员也浏览了