Load Balancers AWS
Qu'est-ce qu'un Load Balancer ?
Un Load Balancer (répartiteur de charge) distribue le trafic entrant entre plusieurs serveurs pour améliorer la disponibilité et les performances.
┌─────────────────────────────────────────────────────────────┐
│ LOAD BALANCER - CONCEPT │
├─────────────────────────────────────────────────────────────┤
│ │
│ Utilisateurs │
│ (1000 requêtes) │
│ │ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Load Balancer│ │
│ │ │ │
│ │ Distribue │ │
│ │ le trafic │ │
│ └──────┬───────┘ │
│ │ │
│ ┌─────────────┼─────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ Server │ │ Server │ │ Server │ │
│ │ 1 │ │ 2 │ │ 3 │ │
│ │ ~333 │ │ ~333 │ │ ~333 │ │
│ │ req │ │ req │ │ req │ │
│ └────────┘ └────────┘ └────────┘ │
│ │
│ Avantages : │
│ ✅ Distribution de charge │
│ ✅ Haute disponibilité │
│ ✅ Scaling horizontal │
│ ✅ Health checks automatiques │
│ │
└─────────────────────────────────────────────────────────────┘
Types de Load Balancers AWS
Comparaison
┌─────────────────────────────────────────────────────────────┐
│ TYPES DE LOAD BALANCERS AWS │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ALB (Application) NLB (Network) │ │
│ │ ───────────────── ─────────────── │ │
│ │ Layer 7 (HTTP) Layer 4 (TCP/UDP) │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ HTTP/HTTPS │ │ TCP/UDP │ │ │
│ │ │ │ │ │ │ │
│ │ │ Path-based │ │ Ultra-fast │ │ │
│ │ │ routing │ │ millions │ │ │
│ │ │ │ │ req/sec │ │ │
│ │ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ │ Cas d'usage : Cas d'usage : │ │
│ │ • Sites web • Gaming │ │
│ │ • APIs REST • IoT │ │
│ │ • Microservices • VoIP │ │
│ │ • TCP pur │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ GWLB (Gateway) CLB (Classic) │ │
│ │ ────────────── ───────────── │ │
│ │ Layer 3 (IP) Legacy │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Appliances │ │ ANCIEN │ │ │
│ │ │ virtuelles │ │ À éviter │ │ │
│ │ │ │ │ │ │ │
│ │ │ Firewalls │ │ Utilisez │ │ │
│ │ │ IDS/IPS │ │ ALB/NLB │ │ │
│ │ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Tableau Comparatif
| Caractéristique | ALB | NLB | GWLB | CLB |
|---|---|---|---|---|
| Couche OSI | 7 | 4 | 3 | 4/7 |
| Protocoles | HTTP, HTTPS, gRPC | TCP, UDP, TLS | IP | TCP, SSL, HTTP |
| Performance | Bonne | Ultra | Très bonne | Moyenne |
| IP statique | Non | Oui | Oui | Non |
| WebSocket | Oui | Oui | Non | Oui |
| Routage avancé | Oui | Non | Non | Non |
| Prix | $$ | $$$ | $$$ | $ |
| Recommandé | ✅ Web | ✅ TCP | Firewalls | ❌ Legacy |
Application Load Balancer (ALB)
Architecture ALB
┌─────────────────────────────────────────────────────────────┐
│ ARCHITECTURE ALB │
├─────────────────────────────────────────────────────────────┤
│ │
│ Internet │
│ │ │
│ ▼ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ ALB │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ LISTENERS │ │ │
│ │ │ │ │ │
│ │ │ ┌────────────┐ ┌────────────┐ │ │ │
│ │ │ │ Port 80 │ │ Port 443 │ │ │ │
│ │ │ │ HTTP │ │ HTTPS │ │ │ │
│ │ │ └─────┬──────┘ └─────┬──────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └────────┼──────────────────┼─────────────────────┘ │ │
│ │ │ │ │ │
│ │ ┌────────┼──────────────────┼─────────────────────┐ │ │
│ │ │ │ RULES │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ IF path=/api/* IF path=/images/* │ │ │
│ │ │ │ │ │ │ │
│ │ └────────┼──────────────────┼─────────────────────┘ │ │
│ │ │ │ │ │
│ └───────────┼──────────────────┼─────────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌───────────────────┐ ┌───────────────────┐ │
│ │ TARGET GROUP │ │ TARGET GROUP │ │
│ │ "api-servers" │ │ "image-servers" │ │
│ │ │ │ │ │
│ │ ┌────┐ ┌────┐ │ │ ┌────┐ ┌────┐ │ │
│ │ │EC2 │ │EC2 │ │ │ │EC2 │ │EC2 │ │ │
│ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │
│ └───────────────────┘ └───────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Composants ALB
| Composant | Description | Exemple |
|---|---|---|
| Listener | Point d'entrée (port) | HTTP:80, HTTPS:443 |
| Rule | Condition de routage | IF path=/api/* THEN... |
| Target Group | Groupe de serveurs cibles | EC2, IP, Lambda |
| Target | Serveur individuel | i-xxx sur port 8080 |
Routage Avancé ALB
┌─────────────────────────────────────────────────────────────┐
│ RÈGLES DE ROUTAGE ALB │
├─────────────────────────────────────────────────────────────┤
│ │
│ Conditions disponibles : │
│ │
│ • Path : /api/*, /images/*, /static/* │
│ • Host : api.example.com, www.example.com │
│ • Headers : X-Custom-Header = value │
│ • Query : ?version=v2 │
│ • Source IP : 10.0.0.0/8 │
│ • HTTP Method : GET, POST │
│ │
│ Exemple de règles : │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Rule 1: IF path = /api/* │ │
│ │ THEN forward to tg-api-servers │ │
│ │ │ │
│ │ Rule 2: IF host = admin.example.com │ │
│ │ THEN forward to tg-admin │ │
│ │ │ │
│ │ Rule 3: IF query = ?v2 │ │
│ │ THEN forward to tg-api-v2 │ │
│ │ │ │
│ │ Default: forward to tg-web-servers │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Configuration ALB
Via AWS CLI
# 1. Créer le Target Group
aws elbv2 create-target-group \
--name tg-web-servers \
--protocol HTTP \
--port 80 \
--vpc-id vpc-xxx \
--target-type instance \
--health-check-path /health \
--health-check-interval-seconds 30
# 2. Créer l'ALB
aws elbv2 create-load-balancer \
--name my-alb \
--subnets subnet-1a subnet-1b subnet-1c \
--security-groups sg-alb \
--scheme internet-facing \
--type application
# 3. Créer le Listener HTTP
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:...:my-alb \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:...:tg-web-servers
# 4. Enregistrer les targets
aws elbv2 register-targets \
--target-group-arn arn:aws:...:tg-web-servers \
--targets Id=i-server1 Id=i-server2
Security Group pour ALB
┌─────────────────────────────────────────────────────────────┐
│ SECURITY GROUPS ALB + EC2 │
├─────────────────────────────────────────────────────────────┤
│ │
│ sg-alb (pour le Load Balancer) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: │ │
│ │ HTTP (80) from 0.0.0.0/0 │ │
│ │ HTTPS (443) from 0.0.0.0/0 │ │
│ │ │ │
│ │ Outbound: │ │
│ │ HTTP (80) to sg-web-servers │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ sg-web-servers (pour les EC2) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Inbound: │ │
│ │ HTTP (80) from sg-alb ← SEULEMENT depuis ALB │ │
│ │ SSH (22) from sg-bastion │ │
│ │ │ │
│ │ Outbound: │ │
│ │ All traffic to 0.0.0.0/0 │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ ⚠️ Les EC2 n'acceptent le trafic HTTP QUE de l'ALB │
│ → Impossible d'accéder directement aux EC2 │
│ │
└─────────────────────────────────────────────────────────────┘
Network Load Balancer (NLB)
Quand utiliser NLB ?
┌─────────────────────────────────────────────────────────────┐
│ QUAND UTILISER NLB │
├─────────────────────────────────────────────────────────────┤
│ │
│ ✅ UTILISEZ NLB POUR : │
│ │
│ • Performance extrême (millions de requêtes/sec) │
│ • Latence ultra-faible (< 100 μs) │
│ • Protocoles TCP/UDP purs (pas HTTP) │
│ • Besoin d'IP statique │
│ • Gaming, IoT, streaming │
│ • Applications ne supportant pas proxy │
│ │
│ ❌ N'UTILISEZ PAS NLB POUR : │
│ │
│ • Sites web standard (utilisez ALB) │
│ • APIs REST (utilisez ALB) │
│ • Besoin de routage par path/host │
│ │
│ Caractéristiques NLB : │
│ • Preserve l'IP source du client │
│ • IP statique ou Elastic IP possible │
│ • Zonal isolation (failover par AZ) │
│ • Cross-zone load balancing (optionnel) │
│ │
└─────────────────────────────────────────────────────────────┘
Coûts des Load Balancers
┌─────────────────────────────────────────────────────────────┐
│ ESTIMATION COÛTS (us-east-1) │
├─────────────────────────────────────────────────────────────┤
│ │
│ ALB : │
│ • $0.0225 / heure ≈ $16 / mois (fixe) │
│ • $0.008 / LCU-heure (usage) │
│ • Estimation typique : $20-50 / mois │
│ │
│ NLB : │
│ • $0.0225 / heure ≈ $16 / mois (fixe) │
│ • $0.006 / NLCU-heure (usage) │
│ • Estimation typique : $20-100 / mois │
│ │
│ LCU (Load Balancer Capacity Unit) : │
│ Basé sur : │
│ • Nouvelles connexions / seconde │
│ • Connexions actives │
│ • Données traitées │
│ │
└─────────────────────────────────────────────────────────────┘
Résumé
| Aspect | ALB | NLB |
|---|---|---|
| Couche | 7 (HTTP) | 4 (TCP) |
| Cas d'usage | Web, API | TCP pur, gaming |
| Routage avancé | ✅ Oui | ❌ Non |
| IP statique | ❌ Non | ✅ Oui |
| Performance | Bonne | Extrême |
| Recommandation | 90% des cas | Cas spéciaux |
Prochaine section : Health Checks