Elastic Load Balancing (ELB) is a service that automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, and IP addresses. ELB helps ensure that your application is highly available and can handle varying levels of traffic.
Key Concepts
-
Load Balancer Types:
- Application Load Balancer (ALB): Best suited for HTTP and HTTPS traffic. Operates at the application layer (Layer 7) and provides advanced routing features.
- Network Load Balancer (NLB): Best suited for TCP, UDP, and TLS traffic. Operates at the transport layer (Layer 4) and is capable of handling millions of requests per second.
- Classic Load Balancer (CLB): Operates at both the application and transport layers. Suitable for applications built within the EC2-Classic network.
-
Target Groups:
- A target group routes requests to one or more registered targets (e.g., EC2 instances) using the specified protocol and port number.
- Health checks are performed on targets to ensure they are available to handle requests.
-
Listeners:
- A listener is a process that checks for connection requests. It is configured with a protocol and port number for front-end (client to load balancer) and back-end (load balancer to target) connections.
-
Health Checks:
- ELB performs health checks on registered targets to ensure they are healthy and can handle traffic. Unhealthy targets are automatically removed from the pool until they recover.
Setting Up an Application Load Balancer
Step-by-Step Guide
-
Create a Load Balancer:
- Navigate to the EC2 Dashboard.
- Select "Load Balancers" from the left-hand menu.
- Click "Create Load Balancer" and choose "Application Load Balancer".
-
Configure Load Balancer:
- Name: Provide a name for your load balancer.
- Scheme: Choose "Internet-facing" or "Internal".
- Listeners: Add listeners (e.g., HTTP, HTTPS).
- Availability Zones: Select the VPC and Availability Zones where the load balancer will be deployed.
-
Configure Security Settings (for HTTPS):
- SSL Certificate: Choose an existing certificate or upload a new one.
- Security Policy: Select a predefined security policy.
-
Configure Security Groups:
- Assign one or more security groups to the load balancer.
-
Configure Routing:
- Target Group: Create a new target group or select an existing one.
- Health Checks: Configure health check settings (e.g., protocol, path, interval).
-
Register Targets:
- Add the instances or IP addresses to the target group.
-
Review and Create:
- Review the settings and click "Create".
Example: Creating an Application Load Balancer using AWS CLI
aws elbv2 create-load-balancer \ --name my-load-balancer \ --subnets subnet-12345678 subnet-87654321 \ --security-groups sg-12345678 \ --scheme internet-facing \ --type application
Example: Registering Targets using AWS CLI
aws elbv2 register-targets \ --target-group-arn arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456 \ --targets Id=i-1234567890abcdef0 Id=i-0abcdef1234567890
Practical Exercise
Exercise: Create and Configure an Application Load Balancer
- Objective: Create an Application Load Balancer that distributes traffic to two EC2 instances.
- Steps:
- Launch two EC2 instances in the same VPC.
- Create a new Application Load Balancer.
- Configure the load balancer with HTTP listeners.
- Create a target group and register the EC2 instances.
- Configure health checks.
- Test the load balancer by accessing its DNS name.
Solution
-
Launch EC2 Instances:
- Launch two EC2 instances in the same VPC and Availability Zone.
-
Create Load Balancer:
- Follow the steps outlined in the "Setting Up an Application Load Balancer" section.
-
Configure Listeners and Target Group:
- Add HTTP listeners on port 80.
- Create a target group with the two EC2 instances.
-
Configure Health Checks:
- Set the health check path to
/
and protocol to HTTP.
- Set the health check path to
-
Test Load Balancer:
- Access the load balancer's DNS name in a web browser to verify traffic distribution.
Common Mistakes and Tips
- Security Groups: Ensure that the security groups for the load balancer and the targets allow the necessary traffic (e.g., HTTP/HTTPS).
- Health Checks: Properly configure health checks to avoid removing healthy instances from the target group.
- DNS Name: Use the load balancer's DNS name to access the application, not the IP addresses of the individual instances.
Conclusion
Elastic Load Balancing is a powerful service that enhances the availability and scalability of your applications. By distributing traffic across multiple targets and performing health checks, ELB ensures that your application can handle varying levels of traffic and remain highly available. In the next module, we will explore Amazon CloudFront, a content delivery network service that works seamlessly with ELB to deliver content to users with low latency.