HAProxy est un load balancer et proxy server open source conçu pour la haute disponibilité, la répartition de charge et le proxy pour les applications TCP et HTTP.
⚙️ Gestion du service
📌 Action
🧠 Commande
▶️ Démarrer HAProxy
sudo systemctl start haproxy
⏹️ Arrêter HAProxy
sudo systemctl stop haproxy
🔁 Redémarrer HAProxy
sudo systemctl restart haproxy
🔄 Recharger la config
sudo systemctl reload haproxy
📊 Statut du service
sudo systemctl status haproxy
🚀 Activer au démarrage
sudo systemctl enable haproxy
✅ Tester la configuration
sudo haproxy -f /etc/haproxy/haproxy.cfg -c
📋 Version d’HAProxy
haproxy -v
📂 Configuration
📌 Fichier/Dossier
🧠 Description
/etc/haproxy/haproxy.cfg
Configuration principale
/var/log/haproxy.log
Logs HAProxy
/var/lib/haproxy/stats
Socket de statistiques
/etc/default/haproxy
Configuration du service
/etc/rsyslog.conf
Configuration des logs système
🏗️ Structure de configuration
Sections principales
📌 Section
🧠 Description
📊 Utilisation
global
Configuration globale
Processus, logs, sécurité
defaults
Paramètres par défaut
Timeouts, mode, options
frontend
Point d’entrée des requêtes
Écoute, routage
backend
Serveurs de destination
Load balancing, santé
listen
Frontend + Backend combinés
Configuration simple
📝 Configuration basique
Configuration HTTP simple
global daemon user haproxy group haproxy chroot /var/lib/haproxy stats socket /var/run/haproxy.sock mode 660defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50000msfrontend web_frontend bind *:80 default_backend web_serversbackend web_servers balance roundrobin server web1 192.168.1.10:8080 check server web2 192.168.1.11:8080 check server web3 192.168.1.12:8080 check
Configuration HTTPS avec SSL
frontend https_frontend bind *:443 ssl crt /etc/ssl/certs/example.pem redirect scheme https if !{ ssl_fc } default_backend web_serversbackend web_servers balance roundrobin option httpchk GET /health server web1 192.168.1.10:8080 check server web2 192.168.1.11:8080 check backup
⚖️ Algorithmes de load balancing
📌 Algorithme
🧠 Description
📊 Configuration
roundrobin
Rotation circulaire
balance roundrobin
leastconn
Moins de connexions
balance leastconn
source
Hash sur IP source
balance source
uri
Hash sur URI
balance uri
url_param
Hash sur paramètre URL
balance url_param id
random
Sélection aléatoire
balance random
first
Premier serveur disponible
balance first
Exemples de configuration
# Load balancing par IP source (session sticky)backend api_servers balance source hash-type consistent server api1 10.0.1.10:3000 check server api2 10.0.1.11:3000 check# Load balancing par URIbackend static_servers balance uri whole server static1 10.0.2.10:80 check server static2 10.0.2.11:80 check
🔍 Health checks et monitoring
📌 Check Type
🧠 Configuration
📊 Exemple
TCP simple
check
server web1 ip:port check
HTTP GET
option httpchk GET /path
option httpchk GET /health
HTTP avec headers
option httpchk GET /path HTTP/1.1\r\nHost:\ domain
Headers personnalisés
Interval check
check inter 5s
Vérif toutes les 5s
Retry count
check rise 2 fall 3
2 OK pour UP, 3 KO pour DOWN
Configuration avancée des checks
backend app_servers option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.example.com server app1 10.0.1.10:3000 check inter 10s rise 2 fall 3 server app2 10.0.1.11:3000 check inter 10s rise 2 fall 3 server app3 10.0.1.12:3000 check inter 10s rise 2 fall 3 backup
📊 Interface de statistiques
Configuration des stats
# Dans le frontend ou comme section listenlisten stats bind *:8404 stats enable stats uri /stats stats refresh 30s stats admin if TRUE stats auth admin:password123
Accès aux statistiques
📌 Action
🧠 URL/Commande
🌐 Interface web
http://server:8404/stats
📊 Statistiques JSON
http://server:8404/stats?stats;json
❌ Désactiver un serveur
Web UI ou echo "disable server backend/server1" | socat stdio /var/run/haproxy.sock
✅ Activer un serveur
echo "enable server backend/server1" | socat stdio /var/run/haproxy.sock
global log 127.0.0.1:514 local0 info chroot /var/lib/haproxy user haproxy group haproxydefaults log global option httplog option dontlognull option log-health-checks