Hello Everyone!
I hope you had a great day yesterday learning about the launch template and instances in EC2. Today, we are going to dive into one of the most important concepts in EC2: Load Balancing.
What is Load Balancing?
A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.
Classic Load Balancer
A load balancer distributes incoming application traffic across multiple EC2 instances in multiple Availability Zones. This increases the fault tolerance of your applications. Elastic Load Balancing detects unhealthy instances and routes traffic only to healthy instances.
Application Load Balancer
An Application Load Balancer functions at the application layer, the seventh layer of the Open Systems Interconnection (OSI) model. After the load balancer receives a request, it evaluates the listener rules in priority order to determine which rule to apply and then selects a target from the target group for the rule action.
Network Load Balancer
A Network Load Balancer functions at the fourth layer of the Open Systems Interconnection (OSI) model. It can handle millions of requests per second. After the load balancer receives a connection request, it selects a target from the target group for the default rule. It attempts to open a TCP connection to the selected target on the port specified in the listener configuration.
Gateway Load Balancer
Gateway Load Balancers enable you to deploy, scale, and manage virtual appliances, such as firewalls, intrusion detection and prevention systems, and deep packet inspection systems. It combines a transparent network gateway (that is, a single entry and exit point for all traffic) and distributes traffic while scaling your virtual appliances with the demand.
Tasks -
Task 1) Launch 2 EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server. Modify the index.html file to include your name so that when your Apache server is hosted, it will display your name also do it for 2nd instance which includes “TrainWithShubham Community is Super Awesome”.
Open the AWS console and go to the ec2 service.
Launch a new ec2 instance.
Scroll down to Advance section and under the User data box and type these commands.
#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
Number of instances:2
Click on Launch Instance. Once the instances are created rename them with numbers so you can easily identify the servers.
To modify the index.html file, we need to modify its content in the directory /var/www/html.
Let’s connect to our instances and run the command
cd /var/www/html
The directory /var/www/html exists. Let’s modify the index.html file.
Give the index.html file root user privilege.
sudo chmod +x index.html
I have edited the file with below content
<!DOCTYPE html>
<html>
<head>
<title>Page of ec2 instance 1</title>
</head>
<body>
<h1>Author - Pranjal Jain </h1>
</body>
</html>
Copy the Public IP of the instance and paste it into the browser. You should be able to see the author’s name.
Likewise, modify the index.html file in other ec2 instance as well.
Task 2) Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console. Add EC2 instances which you launch in task-1 to the ALB as target groups. Verify that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities.
Open EC2 service console in aws. In left pannel Select Load Balancers.
Click on Create Load Balancer.
Select Application Load Balancer and click on Create.
Basic configuration:
Load balancer name: my-load-balancer
Scheme: Internet Facing (access to public)
Network mapping: Select at least two Availability Zones and one subnet per zone.
Security Group: default will be selected and select the other security groups you require.
Before going ahead, let’s create Target Groups.
Click on Register targets
Available Instances: Select the instances which you need
Once the ALB is in an active state, copy the DNS of the load balancer and access it from your website.
Let’s open this in the browser.
As per the load, we can observe that sometimes Server 1 comes up and other times Server 2 comes up.
Here we have created an ALB, and created the load!