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/xxx

Health Check:ALB 幫你擋掉壞掉的 instance

# 確認 Target 健康狀態
aws elbv2 describe-target-health \
  --target-group-arn arn:aws:...:targetgroup/app-tg/xxx

Health 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——所以更快、延遲更低。

ALBNLB
層級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 改成 SECONDARY

TLS 憑證:用 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 blockALB Listener Rules
L4 負載均衡HAProxy TCP modeNLB
TLSLet’s Encrypt + certbotACM(免費、自動續期)
Health CheckNginx upstream checkTarget Group Health Check
DNSBind / CloudflareRoute 53
高可用Keepalived + VIPALB/NLB 原生跨 AZ
費用機器費用按流量/LCU 計費

如果你讀過 Reverse Proxy + TLSDNS、負載均衡與 CDN,ALB 就是 Nginx 反向代理的 AWS 版,但你不用管 Nginx config 和 certbot 續期。


K8s 映射

AWS LB/DNSK8s 對應
ALBIngress(nginx-ingress / AWS LB Controller)
NLBService type: LoadBalancer
Target GroupEndpoints(Pod IP list)
Health CheckreadinessProbe / livenessProbe
Route 53ExternalDNS(自動同步 Ingress → DNS)
ACMcert-manager + Let’s Encrypt
ALB path routingIngress path rules

在 EKS 上,用 AWS Load Balancer Controller 可以讓 K8s Ingress 自動建立 ALB。詳見 K8s 網路模型


系列導覽

上一篇下一篇
EKS S3

Load Balancer 是使用者跟你的服務之間的第一道關卡——它掛了,後面再健康都沒用。