Aller au contenu principal

Concepts Auto Scaling

Qu'est-ce que l'Auto Scaling ?​

L'Auto Scaling permet d'ajuster automatiquement le nombre d'instances EC2 en fonction de la demande. Cela garantit les performances pendant les pics de trafic et réduit les coûts pendant les périodes creuses.

┌─────────────────────────────────────────────────────────────┐
│ AUTO SCALING - CONCEPT │
├─────────────────────────────────────────────────────────────┤
│ │
│ SANS AUTO SCALING │
│ ───────────────── │
│ │
│ Capacité ▲ │
│ │ ┌─────────────────────────────────┐ │
│ │ │ Capacité fixe (10 instances) │ │
│ │ ───┴─────────────────────────────────┴─── │
│ │ /\ │
│ │ / \ Demande réelle │
│ │ / \___________ │
│ │ / \ │
│ │ / \___ │
│ └─────────────────────────────► Temps │
│ │
│ Problèmes : ❌ Sur-provisionné la nuit = gaspillage │
│ ❌ Sous-provisionné aux pics = lenteur │
│ │
│ ──────────────────────────────────────────────────────── │
│ │
│ AVEC AUTO SCALING │
│ ───────────────── │
│ │
│ Capacité ▲ │
│ │ ________ │
│ │ / \ Capacité s'adapte │
│ │ / \___________ │
│ │ / \ │
│ │ / \___ │
│ └─────────────────────────────────► Temps │
│ │
│ Avantages : ✅ Capacité = Demande (toujours) │
│ ✅ Coûts optimisés │
│ ✅ Performance maintenue │
│ │
└─────────────────────────────────────────────────────────────┘

Composants d'Auto Scaling​

Architecture​

┌─────────────────────────────────────────────────────────────┐
│ COMPOSANTS AUTO SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ LAUNCH TEMPLATE │ │
│ │ │ │
│ │ "Comment créer les nouvelles instances" │ │
│ │ • AMI : ami-xxxxx │ │
│ │ • Type : t3.medium │ │
│ │ • Security Group : sg-web │ │
│ │ • User Data : script de démarrage │ │
│ │ • Key Pair : my-key │ │
│ │ │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AUTO SCALING GROUP (ASG) │ │
│ │ │ │
│ │ "Combien d'instances et où" │ │
│ │ • Min : 2 │ │
│ │ • Max : 10 │ │
│ │ • Desired : 4 │ │
│ │ • Subnets : subnet-1a, subnet-1b │ │
│ │ • Health Check : EC2 ou ELB │ │
│ │ │ │
│ └──────────────────────┬──────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ SCALING POLICIES │ │
│ │ │ │
│ │ "Quand ajouter/retirer des instances" │ │
│ │ • CPU > 70% → +2 instances │ │
│ │ • CPU < 30% → -1 instance │ │
│ │ • Schedule : 8h → 6 instances, 20h → 2 instances │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Tableau des Composants​

ComposantRôleExemple
Launch TemplateDéfinit la config des instancesAMI, type, SG, user data
ASGGère le groupe d'instancesMin/Max/Desired, subnets
Scaling PolicyRègles de scalingCPU > 70% → scale out
CloudWatch AlarmDéclenche le scalingAlerte sur métrique

Types de Scaling​

1. Manual Scaling​

Modification manuelle du nombre d'instances.

# Changer le nombre désiré manuellement
aws autoscaling set-desired-capacity \
--auto-scaling-group-name my-asg \
--desired-capacity 5

Cas d'usage : Événements prévisibles (Black Friday prévu)

2. Scheduled Scaling​

Scaling basé sur un calendrier prédéfini.

┌─────────────────────────────────────────────────────────────┐
│ SCHEDULED SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Exemple : Site e-commerce avec trafic prévisible │
│ │
│ Instances ▲ │
│ │ │
│ 10─┤ ┌───────────────┐ │
│ │ │ │ │
│ 6─┤ ┌────┘ └────┐ │
│ │ │ │ │
│ 2─┤─────┘ └───── │
│ │ │
│ └─────┬─────┬─────┬─────┬─────┬─────► Heure │
│ 6h 9h 12h 18h 22h │
│ │
│ Règles : │
│ • 6h → min:2, max:6 (avant ouverture) │
│ • 9h → min:6, max:10 (heures de pointe) │
│ • 22h → min:2, max:4 (nuit) │
│ │
└─────────────────────────────────────────────────────────────┘
# Créer une action schedulée
aws autoscaling put-scheduled-update-group-action \
--auto-scaling-group-name my-asg \
--scheduled-action-name scale-up-morning \
--recurrence "0 9 * * MON-FRI" \
--min-size 6 \
--max-size 10 \
--desired-capacity 6

3. Dynamic Scaling (Target Tracking)​

Le plus recommandé. AWS maintient automatiquement une métrique cible.

┌─────────────────────────────────────────────────────────────┐
│ TARGET TRACKING SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Objectif : Maintenir CPU à 50% │
│ │
│ CPU ▲ │
│ │ │
│ 80─┤ x x │
│ │ x x ← Trop haut → Scale OUT │
│ 50─┤──────────────────────────────────────── Target │
│ │ x │
│ 20─┤ x x ← Trop bas → Scale IN │
│ │ │
│ └─────────────────────────────────────────► Temps │
│ │
│ AWS ajuste automatiquement le nombre d'instances │
│ pour maintenir le CPU autour de 50% │
│ │
│ Métriques disponibles : │
│ • ASGAverageCPUUtilization │
│ • ASGAverageNetworkIn │
│ • ASGAverageNetworkOut │
│ • ALBRequestCountPerTarget │
│ │
└─────────────────────────────────────────────────────────────┘
# Créer une politique Target Tracking
aws autoscaling put-scaling-policy \
--auto-scaling-group-name my-asg \
--policy-name cpu-target-policy \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"TargetValue": 50.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
}
}'

4. Step Scaling​

Scaling par paliers selon l'intensité de la charge.

┌─────────────────────────────────────────────────────────────┐
│ STEP SCALING │
├─────────────────────────────────────────────────────────────┤
│ │
│ Règles de scaling par paliers : │
│ │
│ CPU% Action │
│ ───── ────── │
│ 0-40% Retirer 2 instances │
│ 40-60% Aucune action │
│ 60-80% Ajouter 1 instance │
│ 80-100% Ajouter 3 instances │
│ │
│ ┌───────────────────────────────────────────────────┐ │
│ │ │ │
│ │ 100% ┬─────────────────────────────┐ │ │
│ │ │ +3 instances │ │ │
│ │ 80% ┼─────────────────────────────┼ │ │
│ │ │ +1 instance │ │ │
│ │ 60% ┼─────────────────────────────┼ │ │
│ │ │ (rien) │ │ │
│ │ 40% ┼─────────────────────────────┼ │ │
│ │ │ -2 instances │ │ │
│ │ 0% └─────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Comparaison des Types​

TypeQuand l'utiliserRéactivité
ManualÉvénements connus à l'avanceImmédiate
ScheduledPatterns prévisibles (horaires)Planifiée
Target TrackingCharge variable, recommandé par défaut~3 min
StepRéponse proportionnelle à la charge~3 min

Comportement de Scaling​

Scale Out vs Scale In​

┌─────────────────────────────────────────────────────────────┐
│ SCALE OUT vs SCALE IN │
├─────────────────────────────────────────────────────────────┤
│ │
│ SCALE OUT (ajouter) │
│ ─────────────────── │
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
│ │ 1 │ │ 2 │ → │ 1 │ │ 2 │ │ 3 │ │ 4 │ │
│ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │
│ 2 instances 4 instances │
│ │
│ Comportement : │
│ • Rapide (instances lancées dès que seuil atteint) │
│ • Pas de cooldown par défaut pour Target Tracking │
│ │
│ ──────────────────────────────────────────────────────── │
│ │
│ SCALE IN (retirer) │
│ ────────────────── │
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
│ │ 1 │ │ 2 │ │ 3 │ │ 4 │ → │ 1 │ │ 2 │ │
│ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │
│ 4 instances 2 instances │
│ │
│ Comportement : │
│ • Plus lent (évite les oscillations) │
│ • Cooldown de 300s par défaut │
│ • Termination policy détermine quelle instance │
│ │
└─────────────────────────────────────────────────────────────┘

Termination Policy​

Quelle instance terminer en premier lors du scale in ?

PolicyComportement
DefaultPlus ancienne AZ avec plus d'instances, puis la plus ancienne config
OldestInstanceInstance la plus ancienne
NewestInstanceInstance la plus récente
OldestLaunchConfigConfig de lancement la plus ancienne
ClosestToNextInstanceHourProche de la prochaine heure de facturation

Cooldown et Warmup​

┌─────────────────────────────────────────────────────────────┐
│ COOLDOWN ET WARMUP │
├─────────────────────────────────────────────────────────────┤
│ │
│ COOLDOWN (après scaling) │
│ ──────────────────────── │
│ Période où aucun scaling supplémentaire ne se produit │
│ → Évite les oscillations │
│ │
│ Temps ─────► │
│ │ │
│ │ Scale Cooldown Scale Cooldown │
│ │ OUT 300s IN 300s │
│ │ │ ├─────┤ │ ├─────┤ │
│ │ ▼ │ │ ▼ │ │ │
│ │ ┌───┐ │ │ ┌───┐ │ │ │
│ │ │+2 │ │ pas │ │-1 │ │ pas │ │
│ │ │ │ │ de │ │ │ │ de │ │
│ │ │ │ │scale│ │ │ │scale│ │
│ │ └───┘ │ │ └───┘ │ │ │
│ └──────────┴─────┴────────┴───────┴─────┴──────────► │
│ │
│ WARMUP (avant métriques) │
│ ──────────────────────── │
│ Temps pour qu'une nouvelle instance soit "chaude" │
│ → Ses métriques ne comptent pas avant warmup │
│ │
│ Défaut Target Tracking : 300 secondes │
│ │
└─────────────────────────────────────────────────────────────┘

Résumé​

ConceptDescriptionValeur Typique
Launch TemplateConfig des nouvelles instancesAMI + type + SG
ASGGroupe d'instances managémin:2, max:10
Target TrackingScaling automatique sur métriqueCPU 50%
CooldownPause après scaling300s
WarmupDélai avant comptage métriques300s

Prochaine section : Politiques de Scaling