Apprenez à structurer un arbre cgroups v2 et appliquer des limites raisonnables par service.
objectifs d’apprentissage
- Créer hiérarchie cgroups.
- Limiter CPU/mémoire/I/O.
- Observer compteurs.
prérequis
- Kernel cgroups v2.
- Accès root.
notions clés
- cpu.max, memory.max.
- io.max.
- systemd.slice.
démonstration guidée
étape 1
Créer une slice dédiée sous systemd et y démarrer un service limité.
sudo systemd-run --unit=demo.slice --slice=work.slice --property=CPUQuota=50% --property=MemoryMax=500M sleep 300
systemctl status demo.slice || true
étape 2
Configurer manuellement des limites cgroups v2 (sans systemd).
# mkdir -p /sys/fs/cgroup/demo
# echo max > /sys/fs/cgroup/demo/memory.high
# echo 300M > /sys/fs/cgroup/demo/memory.max
# echo $$ > /sys/fs/cgroup/demo/cgroup.procs
exercice
Lancez un processus CPU-bound et limitez-le à 30% avec systemd-run.
correction
Quota CPU via CPUQuota.
sudo systemd-run --property=CPUQuota=30% --unit=cpu30.slice yes > /dev/null & sleep 1; systemctl status cpu30.slice || true
quiz éclair
- Quel fichier cgroups v2 fixe un plafond mémoire dur ?
- a) memory.max
- b) memory.high
- c) mem.limit