Day 41: Setting up an Application Load Balancer with AWS EC2
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.
In this article, we will embark on a journey to explore the intricacies of setting up an Application Load Balancer (ALB) within the AWS Elastic Compute Cloud (EC2) environment. As we delve into Day 41 of our AWS learning series, we will gain valuable insights into the configuration, advantages, and best practices associated with deploying an ALB alongside EC2 instances.
What is Load Balancing?
Load balancing is the distribution of workloads across multiple servers to ensure consistent and optimal resource utilization. It is an essential aspect of any large-scale and scalable computing system, as it helps you to improve the reliability and performance of your applications.
What Elastic Load Balancing?
Elastic Load Balancing (ELB) is a service provided by Amazon Web Services (AWS) that helps distribute incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, and IP addresses. The primary goal of ELB is to ensure high availability and fault tolerance for your applications by evenly distributing the incoming requests across healthy instances. ELB provides three types of load balancers:
load balancers are:
- Classic Load Balancer (CLB): The Classic Load Balancer was the first load balancer provided by AWS. It supports both HTTP and HTTPS applications and operates at the Layer 4 (Transport Layer) and Layer 7 (Application Layer) of the OSI model. It can distribute traffic across multiple EC2 instances within a single Availability Zone or across multiple zones. However, AWS recommends using Application Load Balancer (ALB) or Network Load Balancer (NLB) for new applications, as Classic Load Balancer has limitations in comparison to the newer load balancer types.
- Application Load Balancer (ALB): The Application Load Balancer is best suited for load balancing HTTP and HTTPS traffic. It operates at the Layer 7 (Application Layer) of the OSI model and provides advanced features, including content-based routing, host-based routing, and path-based routing. This makes ALB ideal for microservices and container-based architectures. ALB also supports WebSocket connections and allows you to use HTTP/2 for improved performance. It can route requests to different target groups based on the content of the request, making it highly flexible for modern application architectures.
- Network Load Balancer (NLB): The Network Load Balancer is designed to handle high-throughput, low-latency traffic at the Layer 4 (Transport Layer) of the OSI model. It is suitable for protocols that require extreme performance, such as TCP, UDP, and TLS traffic. NLB is capable of handling millions of requests per second and is often used for applications that require high availability and scalability, such as gaming, streaming, and real-time communication.
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 include “TrainWithShubham Community is Super Awesome”.
Open AWS console and go to 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 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 - Radheya Zunjur </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.
Let us do the same thing for the other server with some more additions.
<!DOCTYPE html>
<html>
<head>
<title>My New Page for Apache Web Server</title>
</head>
<body>
<h1>Author - Radheya Zunjur</h1>
<h2> TrainWithShubham Community is Super Awesome :) <h2>
</body>
</html>
Let’s connect to IP address of our second ec2 instance.
We have successfully installed the Apache Web server and edited the index.html file.
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 is 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!