ALB / NLB / Route 53:流量入口全攻略
你的 App 跑起來了,但使用者怎麼連進來?直接打 EC2 的 Public IP?那你只是在玩具階段。生產環境需要 Load Balancer + DNS。
先講結論
ALB(Application Load Balancer)處理 HTTP/HTTPS 流量,能做 path-based / host-based routing,是 Web App 的標配。NLB(Network Load Balancer)處理 TCP/UDP,延遲更低、吞吐更高,適合非 HTTP 協定(gRPC、遊戲、IoT)。Route 53 不只是 DNS,它能做 health check + failover routing,是你的高可用入口。
ALB:Web App 的標配
ALB 運作在 L7(HTTP),能看到 HTTP header、path、host——所以能做路由。
建立 ALB
# 1. 建立 ALB
aws elbv2 create-load-balancer --name prod-alb \
--type application \
--subnets subnet-public-1a subnet-public-1c \
--security-groups sg-alb
# 2. 建立 Target Group
aws elbv2 create-target-group --name app-tg \
--protocol HTTP --port 8080 \
--vpc-id vpc-xxx \
--target-type ip \
--health-check-path /health \
--health-check-interval-seconds 15 \
--healthy-threshold-count 2 \
--unhealthy-threshold-count 3
# 3. 建立 Listener(HTTPS)
aws elbv2 create-listener --load-balancer-arn arn:aws:...:loadbalancer/app/prod-alb/xxx \
--protocol HTTPS --port 443 \
--certificates CertificateArn=arn:aws:acm:...:certificate/xxx \
--default-actions Type=forward,TargetGroupArn=arn:aws:...:targetgroup/app-tg/xxx
# 4. HTTP → HTTPS 自動轉址
aws elbv2 create-listener --load-balancer-arn arn:aws:...:loadbalancer/app/prod-alb/xxx \
--protocol HTTP --port 80 \
--default-actions Type=redirect,RedirectConfig='{Protocol=HTTPS,Port=443,StatusCode=HTTP_301}'Path-based Routing
一個 ALB 服務多個 App:
# /api/* → api-service
aws elbv2 create-rule \
--listener-arn arn:aws:...:listener/xxx \
--priority 10 \
--conditions Field=path-pattern,Values='/api/*' \
--actions Type=forward,TargetGroupArn=arn:aws:...:targetgroup/api-tg/xxx
# /admin/* → admin-service
aws elbv2 create-rule \
--listener-arn arn:aws:...:listener/xxx \
--priority 20 \
--conditions Field=path-pattern,Values='/admin/*' \
--actions Type=forward,TargetGroupArn=arn:aws:...:targetgroup/admin-tg/xxxHealth Check:ALB 幫你擋掉壞掉的 instance
# 確認 Target 健康狀態
aws elbv2 describe-target-health \
--target-group-arn arn:aws:...:targetgroup/app-tg/xxxHealth Check 設定建議:
- Path:
/health(不要用/,首頁可能很重) - Interval: 15 秒
- Healthy threshold: 2(連續 2 次通過才算健康)
- Unhealthy threshold: 3(連續 3 次失敗才踢掉)
踩坑:Health Check 的 port 預設跟 Target Group 的 port 一樣。如果你的 App 在 8080 但 health endpoint 在 8081,記得另外設。
NLB:當 ALB 不夠快
NLB 運作在 L4(TCP/UDP),不解析 HTTP——所以更快、延遲更低。
| ALB | NLB | |
|---|---|---|
| 層級 | L7 (HTTP/HTTPS) | L4 (TCP/UDP/TLS) |
| 延遲 | 毫秒級 | 微秒級 |
| 靜態 IP | 否(DNS name) | 是(Elastic IP per AZ) |
| WebSocket | 支援 | 支援 |
| gRPC | 支援 | 支援(但 ALB 更方便) |
| 費用 | 按 LCU 計 | 按 NLCU 計(通常更便宜) |
# 建立 NLB(用於 gRPC 或 TCP 服務)
aws elbv2 create-load-balancer --name prod-nlb \
--type network \
--subnets subnet-public-1a subnet-public-1c
# TCP Target Group
aws elbv2 create-target-group --name grpc-tg \
--protocol TCP --port 50051 \
--vpc-id vpc-xxx \
--target-type ip \
--health-check-protocol TCP什麼時候用 NLB?
- 需要靜態 IP(例如防火牆白名單)
- 非 HTTP 協定(TCP/UDP)
- 極低延遲需求
- 每秒數百萬連線
Route 53:不只是 DNS
Route 53 是 AWS 的 DNS 服務,但它比一般的 DNS 強在路由策略和 health check。
基本設定
# 建立 Hosted Zone
aws route53 create-hosted-zone --name example.com \
--caller-reference $(date +%s)
# 建立 A Record(Alias 指向 ALB)
aws route53 change-resource-record-sets \
--hosted-zone-id Z1234567890 \
--change-batch '{
"Changes": [{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z14GRHDCWA56QT",
"DNSName": "prod-alb-123456.ap-northeast-1.elb.amazonaws.com",
"EvaluateTargetHealth": true
}
}
}]
}'路由策略
| 策略 | 用途 | 場景 |
|---|---|---|
| Simple | 單一目標 | 只有一個 ALB |
| Weighted | 按比例分流 | Canary 部署(90%/10%) |
| Latency | 按延遲選 Region | 多 Region 部署 |
| Failover | 主備切換 | DR(Disaster Recovery) |
| Geolocation | 按使用者地理位置 | 多語言站 / 法規合規 |
Failover 實戰
# Primary Record(ap-northeast-1)
aws route53 change-resource-record-sets \
--hosted-zone-id Z1234567890 \
--change-batch '{
"Changes": [{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"SetIdentifier": "primary",
"Failover": "PRIMARY",
"AliasTarget": {
"HostedZoneId": "Z14GRHDCWA56QT",
"DNSName": "prod-alb-tokyo.ap-northeast-1.elb.amazonaws.com",
"EvaluateTargetHealth": true
}
}
}]
}'
# Secondary Record(us-west-2)
# 同樣格式,Failover 改成 SECONDARYTLS 憑證:用 ACM 免費搞定
# 申請免費的 TLS 憑證
aws acm request-certificate --domain-name "*.example.com" \
--validation-method DNS \
--subject-alternative-names "example.com"
# 拿到 CNAME 驗證記錄後,加到 Route 53
# ACM 會自動驗證並核發憑證ACM 憑證免費、自動續期、只能掛在 AWS 服務上(ALB、CloudFront、API Gateway)。
自架 vs AWS
| 面向 | 自架(Nginx/HAProxy) | AWS ALB/NLB |
|---|---|---|
| L7 路由 | Nginx location block | ALB Listener Rules |
| L4 負載均衡 | HAProxy TCP mode | NLB |
| TLS | Let’s Encrypt + certbot | ACM(免費、自動續期) |
| Health Check | Nginx upstream check | Target Group Health Check |
| DNS | Bind / Cloudflare | Route 53 |
| 高可用 | Keepalived + VIP | ALB/NLB 原生跨 AZ |
| 費用 | 機器費用 | 按流量/LCU 計費 |
如果你讀過 Reverse Proxy + TLS 和 DNS、負載均衡與 CDN,ALB 就是 Nginx 反向代理的 AWS 版,但你不用管 Nginx config 和 certbot 續期。
K8s 映射
| AWS LB/DNS | K8s 對應 |
|---|---|
| ALB | Ingress(nginx-ingress / AWS LB Controller) |
| NLB | Service type: LoadBalancer |
| Target Group | Endpoints(Pod IP list) |
| Health Check | readinessProbe / livenessProbe |
| Route 53 | ExternalDNS(自動同步 Ingress → DNS) |
| ACM | cert-manager + Let’s Encrypt |
| ALB path routing | Ingress path rules |
在 EKS 上,用 AWS Load Balancer Controller 可以讓 K8s Ingress 自動建立 ALB。詳見 K8s 網路模型。
系列導覽
Load Balancer 是使用者跟你的服務之間的第一道關卡——它掛了,後面再健康都沒用。