Day 41 of 90 Days of DevOps Challenge: Setting up an Application Load Balancer with AWS EC2
Table of contents
Load Balancing in AWS
I hope you had a productive day yesterday learning about EC2 launch templates! Today, we’re diving into Load Balancing, one of the most crucial components for scaling and managing traffic across multiple EC2 instances.
What is Load Balancing?
Load Balancing ensures that incoming traffic is distributed evenly across multiple servers to maintain performance, avoid server overloads, and ensure fault tolerance. It's essential for any large-scale, scalable application to improve both reliability and performance.
Elastic Load Balancing (ELB):
AWS Elastic Load Balancing (ELB) provides a highly scalable, fault-tolerant solution for distributing traffic across multiple EC2 instances. ELB comes in three flavors:
Application Load Balancer (ALB): Best for applications that require advanced routing, operating at Layer 7 of the OSI model. It’s perfect for microservices and containerized applications.
Network Load Balancer (NLB): Best for applications that need high throughput and low latency, operating at Layer 4.
Classic Load Balancer (CLB): A basic load balancer, operating at Layer 4, used for legacy applications.
Today's Tasks
Task 1: Launch EC2 Instances with Apache Web Server
Step 1: Launch Two EC2 Instances
Go to EC2 Console: In the AWS Management Console, navigate to EC2.
Launch EC2 Instances:
AMI: Select Ubuntu as the Amazon Machine Image (AMI).
Instance Type: Choose t2.micro (free tier eligible).
Key Pair: Choose an existing key pair or create a new one.
User Data: Add the following User Data script to install Apache Web Server during instance launch:
#!/bin/bash sudo apt update -y sudo apt install apache2 -y echo "<h1>Hello from $(hostname -f)</h1><p>Tushar</p>" | sudo tee /var/www/html/index.html sudo systemctl start apache2 sudo systemctl enable apache2
Repeat for the Second Instance: Use the same steps for the second instance, but modify the User Data to add:
#!/bin/bash sudo apt update -y sudo apt install apache2 -y echo "<h1>TrainWithShubham Community is Super Awesome :)</h1>" > /var/www/html/index.html sudo systemctl start apache2 sudo systemctl enable apache2
Step 2: Test the Web Server
After launching both instances, go to Instances in the EC2 Dashboard.
Copy the Public IP Address of each instance.
Open a Web Browser: Paste the IP addresses in the address bar and verify the following:
The first instance should display a page with your name.
The second instance should display "TrainWithShubham Community is Super Awesome :)".
Task 2: Create an Application Load Balancer (ALB)
Step-by-Step Guide:
Step 1: Create an ALB
Go to the EC2 Console: Under the Load Balancing section in the left-hand menu, click Load Balancers.
Create Load Balancer:
Choose Application Load Balancer.
Name: Provide a meaningful name (e.g.,
my-alb
).Scheme: Choose
internet-facing
to make it accessible from the web.IP Address Type: Set to
ipv4
.Listeners: Keep the default HTTP port 80 listener.
VPC: Choose the default VPC.
Subnets: Select the subnets associated with your region.
Step 2: Configure Security Group
- Security Group: Choose or create a security group that allows HTTP traffic on port 80.
Step 3: Configure Target Group
Target Group: Choose to create a new Target Group.
Target Type: Choose
instance
.Name: Give it a name (e.g.,
my-alb-targets
).Health Check Path: Set to
/
for checking the root URL.Instances: Add both EC2 instances you launched in Task 1 to the target group.
Step 4: Verify ALB
Check Health Status: Once the ALB is created, check the Target Group for the health status of the instances.
Test Load Balancing: Copy the DNS Name of the ALB from the EC2 Console and paste it into a browser. It should load one of the two web pages, alternating between the instances with each refresh.
Conclusion
In today's task, you learned how to launch EC2 instances and set up an Application Load Balancer (ALB) to evenly distribute traffic between multiple servers. This is a vital concept in building scalable and reliable cloud applications. The ALB ensures that your application can handle increased traffic while providing high availability and fault tolerance.
As you move forward in your DevOps journey, automating processes like load balancing will help ensure that your infrastructure remains highly available and efficient, no matter the scale of your applications. Keep practicing, and happy learning! 🌟