Tuần 2 - Ngày 4: Elastic Load Balancing (ELB)
Mục tiêu học tập
- Phân biệt 4 loại ELB: ALB, NLB, GLB, CLB
- Hiểu listener, target group, health check
- Nắm sticky session, SSL termination, cross-zone load balancing
- Biết khi nào chọn ALB vs NLB
1. Tổng quan ELB
ELB (Elastic Load Balancer) là managed service phân phối traffic đến nhiều targets (EC2, IP, Lambda, container).
Lý do dùng ELB
- High availability: spread traffic across AZs
- Health check: tự động loại bỏ unhealthy instance
- SSL termination: offload SSL từ backend
- Scalability: tự động scale theo traffic
- Single endpoint: clients chỉ gọi 1 DNS name của LB
2. 4 loại ELB
| Type | Layer | Protocol | Use case |
|---|---|---|---|
| Application Load Balancer (ALB) | 7 | HTTP, HTTPS, gRPC, WebSocket | Microservices, container, path-based routing |
| Network Load Balancer (NLB) | 4 | TCP, UDP, TLS | High performance, static IP, < 1ms latency |
| Gateway Load Balancer (GLB) | 3 (gateway) | IP packets | Third-party virtual appliances (firewall, IDS) |
| Classic Load Balancer (CLB) | 4 & 7 | HTTP, HTTPS, TCP, SSL | Legacy — không dùng cho thiết kế mới |
CLB đã deprecated cho EC2-Classic, vẫn còn cho VPC nhưng AWS khuyến nghị dùng ALB/NLB.
3. Application Load Balancer (ALB)
Đặc điểm
- Layer 7 (HTTP/HTTPS)
- Route theo:
- Host header (api.example.com vs www.example.com)
- Path (/api/* vs /static/*)
- HTTP method (GET, POST)
- Query string (?version=v2)
- Source IP
- HTTP header
- Hỗ trợ WebSocket, HTTP/2, gRPC
- SSL termination + SNI multi-domain
- DNS name thay đổi theo thời gian (KHÔNG dùng static IP)
Targets
- EC2 instances
- IP addresses (on-prem, VPC)
- Lambda functions (ALB invoke Lambda như HTTP backend)
- Containers (ECS, EKS)
Sticky Sessions (Session Affinity)
- Application-based cookie (do app gen) hoặc load balancer-cookie (
AWSALB) - Đảm bảo user request đi về cùng 1 target trong session
- Duration: configurable (1 second - 7 days)
Health Check
- HTTP/HTTPS probe
- Customize: path, port, interval, threshold
- ALB chỉ route traffic đến healthy targets
4. Network Load Balancer (NLB)
Đặc điểm
- Layer 4 (TCP, UDP, TLS)
- Cực kỳ performance: hàng triệu requests/giây, latency < 1ms
- Static IP per AZ (hoặc Elastic IP)
- Source IP preservation: backend thấy IP thật của client (không phải IP của NLB)
- Mặc định không có Security Group (chuyển từ 2023 NLB có thể có SG)
- Hỗ trợ PrivateLink (NLB là backend cho VPC Endpoint Service)
Use cases
- Gaming server (UDP)
- IoT (high connection)
- Streaming media
- Trading platform (low latency)
- Cần static IP cho whitelisting
Sticky Sessions
- Source IP affinity (hash source IP → cùng target)
- Per-flow stickiness, không cookie
SSL/TLS Termination
- NLB có thể terminate TLS (TLS listener)
- Hoặc pass-through TCP (backend tự handle TLS)
5. Gateway Load Balancer (GWLB)
Định nghĩa
GWLB distribute traffic đến third-party virtual appliances (firewall, IDS/IPS, packet inspection) ở Layer 3.
Workflow
Đặc điểm
- Hỗ trợ Palo Alto, Fortinet, Check Point... appliances trên AWS Marketplace
- Single entry point cho all traffic inspection
- Scale horizontal với multiple appliances
Use case
- Centralized security inspection
- DPI (deep packet inspection)
- Compliance traffic logging
6. Listener và Target Group
Listener
- Lắng nghe trên port/protocol cụ thể
- 1 LB có thể có nhiều listeners (HTTP:80, HTTPS:443)
- Forward traffic đến target group dựa trên rules
Target Group
- Tập hợp targets (EC2/IP/Lambda) cùng kiểu
- Có health check riêng
- 1 target group có thể được dùng bởi nhiều LB
- 1 LB listener forward đến 1 hoặc nhiều target groups (weighted)
Architecture
7. ALB Routing Rules
Listener Rules
Mỗi listener có priority list of rules:
Advanced Actions
- Forward to target group(s)
- Redirect (HTTP → HTTPS)
- Fixed response (return static 503, 404...)
- Authenticate với Cognito hoặc OIDC
8. SSL/TLS với ELB
ALB
- Terminate TLS, decrypt, route HTTP đến backend
- SNI (Server Name Indication): nhiều SSL certs trên cùng listener cho multi-domain
- Cert từ AWS Certificate Manager (ACM) — free cho ALB/NLB/CloudFront
NLB
- TLS termination với TLS listener
- Hoặc TCP pass-through (backend tự decrypt)
- Hỗ trợ TLS 1.3
Security Policy
- Predefined cipher suite policies:
ELBSecurityPolicy-TLS13-1-2-2021-06 - Compliance: FIPS-140-2 policies có sẵn
9. Cross-Zone Load Balancing
Định nghĩa
Cross-zone = LB distribute traffic đều giữa tất cả targets across AZs thay vì chỉ trong AZ của LB node.
Ví dụ
Default behavior
| LB type | Default | Cost cross-AZ |
|---|---|---|
| ALB | Enabled, free (không tính cross-AZ data) | Free |
| NLB | Disabled by default (có thể enable, tính phí cross-AZ data) | Cost |
| GWLB | Disabled by default | Cost |
| CLB | Disabled (enable free) | Free |
10. Health Check
Cấu hình
- Protocol: HTTP, HTTPS, TCP
- Port: target port hoặc traffic port
- Path (HTTP):
/health,/healthz - Interval: 5-300s (mặc định 30s)
- Timeout: 2-120s (mặc định 5s)
- Healthy threshold: số lần pass liên tiếp → healthy (mặc định 5)
- Unhealthy threshold: số lần fail liên tiếp → unhealthy (mặc định 2)
- Matcher: HTTP code success (mặc định 200)
Best practice
- Endpoint
/healthriêng, không phải/(vì homepage có thể slow) /healthcheck dependencies (DB connection, cache)- Return 200 nếu healthy, 503 nếu draining
Connection Draining (Deregistration Delay)
- Khi target deregister hoặc unhealthy:
- LB ngừng gửi request mới
- Chờ existing connections complete (default 300s)
- Tránh disconnect đột ngột user đang dùng
11. ALB vs NLB — Khi nào dùng cái nào?
| Yêu cầu | Chọn |
|---|---|
| HTTP routing theo path/host/header | ALB |
| WebSocket, HTTP/2, gRPC | ALB |
| Lambda backend | ALB |
| Cognito/OIDC authentication | ALB |
| TCP/UDP non-HTTP traffic | NLB |
| Static IP cho whitelisting | NLB |
| Hàng triệu req/s, sub-ms latency | NLB |
| Source IP preservation | NLB |
| Private endpoint cho service (PrivateLink) | NLB |
| Inspection firewall appliances | GWLB |
Câu hỏi ôn tập
-
ALB và NLB hoạt động ở Layer nào của OSI?
Xem đáp án
ALB (Application Load Balancer): Layer 7 (HTTP/HTTPS/WebSocket) — hiểu content-based routing (path, header, hostname, query string). NLB (Network Load Balancer): Layer 4 (TCP/UDP/TLS) — routing dựa trên IP + port, không inspect payload. GLB (Gateway LB) hoạt động ở Layer 3/4 dành cho security appliances. CLB (Classic) là legacy, hỗ trợ cả L4 và L7 cơ bản.
-
NLB có static IP, ALB thì sao?
Xem đáp án
NLB cấp static IP cho mỗi AZ (1 IP/AZ) — IP không thay đổi kể cả khi scale. ALB không có static IP — chỉ có DNS name (hostname); IP thay đổi khi AWS scale ALB internally. Nếu cần static IP cho ALB (ví dụ: firewall whitelist), dùng AWS Global Accelerator trước ALB — Global Accelerator cung cấp static anycast IPs.
-
Cross-zone load balancing default behavior khác nhau giữa ALB và NLB?
Xem đáp án
ALB: cross-zone load balancing bật mặc định và không tính phí thêm. NLB: cross-zone load balancing tắt mặc định (chỉ route trong AZ nhận traffic); bật thêm sẽ tính phí data transfer cross-AZ ($0.01/GB). Lý do: NLB thường dùng cho latency-sensitive workloads, cross-AZ adds latency.
-
ALB target type nào có thể được Lambda function?
Xem đáp án
ALB hỗ trợ 3 target types: IP (IP addresses), Instance (EC2 instance IDs), và Lambda (Lambda function ARN). Khi chọn Lambda target type, ALB invoke Lambda synchronously với event chứa HTTP request details. Không cần API Gateway — ALB có thể làm HTTP frontend trực tiếp cho Lambda, rẻ hơn API Gateway cho high-volume traffic.
-
Khi cần WebSocket support, chọn LB nào?
Xem đáp án
ALB hỗ trợ WebSocket (và HTTP/2). ALB duy trì connection lâu dài (long-lived connections) cần cho WebSocket. NLB cũng hỗ trợ WebSocket vì nó hoạt động ở Layer 4 (TCP) — nhưng không có sticky sessions hay content-based routing. Thường chọn ALB cho WebSocket apps cần routing intelligence; NLB cho high-performance TCP WebSocket connections.
Bài tập thực hành
- Tạo ALB với 2 target groups (web và api), route path-based
- Setup health check endpoint
/healthtrả 200, test với 1 instance fail - Enable HTTPS với ACM certificate, redirect HTTP → HTTPS
- Tạo NLB với UDP listener (port 53 simulate DNS)
- Enable cross-zone load balancing cho NLB, observe cost change
Tài liệu tham khảo chính thức
- Elastic Load Balancing
- Application Load Balancer
- Network Load Balancer
- Gateway Load Balancer
- ALB Listener Rules
Tiếp theo: Auto Scaling