What is a Load Balancer?
A load balancer is a system that automatically distributes incoming application traffic across multiple targets, such as servers or instances, to ensure no single server becomes overwhelmed. This distribution enhances the availability and reliability of your application by spreading the load, thereby preventing any single point of failure.
Key Benefits:-
High Availability: Ensures your application remains accessible by distributing traffic across multiple resources.
Scalability: Automatically adjusts to handle traffic spikes, maintaining optimal performance.
Fault Tolerance: Detects unhealthy targets and reroutes traffic to healthy ones, maintaining seamless user experiences.
What is Elastic Load Balancing (ELB) in AWS?
Elastic Load Balancing (ELB) is a fully managed service provided by AWS that automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, and IP addresses, within one or more Availability Zones. ELB scales your load balancer as traffic to your application changes over time and can handle the varying load of your application traffic in a single Availability Zone or across multiple Availability Zones.
Key Features:
Automatic Scaling: Adjusts capacity to handle incoming traffic demands seamlessly.
Health Monitoring: Continuously monitors the health of registered targets and directs traffic only to healthy ones.
Robust Security: Supports integration with AWS Certificate Manager (ACM) for SSL/TLS certificates, enabling secure connections.
Why is ELB Used in Industries?
Industries leverage ELB to enhance application performance, reliability, and security.
Use Cases:
E-commerce Platforms: Manage high traffic volumes during peak shopping periods by distributing requests across multiple servers.
Financial Services: Ensure low-latency and secure transaction processing by balancing loads across secure instances.
Media and Entertainment: Streamline content delivery by routing user requests to the nearest or best-performing servers.
Healthcare Applications: Maintain availability and compliance by distributing workloads across compliant and secure instances.
Types of AWS Elastic Load Balancers (ELB)
AWS offers four types of load balancers, each tailored to different use cases:
Application Load Balancer (ALB)
Layer: Operates at the application layer (Layer 7) of the OSI model.
Protocols: Supports HTTP, HTTPS, and WebSocket protocols.
Routing Features: Offers advanced request routing based on URL path, host headers, HTTP headers, and query string parameters.
Use Cases: Ideal for microservices and container-based applications, allowing load balancing to multiple services or containers across one or more Availability Zones.
Network Load Balancer (NLB)
Layer: Operates at the transport layer (Layer 4) of the OSI model.
Protocols: Supports TCP, UDP, and TLS (Transport Layer Security) protocols.
Performance: Designed for ultra-high performance and can handle millions of requests per second with low latency.
Use Cases: Suitable for applications requiring extreme performance, such as real-time gaming, high-performance computing, and financial systems.
Gateway Load Balancer (GWLB)
Layer: Operates at Layer 3 (Network Layer) of the OSI model.
Functionality: Simplifies the deployment, scaling, and management of third-party virtual network appliances, such as firewalls, intrusion detection and prevention systems, and deep packet inspection systems.
Use Cases: Ideal for integrating and scaling virtual appliances for security, network analytics, and other network services.
Classic Load Balancer (CLB)
Layer: Supports both Layer 4 (TCP) and Layer 7 (HTTP/HTTPS) load balancing.
Functionality: Provides basic load balancing across multiple Amazon EC2 instances and operates at both the request and connection levels.
Use Cases: Recommended for applications built within the EC2-Classic network. For new applications, AWS recommends using ALB or NLB.
ELB Pricing and Cost Comparison
AWS Elastic Load Balancing pricing varies based on the type of load balancer and the region. Charges generally comprise two components:
Load Balancer Hours: The time your load balancer is running.
Data Processed: The amount of data in gigabytes (GB) processed by the load balancer.
Cost Components:
Application Load Balancer (ALB): Charged based on the number of Load Balancer Capacity Units (LCUs) and the hours the load balancer is running.
Network Load Balancer (NLB): Charged based on the number of Network Load Balancer Capacity Units (NLCUs) and the hours the load balancer is running.
Gateway Load Balancer (GWLB): Charged based on the number of Gateway Load Balancer Capacity Units (GLCUs) and the hours the load balancer is running.
Classic Load Balancer (CLB): Charged based on the hours the load balancer is running and the amount of data processed.
Pricing Example: In the Asia Pacific (Mumbai) region, the pricing details are as follows:
ALB: ₹0.028 per LCU-hour.
NLB: ₹0.0225 per NLCU-hour.
Deploying an ALB Across same Regions
Create 2 VPCs and Subnet in same region
VPC1 (region1-vpc1) in Region A (us-east-1):
CIDR Block: 10.0.0.0/16
Subnets:
region1-subnet1 (CIDR: 10.0.0.0/24) in us-east-1a
region1-subnet2 (CIDR: 10.0.2.0/24) in us-east-1b
Internet Gateway:
- create internet gateway named: region1-igw
VPC2 in Region B:
CIDR Block: 192.168.0.0/16
Subnet:
- Subnet1-B (CIDR: 192.168.1.0/24) in us-east-1a
Internet Gateway:
create internet gateway named: region2-igw
Establish VPC Peering Between VPC1 and VPC2:
Initiate a VPC peering connection between VPC1 and VPC2.
Update route tables in each VPC to allow traffic to flow between them over the peering connection. you can see in each route table that there is peering connection is add. each vpc have eachother ip address.
For a detailed walkthrough on VPC peering, refer to my previous blog post here
Launching EC2 Instances:
In VPC1, launch two EC2 instances:
One in Subnet1-A. name of instances is instance1-region1, it get a private IP 10.0.0.183
One in Subnet2-A. name of instances is instance2-region it get a private IP 10.0.2.99
In VPC2, launch one EC2 instances:
Only in a subnet1-A, name of the instances is region2-instances1 the private IP is 192.168.1.19
after this i connected with this instance or you can say that access the instances terminal. I ensure that i downloaded the webserver in all three EC2 instance in different VPC. I install Apache server on it i created index.html file. you can see below.
this is first instances where i created the index.html file in that i written Welcome to server in AZ1 instance ip is 10.0.0.183.
then i created other webpage index.html file in second instance of same vpc. there i written Welcome to server in AZ-2.
After that i created 3rd index.html file in instances of other vpc there i Written Welcome to server in VPC2 in AZ1.
Setting up the Load Balancer:
- Before setting up the load balancer we need the a new security group for that load balance, in that new security group i allowed a some ports which is necessary for load balancing All ICMP IPv4, HTTP.
Now after doing this I setup the load balancer, As we know we can create a load balancer in one region only or you can say only in one VPC. so i create in region1-vpc1. After that i add both AZ in that load balancer, one thing to remember that a load balancer required minimum 2 AZ in a VPC to route the traffic otherwise it will give error. then i simply add the security group named newsg. After doing this the main step to add Target group in load balancer.
Here, i define the load balancer which inbound traffic you have to route to the EC2 instance in all vpc, so my load balancer only hear HTTP mean port 80. Other request which is not HTTP request doesnt come through Load balance. Suppose any request is made by someone who have private ip of my instances like peering request or any other request , so that request doesnt come through Load balancer.
Here you can see i add all my subnet which i connect to my load balancer. note that load balancer only route the load to that subnet which is connect to him, by instances or by IP, Suppose you create a subnet in that VPC but you didnt connect that subnet to load balancer then load balancer does not know that the subnet is exist or not.
Now, i have to create a new Target group where i declare which instances ip should be connected to load balancer. but first let know what is target group and what is target?
What is a Target Group in AWS?
A Target Group is an AWS Elastic Load Balancer (ELB) configuration that defines a set of targets (such as EC2 instances, IP addresses, or Lambda functions) to receive traffic from the Load Balancer.
Three Ways to Define a Target Group
Instance-Based Target Group
Registers EC2 instances as targets.
Load Balancer forwards traffic to registered instances.
Best for traditional EC2-based applications.
IP-Based Target Group
Registers private IP addresses as targets.
Allows cross-VPC and on-premises servers to be load-balanced.
Useful when instances are in different VPCs (like in your project).
Lambda-Based Target Group
Registers AWS Lambda functions as targets.
Load Balancer invokes Lambda functions instead of routing to servers.
Best for serverless applications.
What is a Target in AWS Load Balancing?
A target is an individual resource (EC2, IP, or Lambda) that receives traffic from the load balancer.
In your project:
Targets: The three EC2 instances in different VPC subnets.
Target Group: Configured to use IP-based targeting, as one instance is in a different VPC.
Create a Target group and add the 3 Target in that:
I create a Target group named TargetG, where i bascially allowed all HTTP traffic this target group is mounted with load balancer. HTTP1 protocol version i used.
When i created the Target group there i have to add the all the instances in the all subnet this we can with help of instances private ip. see, here in this project i used to 2 VPC so, i need to route my traffic in both VPC, we can do that with help of IP based Target group. so here simply i haved to add my private ip all instances, as you can see I add private ip, 10.0.2.99 , 10.0.0.183 , 192.168.1.19. You can see the region beside that , all my instance is ready and healthy. if i get all green and healthy status of my instances it means my load balancer is connected successfully with them.
Here its basics health check parameter for load balancer to assure that all instances which is connected to him are working properly or not. this can happened with the help of this parameter
All step up is done now, lets use this load balancer DNS to check that our request is route to all EC2 instances in same and different VPC or not?
Now, i put load balancer DNS into my browser, and as you can see the my request is route to 1st instances in the 1st image.
Now when i reload the webpage my request is route to 2nd instances.
Later when i again reload the webpage then my request is route toward my 2 vpc instances with help of private id of that instances.
As you can see with help of ELB we can route the traffic towards multiple vpc in same or in different regions.
Why Use ELB Instead of Nginx?
Managed Service: ELB is a fully managed AWS service, while Nginx requires manual setup, scaling, and maintenance.
Auto Scaling: ELB integrates with AWS Auto Scaling, dynamically adding or removing targets based on demand.
Multi-AZ Support: ELB distributes traffic across multiple Availability Zones, ensuring high availability.
Security: ELB integrates with AWS WAF, IAM, and ACM for enhanced security.
Cross-VPC and Private IP Support: In your scenario, ELB allows cross-VPC load balancing using private IPs, which is complex to configure with Nginx.
Nginx is a great choice for reverse proxying, caching, and static content delivery but requires manual configuration for high availability and scaling
Conclusion:
By following these steps, I had successfully deployed an Application Load Balancer that distributes traffic across EC2 instances located in two different VPCs same and different regions, demonstrating a robust and scalable multi-region architecture. This setup ensures high availability, security, and optimized performance while leveraging AWS-managed services to simplify infrastructure management.