Fargate en détail
1 - Qu'est-ce que Fargate ?
AWS Fargate est un moteur de calcul serverless pour conteneurs qui fonctionne avec ECS et EKS, éliminant le besoin de provisionner et gérer des serveurs.
2 - Fargate vs EC2 Launch Type
| Aspect | Fargate | EC2 |
|---|---|---|
| Gestion serveurs | Aucune | Vous gérez |
| Scaling | Par task | Cluster + tasks |
| Pricing | Pay-per-task | Pay-per-instance |
| GPU | Non supporté | Supporté |
| Instances Spot | Fargate Spot | EC2 Spot |
| Accès instance | Non | Oui |
| Isolation | Task-level | Instance-level |
3 - Configuration Fargate
3.1 Task Definition Fargate
{
"family": "fargate-app",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"executionRoleArn": "arn:aws:iam::123456789:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "app",
"image": "nginx:latest",
"essential": true,
"portMappings": [
{"containerPort": 80, "protocol": "tcp"}
]
}
]
}
3.2 Platform Versions
| Version | Description |
|---|---|
| LATEST | Dernière version stable |
| 1.4.0 | Support EFS, secrets |
| 1.3.0 | Support Container Insights |
ECSService:
Properties:
PlatformVersion: "1.4.0"
4 - Fargate Spot
Économisez jusqu'à 70% avec les capacités spot.
4.1 Configuration
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
- FARGATE_SPOT
DefaultCapacityProviderStrategy:
- CapacityProvider: FARGATE
Base: 2 # 2 tasks minimum sur Fargate
Weight: 1
- CapacityProvider: FARGATE_SPOT
Weight: 4 # 4x plus de tasks sur Spot
4.2 Stratégie de placement
# 20% Fargate, 80% Fargate Spot
aws ecs create-service \
--cluster prod-cluster \
--service-name web \
--task-definition web:1 \
--desired-count 10 \
--capacity-provider-strategy \
capacityProvider=FARGATE,base=2,weight=1 \
capacityProvider=FARGATE_SPOT,weight=4
4.3 Gestion des interruptions
5 - ARM64 Architecture
5.1 Avantages
- Jusqu'à 40% moins cher
- Meilleure performance par watt
- Idéal pour workloads CPU-bound
5.2 Configuration
{
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
},
"containerDefinitions": [
{
"name": "app",
"image": "my-arm64-image:latest"
}
]
}
5.3 Build multi-architecture
# Dockerfile multi-arch
docker buildx create --use
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myapp:latest \
--push .
6 - Stockage éphémère
6.1 Configuration
{
"ephemeralStorage": {
"sizeInGiB": 100
}
}
| Taille par défaut | Max | Coût supplémentaire |
|---|---|---|
| 20 GB | 200 GB | $0.000111/GB/heure |
6.2 Cas d'usage
- Cache temporaire
- Fichiers de build
- Données de session
- Logs temporaires
7 - EFS avec Fargate
7.1 Configuration
{
"volumes": [
{
"name": "efs-volume",
"efsVolumeConfiguration": {
"fileSystemId": "fs-12345678",
"rootDirectory": "/",
"transitEncryption": "ENABLED",
"transitEncryptionPort": 2049,
"authorizationConfig": {
"accessPointId": "fsap-12345678",
"iam": "ENABLED"
}
}
}
],
"containerDefinitions": [
{
"mountPoints": [
{
"sourceVolume": "efs-volume",
"containerPath": "/data",
"readOnly": false
}
]
}
]
}
7.2 EFS Access Point
EFSAccessPoint:
Type: AWS::EFS::AccessPoint
Properties:
FileSystemId: !Ref EFSFileSystem
PosixUser:
Uid: "1000"
Gid: "1000"
RootDirectory:
CreationInfo:
OwnerUid: "1000"
OwnerGid: "1000"
Permissions: "755"
Path: /app-data
8 - Tarification
8.1 Fargate standard
| Ressource | Prix (eu-west-1) |
|---|---|
| vCPU | $0.04048/heure |
| Memory | $0.004445/GB/heure |
| Storage (> 20GB) | $0.000111/GB/heure |
8.2 Fargate Spot
| Ressource | Prix (eu-west-1) |
|---|---|
| vCPU | ~$0.012/heure (-70%) |
| Memory | ~$0.0013/GB/heure (-70%) |
8.3 Exemple de calcul
1 task : 1 vCPU, 2GB RAM, 24h
Fargate standard :
- vCPU: 0.97
- Memory: 0.21
- Total: $1.18/jour
Fargate Spot :
- Total: ~$0.35/jour (-70%)
9 - Optimisation des coûts
9.1 Right-sizing
# Analyser l'utilisation avec Container Insights
# CloudWatch > Insights > Container Insights > Performance Monitoring
# Métriques clés :
# - CpuUtilized vs CpuReserved
# - MemoryUtilized vs MemoryReserved
9.2 Recommandations
- Utiliser Fargate Spot pour les workloads non-critiques
- Choisir ARM64 quand possible
- Ajuster CPU/Memory selon l'utilisation réelle
- Utiliser le scaling pour s'adapter à la charge
Résumé
Dans ce chapitre, nous avons appris :
- Les avantages de Fargate vs EC2
- La configuration des Task Definitions Fargate
- Fargate Spot pour économiser
- L'architecture ARM64
- Le stockage éphémère et EFS
- La tarification et l'optimisation des coûts
Prochaine étape
Dans le prochain chapitre, nous verrons l'Auto Scaling.
→ Chapitre suivant : Auto Scaling