DevOps, Day - 41
Setting up an Application Load Balancer with AWS EC2 π
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.
Elastic Load Balancing
Elastic Load Balancing (ELB) is a service provided by Amazon Web Services (AWS) that automatically distributes incoming traffic across multiple EC2 instances. ELB provides three types of load balancers:
Read more here
- Application Load Balancer (ALB) - operates at layer 7 of the OSI model and is ideal for applications that require advanced routing and microservices.
- Read more here
- Network Load Balancer (NLB) - operates at layer 4 of the OSI model and is ideal for applications that require high throughput and low latency.
- Read more here
- Classic Load Balancer (CLB) - operates at layer 4 of the OSI model and is ideal for applications that require basic load balancing features.
- Read more here
Step 1: Launch EC2 Instances
Log in to your AWS Management Console.
Go to the EC2 dashboard.
Click the "Launch Instance" button.
Choose an Ubuntu AMI (Amazon Machine Image) as your instance type. You can search for "Ubuntu" in the "Quick Start" section.
Select an instance type and configure the instance as needed (e.g., instance size, VPC, security groups). Make sure the instances have public IP addresses.
On the "Additional Details" page, you can add User Data. User Data is a script that runs when the instance launches. Add the following User Data script to install Apache and modify the index.html file:
For the first instance:
#!/bin/bash apt update apt install -y apache2 echo 'Your Name' > /var/www/html/index.html service apache2 restart
Repeat Step 1 - Step 6 for the second instance, and paste the below script in the User Data section.
For the second instance:
#!/bin/bash apt update apt install -y apache2 echo 'TrainWithShubham Community is Super Aweasome :)' > /var/www/html/index.html service apache2 restart
Complete the instance creation process, and launch both instances.
Step 2: Access the EC2 Instances
Once the instances are running, note down their public IP addresses from the EC2 dashboard.
Edit inbound rules --> Enable port number 80 for both instances.
Open a web browser and paste the public IP address of the first instance into the address bar. You should see a webpage displaying "Your Name."
Similarly, paste the public IP address of the second instance into the address bar, and you should see a webpage displaying "TrainWithShubham Community is Super Awesome :)."
You've now successfully launched two EC2 instances with Ubuntu AMIs, installed the Apache Web Server, and modified the index.html files to display the desired content. You can access both instances using their public IP addresses.
Step 1: Create a Target Group
Target groups define how the ALB routes traffic to your instances.
Configure the target group as follows:
In the "Target type" section, select "Instance."
Give the target group a name.
Set the "Port" to 80 (or the port your instances are serving traffic on).
Select the same VPC as your instances.
Set the protocol to HTTP (or the protocol your instances are using).
Review the settings and click "Next"
Click on Create target group.
Our target group is created
Step 2: Create an Application Load Balancer
After creating the target group, go to the EC2 dashboard --> Click the "Load Balancers" section.
Configure the load balancer as follows:
Give your ALB a name.
Select the same VPC that your EC2 instances are in.
Select two or more availability zones.
For "Listeners," leave the default HTTP (80).
Under "Configure routing," select "Existing target group" and choose the target group you created in Step 1.
Configure security settings and other options as needed.
Review the settings, and click "Create" to create the ALB.
Step 3: Register Instances with the Target Group
After creating the target group, click on it to view its details.
In the "Targets" tab, click "Edit" to register instances.
You will see a list of available instances. Select the instances you want to add to the target group, which should be the EC2 instances you launched in Task 1. Click "Add to registered."
Step 4: Test the ALB
Once the instances are registered with the target group, go back to the "Load Balancers" section, and select your ALB.
In the "Description" tab, you can find the DNS name of your ALB (e.g., my-alb-1234567890.us-east-1.elb.amazonaws.com).
Open a web browser and access the ALB's DNS name. You should see the content served by your EC2 instances. The ALB will distribute traffic between the registered instances.
Step 5: Verify Target Health
In the "Target Groups" section, click on your target group to see the list of registered instances.
In the "Targets" tab, you can monitor the health status of the instances. Make sure they show a healthy status, indicating that the ALB is correctly routing traffic to them.
By following these steps, you have successfully created an Application Load Balancer (ALB), added your EC2 instances as target groups, and tested the ALB's load balancing capabilities.
Thank you so much for reading
Follow me on LinkedIn to see interesting posts like this : )