Setting up an Application Load Balancer with AWS EC2
#90 DaysofDevOps Challenge - Day 41
Table of contents
- What is Load Balancing?
- Elastic Load Balancing:
- 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 :) ".
- Task 2:
- Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console.
- Add EC2 instances that 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.
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 an AWS service designed to evenly distribute incoming traffic across multiple EC2 instances to ensure efficient and reliable application delivery. ELB offers three types of load balancers:
- Application Load Balancer (ALB): ALB operates at the application layer (Layer 7) of the OSI model and is ideal for balancing HTTP and HTTPS traffic. It intelligently routes requests to different EC2 instances based on specific rules, allowing for advanced load balancing and content-based routing.
- Network Load Balancer (NLB): NLB operates at the transport layer (Layer 4) and is well-suited for handling high volumes of traffic. It efficiently balances TCP and UDP traffic by distributing it across multiple EC2 instances, making it suitable for applications that require extreme performance and scalability.
- Classic Load Balancer (CLB): CLB is the legacy version of ELB and provides basic load balancing for both HTTP/HTTPS and TCP/UDP traffic. It distributes incoming requests across multiple EC2 instances using a round-robin algorithm, ensuring an even distribution of workload.
Task 1:
Launch 2 EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server.
To launch two EC2 instances with an Ubuntu AMI and install the Apache Web Server using User Data, follow these steps:
Log in to the AWS Management Console.
Go to the EC2 service.
Click on "Launch Instances" to start launching a new EC2 instance.
Select the desired Ubuntu AMI from the available options.
Choose the instance type, such as t2.micro.
Configure the instance details, such as the number of instances (in this case, choose 2) and networking settings.
Scroll down to the "Advanced details" section and locate the "User Data" field.
In the User Data field, enter the following script:
This script performs the following actions:
Updates the package repository on the instance.
Installs the Apache Web Server using the
apt
package manager.Starts the Apache service.
Configures the Apache service to start automatically upon instance boot.
Review the configuration and click on "Launch Instances" to launch the EC2 instances.
- Once the instances are launched, you can access the Apache Web Server by entering the public IP address of either instance in a web browser. You should see the default Apache welcome page, indicating that the installation was successful.
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 :) ".
To modify both servers index.html files based on project requirements and need to go to "/var/www/html" path and edit index.html
cd /var/www/html
ls
vim index.html
- 1st Server
- 2nd Server
Copy the public IP address of your EC2 instances.
Open a web browser and paste the public IP address into the address bar.
You should see a webpage displaying information about your PHP installation.
Task 2:
Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console.
Add EC2 instances that 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.
To create an Application Load Balancer (ALB) in EC2 using the AWS Management Console and add the EC2 instances from Task 1 to the ALB as target groups, follow these steps:
Go to the EC2 service.
In the left-hand menu, click on "Load Balancers" under the "Load Balancing" section.
Click on the "Create Load Balancer" button.
- Select "Application Load Balancer" as the load balancer type and click on "Create".
- Configure the load balancer settings:
- choose at least 2 subnets
Configure the routing:
Set up the listener protocol and port for the load balancer.
Under "Default action" choose "Create target group" which redirects you to the new tab.
Choose the target type as "Instance" and configure the target group.
After selecting Instances give "Target group name".
Choose the same VPC and register the instances from Task 1 as targets.
Click on "Next: Register Targets".
Select both servers/instances and need to click on include as pending below after that scroll down and click on create target group.
- Now go back to your "Load Balancer" tab and click on the refresh button near the "Default action" and then you will see the new target group we just created.
- Review the configuration settings and click on "Create" to create the ALB.
Once the ALB is created, it will distribute incoming traffic to the registered EC2 instances based on the configured target group. To verify that the ALB is working properly and load balancing correctly:
Select the ALB that you created.
In the "Details" tab, you can view the "DNS name" of the load balancer.
Open a web browser and enter the DNS name of the ALB. This will redirect your request to one of the instances through load balancing.
Verify that the application or web page hosted on the instances is accessible and functioning correctly.
Test the load-balancing capabilities by refreshing the page multiple times.
Note:- To delete the Load Balancer then first need to delete the load balancer-> target groups->instances/servers.
Thank you for reading. I hope you were able to understand and learn something new from my blog.
Happy Learning!
Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh.