AWS Load Balancing
Amazon Web Services (AWS) provides several services for distributing incoming application traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones. This process is known as load balancing. AWS offers three types of load balancers: Application Load Balancer (ALB), Network Load Balancer (NLB), and Classic Load Balancer (CLB). Below, we'll delve into each type, how they work, and provide example codes for setting them up.
1. Types of AWS Load Balancers
1.1 Application Load Balancer (ALB)
The Application Load Balancer operates at the application layer (Layer 7) and provides advanced request routing based on HTTP/HTTPS. It's suitable for microservices and container-based applications.
1.2 Network Load Balancer (NLB)
The Network Load Balancer operates at the transport layer (Layer 4) and is capable of handling millions of requests per second while maintaining ultra-low latencies. It is designed for load balancing TCP, UDP, and TLS traffic.
1.3 Classic Load Balancer (CLB)
The Classic Load Balancer supports both Layer 4 and Layer 7, but it is generally considered legacy. It is suitable for simple load balancing of traffic across multiple EC2 instances.
2. Setting Up an Application Load Balancer (ALB)
2.1 Prerequisites
- An AWS account
- AWS CLI installed and configured
- At least two running EC2 instances in different Availability Zones
2.2 Step-by-Step Guide
Step 1: Create a Target Group
A target group is used to route requests to one or more registered targets (EC2 instances).
aws elbv2 create-target-group --name my-targets --protocol HTTP --port 80 --vpc-id vpc-xxxxxxxx
Step 2: Register Targets with the Target Group
Register your EC2 instances with the target group.
aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/xxxxxxxx --targets Id=i-xxxxxxxx Id=i-yyyyyyyy
Step 3: Create an ALB
Create an Application Load Balancer in your VPC.
aws elbv2 create-load-balancer --name my-alb --subnets subnet-xxxxxxxx subnet-yyyyyyyy --security-groups sg-xxxxxxxx
Step 4: Create a Listener
A listener checks for connection requests from clients, using the protocol and port you configure.
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:account-id:loadbalancer/app/my-alb/xxxxxxxx --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/xxxxxxxx
3. Setting Up a Network Load Balancer (NLB)
3.1 Prerequisites
- An AWS account
- AWS CLI installed and configured
- At least two running EC2 instances in different Availability Zones
3.2 Step-by-Step Guide
Step 1: Create a Target Group
Create a target group for TCP traffic.
aws elbv2 create-target-group --name my-nlb-targets --protocol TCP --port 80 --vpc-id vpc-xxxxxxxx
Step 2: Register Targets with the Target Group
Register your EC2 instances with the target group.
aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-nlb-targets/xxxxxxxx --targets Id=i-xxxxxxxx Id=i-yyyyyyyy
Step 3: Create an NLB
Create a Network Load Balancer.
aws elbv2 create-load-balancer --name my-nlb --type network --subnets subnet-xxxxxxxx subnet-yyyyyyyy
Step 4: Create a Listener
Create a listener for the NLB.
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:account-id:loadbalancer/net/my-nlb/xxxxxxxx --protocol TCP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-nlb-targets/xxxxxxxx
4. Setting Up a Classic Load Balancer (CLB)
4.1 Prerequisites
- An AWS account
- AWS CLI installed and configured
- At least two running EC2 instances in different Availability Zones
4.2 Step-by-Step Guide
Step 1: Create a Classic Load Balancer
Create a Classic Load Balancer.
aws elb create-load-balancer --load-balancer-name my-clb --listeners "Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80" --subnets subnet-xxxxxxxx subnet-yyyyyyyy --security-groups sg-xxxxxxxx
Step 2: Register Instances with the Load Balancer
Register your EC2 instances with the load balancer.
aws elb register-instances-with-load-balancer --load-balancer-name my-clb --instances i-xxxxxxxx i-yyyyyyyy
5. Additional Configurations
5.1 Health Checks
Health checks ensure that traffic is only routed to healthy instances.
aws elbv2 modify-target-group --target-group-arn arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/xxxxxxxx --health-check-protocol HTTP --health-check-port 80 --health-check-path /health
5.2 SSL Termination (for ALB and NLB)
For secure (HTTPS) connections, you can set up SSL termination.
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:account-id:loadbalancer/app/my-alb/xxxxxxxx --protocol HTTPS --port 443 --certificates CertificateArn=arn:aws:acm:region:account-id:certificate/xxxxxxxx --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/xxxxxxxx
6. Monitoring and Logging
AWS provides tools to monitor and log the performance of your load balancers.
6.1 CloudWatch Metrics
You can monitor your load balancer using CloudWatch metrics.
aws cloudwatch get-metric-statistics --namespace AWS/ELB --metric-name RequestCount --dimensions Name=LoadBalancerName,Value=my-alb --start-time 2022-01-01T00:00:00Z --end-time 2022-01-02T00:00:00Z --period 300 --statistics Average
6.2 Access Logs
Enable access logs to record all requests sent to your load balancer.
aws elb enable-access-logs --load-balancer-name my-clb --s3-bucket-name my-logs-bucket --s3-bucket-prefix my-app
AWS Load Balancing provides robust and scalable solutions for distributing traffic across multiple targets. By setting up ALB, NLB, and CLB, you can ensure high availability and fault tolerance for your applications. The example codes provided above give a basic overview of how to configure these load balancers using AWS CLI. For more detailed configurations and advanced settings, refer to the AWS documentation.