Erreur32 bu gisti düzenledi 4 months ago. Düzenlemeye git
1 file changed, 144 insertions
Docker aide limit cpu Ram compose.md (dosya oluşturuldu)
| @@ -0,0 +1,144 @@ | |||
| 1 | + | # 📋 Docker Compose - Limitation CPU & RAM | |
| 2 | + | ||
| 3 | + | ## 📌 **Méthodes Principales** | |
| 4 | + | ||
| 5 | + | ### **1. Pour usage local (standalone)** | |
| 6 | + | ```yaml | |
| 7 | + | services: | |
| 8 | + | mon-service: | |
| 9 | + | image: mon-image:latest | |
| 10 | + | # RAM | |
| 11 | + | mem_limit: 512M # Maximum | |
| 12 | + | mem_reservation: 256M # Minimum garanti | |
| 13 | + | memswap_limit: 1G # RAM + swap | |
| 14 | + | # CPU | |
| 15 | + | cpus: '0.5' # 50% d'un cœur | |
| 16 | + | ``` | |
| 17 | + | ||
| 18 | + | ### **2. Pour Docker Swarm** | |
| 19 | + | ```yaml | |
| 20 | + | services: | |
| 21 | + | mon-service: | |
| 22 | + | image: mon-image:latest | |
| 23 | + | deploy: | |
| 24 | + | resources: | |
| 25 | + | limits: | |
| 26 | + | cpus: '0.5' | |
| 27 | + | memory: 512M | |
| 28 | + | reservations: | |
| 29 | + | cpus: '0.25' | |
| 30 | + | memory: 256M | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | --- | |
| 34 | + | ||
| 35 | + | ## ⚙️ **Options détaillées** | |
| 36 | + | ||
| 37 | + | ### **Mémoire (RAM)** | |
| 38 | + | | Option | Description | Exemple | | |
| 39 | + | |--------|-------------|---------| | |
| 40 | + | | `mem_limit` | Limite maximale de RAM | `512M`, `2G` | | |
| 41 | + | | `mem_reservation` | Minimum garanti | `256M`, `1G` | | |
| 42 | + | | `memswap_limit` | RAM + swap totale | `1G`, `-1` (désactive swap) | | |
| 43 | + | ||
| 44 | + | ### **CPU** | |
| 45 | + | | Option | Description | Exemple | | |
| 46 | + | |--------|-------------|---------| | |
| 47 | + | | `cpus` | Nombre/part de CPU | `'1.5'`, `'0.5'` | | |
| 48 | + | | `cpu_period` + `cpu_quota` | Contrôle fin | `period: 100000`, `quota: 50000` (50%) | | |
| 49 | + | | `cpuset` | Affinité CPU | `'0-1'`, `'0,2'` | | |
| 50 | + | | `cpu_shares` | Poids relatif | `512` (50% poids normal) | | |
| 51 | + | ||
| 52 | + | --- | |
| 53 | + | ||
| 54 | + | ## 💡 **Exemple Complet** | |
| 55 | + | ```yaml | |
| 56 | + | version: '3.8' | |
| 57 | + | services: | |
| 58 | + | app: | |
| 59 | + | image: nginx:alpine | |
| 60 | + | container_name: mon-app | |
| 61 | + | ||
| 62 | + | # RAM | |
| 63 | + | mem_limit: 1G | |
| 64 | + | mem_reservation: 512M | |
| 65 | + | memswap_limit: 1.5G | |
| 66 | + | ||
| 67 | + | # CPU | |
| 68 | + | cpus: '0.75' | |
| 69 | + | cpu_shares: 768 | |
| 70 | + | cpu_period: 100000 | |
| 71 | + | cpu_quota: 75000 | |
| 72 | + | ||
| 73 | + | # Optimisations | |
| 74 | + | restart: unless-stopped | |
| 75 | + | healthcheck: | |
| 76 | + | test: ["CMD", "curl", "-f", "http://localhost"] | |
| 77 | + | interval: 30s | |
| 78 | + | timeout: 10s | |
| 79 | + | retries: 3 | |
| 80 | + | ``` | |
| 81 | + | ||
| 82 | + | --- | |
| 83 | + | ||
| 84 | + | ## 🔧 **Formules Utiles** | |
| 85 | + | ||
| 86 | + | ### **Calcul CPU Quota** | |
| 87 | + | ``` | |
| 88 | + | CPU % = (cpu_quota / cpu_period) * 100 | |
| 89 | + | ``` | |
| 90 | + | Exemple : `quota: 50000` ÷ `period: 100000` = **50% CPU** | |
| 91 | + | ||
| 92 | + | ### **Unités de mémoire** | |
| 93 | + | - `B` = bytes | |
| 94 | + | - `K` = kilobytes (1000 bytes) | |
| 95 | + | - `M` = megabytes | |
| 96 | + | - `G` = gigabytes | |
| 97 | + | ||
| 98 | + | --- | |
| 99 | + | ||
| 100 | + | ## 🛠️ **Commandes de vérification** | |
| 101 | + | ||
| 102 | + | ```bash | |
| 103 | + | # Voir l'utilisation en temps réel | |
| 104 | + | docker stats | |
| 105 | + | ||
| 106 | + | # Voir les limites d'un conteneur | |
| 107 | + | docker inspect <container_id> | grep -i "memory\|cpu" | |
| 108 | + | ||
| 109 | + | # Tester les limites | |
| 110 | + | docker run --rm -it --cpus="0.5" --memory="512m" alpine:latest | |
| 111 | + | ``` | |
| 112 | + | ||
| 113 | + | --- | |
| 114 | + | ||
| 115 | + | ## ⚠️ **Bonnes pratiques** | |
| 116 | + | ||
| 117 | + | 1. **Toujours définir des limites** pour éviter la surconsommation | |
| 118 | + | 2. **Reservation < Limit** pour éviter les conflits | |
| 119 | + | 3. **Adapter au hardware** : ne pas allouer plus que la machine n'a | |
| 120 | + | 4. **Surveiller l'utilisation** avec `docker stats` | |
| 121 | + | 5. **Penser au swap** : `memswap_limit` contrôle RAM + swap totale | |
| 122 | + | ||
| 123 | + | --- | |
| 124 | + | ||
| 125 | + | ## 🎯 **Récapitulatif rapide** | |
| 126 | + | ||
| 127 | + | | Besoin | Configuration | | |
| 128 | + | |--------|--------------| | |
| 129 | + | | **Limiter à 50% CPU** | `cpus: '0.5'` | | |
| 130 | + | | **Max 1GB RAM** | `mem_limit: 1G` | | |
| 131 | + | | **Garantir 256MB** | `mem_reservation: 256M` | | |
| 132 | + | | **Désactiver swap** | `memswap_limit: -1` | | |
| 133 | + | | **2 cœurs max** | `cpuset: '0-1'` | | |
| 134 | + | ||
| 135 | + | --- | |
| 136 | + | ||
| 137 | + | ## 📚 **Ressources** | |
| 138 | + | - [Documentation officielle Docker](https://docs.docker.com/compose/compose-file/compose-file-v3/#resources) | |
| 139 | + | - [Best practices - Resource limits](https://docs.docker.com/config/containers/resource_constraints/) | |
| 140 | + | - [Calculateur CPU quota](https://docs.docker.com/config/containers/resource_constraints/#cpu) | |
| 141 | + | ||
| 142 | + | --- | |
| 143 | + | ||
| 144 | + | **📝 À copier/coller dans votre fichier `docker-compose.yml`** | |
Daha yeni
Daha eski