Introduction
In this blog, we learned about load balancing and how to set up an EC2 Apache server with custom web pages and an AWS ALB to distribute traffic across your EC2 instances.
What is Load Balancing?
Load balancing is a crucial aspect of distributing network traffic across multiple servers or resources to ensure that no single server becomes overloaded. It improves the availability and reliability of applications, websites, or services by spreading the workload across a group of servers.
How does load Balancing work?
Incoming Requests: When a user makes a request to access a website or application, it is directed to a load balancer.
Load Balancer's Decision: The load balancer evaluates the current state of the servers in the pool, considering factors like server response time, current load, and other defined criteria.
Selecting a Server: Based on the evaluation, the load balancer forwards the request to one of the servers. The selected server will then process the request and send the response back to the user.
Key Benefits of Load Balancing:
High Availability: If one server fails, the load balancer redirects traffic to other servers, ensuring uninterrupted service.
Improved Performance: Distributing traffic evenly prevents any single server from being overloaded, resulting in faster response times.
Scalability: New servers can be easily added to the pool to handle increased traffic, allowing for seamless scalability.
Redundancy: Load balancers can be configured for failover, ensuring that traffic is directed to healthy servers if others become unavailable.
Efficient Resource Utilization: Load balancing optimizes resource usage, making the most of available server capacity.
Elastic Load Balancing:
Elastic Load Balancing (ELB) is an AWS service that automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, and IP addresses, in one or more Availability Zones. This helps improve the availability and fault tolerance of your applications.
Types of Elastic Load Balancers:
There are four types of Elastic Load Balancers:
Application Load Balancer (ALB): Works at the application layer (Layer 7) and is ideal for routing HTTP/HTTPS traffic. It can route traffic based on content, such as URLs or cookies.
Read more here
Network Load Balancer (NLB): Operates at the transport layer (Layer 4) and is designed to handle high volumes of traffic. It's well-suited for TCP, UDP, and TLS traffic.
Read more here
Classic Load Balancer: Provides basic load balancing across multiple Amazon EC2 instances. It works at both the application and transport layers.
Read more here
Gateway Load Balancer (GWLB): This is a network virtual appliance that you can deploy in your VPC. It operates at the network layer (Layer 3) and is designed to handle a wide range of network traffic, making it suitable for various use cases.
Tasks:)
⭐Task1: EC2 Apache Server Setup with Custom Web Pages
Log in to AWS Console:
- Go to the AWS Management Console and sign in to your AWS account.
Navigate to EC2 Dashboard:
- From the AWS Management Console, search and click on "EC2" to open the EC2 dashboard.
Launch Instances:
Click on "Instances" in the left-hand menu.
Click "Launch Instance" to start the process.
Choose Ubuntu AMI:
In the list of available Amazon Machine Images (AMIs), select an Ubuntu AMI (e.g., "Ubuntu Server 20.04 LTS").
Choose Instance Type:
Select an instance type (e.g., t2.micro) and click "Next: Configure Instance Details".
Add User Data:
In the "Configure Instance Details" page, scroll down to the "Advanced Details" section.
Add the following User Data script for the instance:
#!/bin/bash apt update -y apt install apache2 -y systemctl start apache2 systemctl enable apache2
Complete Instance Launch:
Continue with the instance launch process, configuring security groups, key pairs, and reviewing the settings.
Click "Launch" and select an existing key pair or create a new one.
Connecting to Your EC2 Instance:
To connect to your EC2 instance, you'll use SSH for Linux instances or RDP for Windows instances. Here's how to check the status of Apache 2:
sudo systemctl status apache2
Modifying the
index.html
File:- Navigate to the
/var/www/html/
directory using the following command:
- Navigate to the
cd /var/www/html/
- Open the
index.html
file for editing:
sudo vim index.html
- Add the following HTML code below the
<title>
tag to display your name:
<body>
<p> Content </p>
</body>
Save the file and exit the text editor.
Accessing Your Apache Server
Add the inbound rule for the instance
Copy the public IP address of your EC2 instance.
Open a web browser and paste the public IP address into the address bar.
⭐Task 2: Setting Up AWS Application Load Balancer (ALB) and Target Groups for EC2 Instances
Absolutely, here is a detailed breakdown:
Creating Target Groups:
Navigate to Target Groups:
Go to the EC2 dashboard.
Under "Load Balancing," click on "Target Groups."
Create a Target Group:
Click the "Create target group" button.
Configure Target Group:
Fill in the necessary settings:
Protocol: HTTP
Port: 80
Health check settings
Register Target Instances:
Select the instances you want to add.
Click "Include as pending below."
Hit "Create target group."
Verify Target Group Creation:
Go back to your "Load Balancer" tab and refresh. You'll see the newly created target group.
Creating an Application Load Balancer (ALB):
Log in to AWS Management Console:
- Go to the AWS Management Console and sign in to your AWS account.
Navigate to EC2 Service:
- From the dashboard, click on "EC2."
Access Load Balancers:
In the left sidebar, under "Load Balancing," click on "Load Balancers."
Create a Load Balancer:
Click the "Create Load Balancer" button.
Choose Load Balancer Type:
Select "Application Load Balancer" and click "Create."
Configure ALB Settings:
Fill in the necessary details:
VPC
Availability Zones
Listener settings (Select HTTP on port 80)
Set Up Security Groups and Routing:
Configure security groups and routing as required.
Attach Target Group:
In the listener section, attach the previously created target group.
Verification:
Check Target Group Health:
Go back to the EC2 dashboard.
Under "Load Balancing," click on "Target Groups."
Check the health status of the newly created target group.
Test Load Balancing:
Access your ALB using its DNS name or public IP address from a web browser.
Refresh the page multiple times to observe load balancing in action.
Conclusion:
We successfully set up an EC2 Apache server with custom web pages and an AWS ALB to distribute traffic across the EC2 instances. This improves the availability, reliability, and scalability of our web application.
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps 🚀, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on LinkedIn for more updates and discussions on DevOps
Happy Learning! Keep pushing those boundaries! 😊