Total Pageviews

Translate

December 27, 2018

AWS DevOps - Part 4 - ELB Primer with AWS EC2 Nginx Webserver Provisioning using Ansible

by 4hathacker  |  in AWS DevOps at  1:17 AM

Hi folks!

In this post, we will discuss about Elastic Load Balancing for EC2 instances. Later, we will look into the same using Ansible.

At first, lets understand what is load balancing. Suppose, we have a cluster of web servers and thousands of requests are coming in a concurrent fashion from users/clients. Either these requests return the text, images, etc responses or they will interact with the database of the application. Whatever be the case, the high traffic can create issues like server down, latency problems, etc. One possible solution is the routing of requests across all the servers capable of fulfilling these requests such that speed and capacity utilization is optimum. And to ensure that no one server is overworked. 

So, Load Balancing refers to the efficient distribution of incoming traffic across the cluster of servers(server farm). A load balancer is a device/software which sits between the client and the server farm accepting incoming network and application and distributing the traffic.

With respect to AWS, Elastic Load Balancing (ELB) is the service to perform the load balancing for EC2 Instances. There are 3 types of Elastic Load Balancing offered by AWS.

1. Classic Load Balancer: provides basic load balancing capabilities and work well for applications built within EC2 Classic network.

2. Application Load Balancer: Operates at the request level (Layer7) routing traffic to targets like EC2 instances, containers, etc. Ideal for advanced load balancing of HTTP and HTTPS traffic.

3. Network Load Balancer: Operates at connection level (Layer4) routing connections to EC2 instances, containers, etc. based on IP data.

We will see how to configure an Application Load Balancer via AWS Management Console.
1. Configure Load Balancer: This includes Basic Configuration like name, scheme (internet facing or internal), IP address type, Listener protocols with specific ports, Availability Zone specification and Tags.
2. Configure Security Settings: If listening to HTTP, just bypass this step and move on. However, with HTTPS, you need a server certificate by IAM or AWS Certificate Manager.
3. Configure Security Groups: Select already configured security group or create a new one as per the requirement to allow specific traffic to reach your ALB first.
4. Configure Routing: Configure Target Groups with protocol, ports, target type, etc. Pay special attention while addressing Advanced health settings. 
5. Register Targets: In this step, select your instances to be registered with the ALB. 
6. Review: Before continuing further, check the information and then hit Create.

Feel free to launch your own ALB in AWS following the AWS documentation.

Lets see how to create an ELB using Ansible. The primary requirement is on the localhost machine installed with python>=2.6 and boto library with ~/.boto file with credentials. We did the same in this article. However, follow this up with Part 2 of AWS DevOps series where we have provisioned EC2 instances using Ansible.

1. The structure of playbook is given below in the image. We will be having two roles here. One for creating EC2 instances within the Security Groups and the other for Nginx Installation with ELB creation.
Note: The aws-private.pem file will only appear in the project after running the ansible playbook.


2. First, we will be defining a "create" role with all the necessary group_vars and hosts files. This will be responsible for the following tasks,
a. Creation of Security Group
b. Creating and Saving Private Key File
c. Launching EC2 instances
d. Updating hosts file dynamically with instance details


Note: There is a term.yaml file which is created to remove the EC2 instances launched. This will be helpful for rolling back to previous state.


3. After EC2 creation within Security Group, we will look forward for "web" role which takes care of the following tasks,
a. Installing nginx server in EC2 Instances
b. Uploading a default index.html page
c. Starting the webserver

4. The above two roles will do their jobs, with the help of a main site.yml file which also accounts for the creation of ELB and  registering EC2 instances with ELB.



This is how we have completed the playbook. Now, lets run the playbook and see if it works accordingly. The supporting files include,
a. index.html

b. hosts

c. all

Note: Do not forget to configure an /etc/ansible/ansible.cfg file with the private_key_file parameter referencing the path to aws-private.pem file. Also mention the "hosts", while running the ansible-playbook command.

[root@server AWSDevOps]# cat /etc/ansible/ansible.cfg
[defaults]
private_key_file=/root/PycharmProjects/AWSELB_Ansible/aws-private.pem
log_path=/var/log/ansible.log



 







We can observe the playbook actions using AWS Management Console.
1. Check if instances are launched successfully.
2. Check the Security Group configurations.


3. Check for the ELB and Instances registered with it.


4. Check the nginx webserver if working properly with both the instances IPs and the load balancer DNS as well.




You can find the playbook code at Github. This is how, we can use Ansible for EC2 provisioning of webservers registered with ELB. In the upcoming posts, we will look into more templates with CloudFormation. And gradually, move towards Docker, Packer, CodeDeploy and CodePipeline. Stay Tuned!!!


2 comments:

Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.