The shop runs on one instance. When it stops, the shop is gone: one server is one point of failure. Two servers help only if users can reach either one at the same address.
A load balancer is that address: one DNS name in front of several servers. It spreads requests over them and stops sending any to a server that fails.
| Type | Works with | Use it for |
|---|---|---|
| Application (ALB) | HTTP requests: paths, hosts | websites and APIs |
| Network (NLB) | TCP connections, fixed IPs | very high traffic, non-HTTP |
A Gateway Load Balancer sends traffic through firewall appliances; this course does not need one. Here it is an ALB.
The target group
A target group lists the servers (targets) and how to check them. $VPC, $SUBNET_A and $SUBNET_B hold the default VPC and a subnet in each of two zones, found as in Chapter 4.
TG=$(aws elbv2 create-target-group --name web-tg --protocol HTTP --port 80 --vpc-id $VPC --query "TargetGroups[0].TargetGroupArn" --output text)aws elbv2 register-targets --target-group-arn $TG --targets Id=$WEB1 Id=$WEB2The load balancer and its listener
An ALB lives in at least two zones, one public subnet each, with its own security group ($ALB_SG, open on 80 to the world).
LB=$(aws elbv2 create-load-balancer --name market-alb --subnets $SUBNET_A $SUBNET_B --security-groups $ALB_SG --query "LoadBalancers[0].LoadBalancerArn" --output text)A listener takes a port and forwards it to the group:
aws elbv2 create-listener --load-balancer-arn $LB --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=$TGIt is provisioning for about a minute, then active:
aws elbv2 describe-load-balancers --names market-alb --query "LoadBalancers[0].[State.Code,DNSName]" --output textKeep the name with DNS=$(aws elbv2 describe-load-balancers --names market-alb --query "LoadBalancers[0].DNSName" --output text):
curl -s http://$DNS/Run it a few times: both servers answer, in no fixed order, because each zone's node spreads its own requests.