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.
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.
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
Now if we go to the AWS console at https://console.aws.amazon.com it will show us the instances launched using Ansible Playbook
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:-
Now we can run ansible all --list-hosts command to see the hosts
Now we need to give some more information in the ansible configuration file so that we can configure the webserver and Haproxy server.
[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
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.
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
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.
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:-
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.
--- # 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:-
Configuring the Apache webserver:-
We also need to configure the Apache Httpd server on the instances.
we will write the following code in the webserver>tasks file :-
--- # 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
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.
- hosts: "LoadBalancer" roles: - role: loadbalancer - hosts: "WebServer" roles: - role: webserver
Now we will run the Ansible PlayBook:-
Now we can check by visiting the IPAddress:8080
Thank you so much for visiting this Article.
Senior DevOps Engineer at Alepo
4 年Amazing
CKA || 1xAWS || 4xGCP || 1xAzure || 2xRedHat Certified || DevOps Engineer [???????]@Searce Inc || Freelancer || Terraform || Ansible || GitLab || Jenkins || Kubernetes || Docker || Openshift || AWS || GCP || Azure
4 年Congratulations ???