Elastic Load Balancing (ELB)
Elastic Load Balancing (ELB) is a service provided by AWS that automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, and IP addresses, in one or more Availability Zones. ELB helps to ensure that your application is highly available, fault-tolerant, and scalable by balancing the load and automatically handling the failure of any backend instance.
Table of Contents
How Elastic Load Balancing Functions:
1. Traffic Distribution
Elastic Load Balancing distributes incoming client requests across multiple targets based on the load-balancing algorithm and the type of load balancer used. This helps in spreading the load evenly, ensuring that no single instance is overwhelmed.
2. Health Checks
ELB continuously monitors the health of registered targets (e.g., EC2 instances) using health checks. If a target becomes unhealthy, ELB stops sending traffic to it until it recovers.
3. Automatic Scaling
ELB works in conjunction with Auto Scaling to automatically add or remove instances based on demand. As new instances are added, ELB automatically starts sending traffic to them, and when instances are removed, it stops sending traffic to them.
4. Security
ELB supports SSL/TLS termination, which means it can handle the encryption and decryption of traffic. It also integrates with AWS security groups and network access control lists (ACLs) to control the traffic flow to and from your application.
5. Cross-Zone Load Balancing
ELB can distribute incoming traffic across instances in multiple Availability Zones, improving fault tolerance and ensuring that traffic is evenly distributed across all available instances.
AWS offers three types of load balancers under ELB:
- Application Load Balancer (ALB): Best suited for HTTP/HTTPS traffic and operates at the application layer (Layer 7).
- Network Load Balancer (NLB): Best suited for TCP/UDP traffic and operates at the transport layer (Layer 4).
- Classic Load Balancer (CLB): Legacy load balancer that supports both Layer 4 and Layer 7 traffic.
Java Example:
Here’s a simple Java example that demonstrates how to create an Elastic Load Balancer using the AWS SDK:
```java
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticloadbalancing.ElasticLoadBalancingClient;
import software.amazon.awssdk.services.elasticloadbalancing.model.*;
public class CreateClassicELB {
public static void main(String[] args) {
Region region = Region.US_EAST_1;
ElasticLoadBalancingClient elbClient = ElasticLoadBalancingClient.builder()
.region(region)
.build();
try {
// Create a Classic Load Balancer
CreateLoadBalancerRequest createLoadBalancerRequest = CreateLoadBalancerRequest.builder()
.loadBalancerName("my-classic-elb")
.availabilityZones("us-east-1a", "us-east-1b")
.listeners(Listener.builder()
.protocol("HTTP")
.loadBalancerPort(80)
.instanceProtocol("HTTP")
.instancePort(80)
.build())
.build();
CreateLoadBalancerResponse response = elbClient.createLoadBalancer(createLoadBalancerRequest);
System.out.println("Created Classic Load Balancer: " + response.dnsName());
} catch (ElasticLoadBalancingException e) {
System.err.println(e.awsErrorDetails().errorMessage());
} finally {
elbClient.close();
}
}
}
```
In this Java example, the AWS SDK is used to create a Classic Load Balancer (CLB) in the US East (N. Virginia) region. The load balancer is configured to distribute HTTP traffic across instances in two Availability Zones.