Haute disponibilité
Table des matières
- Concepts de haute disponibilité
- Load balancing
- Clustering avec Keepalived
- Pacemaker et Corosync
- Réplication de données
- Exercices pratiques
1 - Concepts de haute disponibilité
Qu'est-ce que la HA ?
SPOF = Single Point of Failure (point de défaillance unique)
Niveaux de disponibilité
| Niveau | Downtime/an | Architecture |
|---|---|---|
| 99% | 3.65 jours | Backup + restore |
| 99.9% | 8.76 heures | Redondance active/passive |
| 99.99% | 52.6 minutes | Cluster actif/actif |
| 99.999% | 5.26 minutes | Géo-distribution |
Patterns de HA
| Pattern | Description |
|---|---|
| Active/Passive | Un seul nœud actif, failover manuel ou auto |
| Active/Active | Tous les nœuds actifs, load balancing |
| N+1 | N serveurs actifs + 1 spare |
| N+M | N actifs + M spares |
Composants d'une solution HA
🔝 Retour à la table des matières
2 - Load balancing
HAProxy - Configuration de base
# /etc/haproxy/haproxy.cfg
global
daemon
maxconn 4096
defaults
mode http
timeout connect 5s
timeout client 50s
timeout server 50s
option httplog
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /health
server web1 192.168.1.10:80 check
server web2 192.168.1.11:80 check
server web3 192.168.1.12:80 check backup
Algorithmes de load balancing
| Algorithme | Description |
|---|---|
roundrobin | Distribution séquentielle |
leastconn | Vers le serveur avec moins de connexions |
source | Sticky par IP source (hash) |
uri | Hash basé sur l'URI |
Health checks
backend http_back
option httpchk GET /health HTTP/1.1\r\nHost:\ localhost
http-check expect status 200
server web1 192.168.1.10:80 check inter 3s fall 3 rise 2
# inter: intervalle entre checks
# fall: nombre d'échecs avant down
# rise: nombre de succès avant up
Stats HAProxy
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:password
🔝 Retour à la table des matières
3 - Clustering avec Keepalived
Architecture VRRP
Installation
apt install keepalived
Configuration Master
# /etc/keepalived/keepalived.conf (Master)
global_defs {
router_id LB_MASTER
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass secret123
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_haproxy
}
}
Configuration Backup
# /etc/keepalived/keepalived.conf (Backup)
global_defs {
router_id LB_BACKUP
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass secret123
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_haproxy
}
}
Scripts de notification
# /etc/keepalived/notify.sh
#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
case $STATE in
"MASTER")
echo "$(date) Becoming MASTER" >> /var/log/keepalived-state.log
# Actions: démarrer services, etc.
;;
"BACKUP")
echo "$(date) Becoming BACKUP" >> /var/log/keepalived-state.log
;;
"FAULT")
echo "$(date) FAULT state" >> /var/log/keepalived-state.log
;;
esac
# Dans keepalived.conf
vrrp_instance VI_1 {
...
notify /etc/keepalived/notify.sh
}
🔝 Retour à la table des matières
4 - Pacemaker et Corosync
Architecture
Installation
apt install pacemaker corosync pcs
systemctl enable --now pcsd
# Définir mot de passe hacluster
passwd hacluster
Configuration initiale
# Sur tous les nœuds, authentifier
pcs host auth node1 node2
# Créer le cluster
pcs cluster setup ha-cluster node1 node2
# Démarrer
pcs cluster start --all
pcs cluster enable --all
# Vérifier
pcs status
Créer des ressources
# VIP
pcs resource create VirtualIP ocf:heartbeat:IPaddr2 \
ip=192.168.1.100 cidr_netmask=24 \
op monitor interval=30s
# Service
pcs resource create WebServer systemd:nginx \
op monitor interval=30s
# Groupe (ressources ensemble)
pcs resource group add WebGroup VirtualIP WebServer
Contraintes
# Colocation (ressources ensemble)
pcs constraint colocation add WebServer with VirtualIP INFINITY
# Ordre (VIP avant WebServer)
pcs constraint order VirtualIP then WebServer
# Localisation (préférence de nœud)
pcs constraint location WebServer prefers node1=100
Gestion du cluster
# Statut
pcs status
crm_mon -1
# Mettre en maintenance
pcs node standby node1
pcs node unstandby node1
# Déplacer une ressource
pcs resource move VirtualIP node2
pcs resource clear VirtualIP
# Logs
journalctl -u pacemaker
journalctl -u corosync
🔝 Retour à la table des matières
5 - Réplication de données
DRBD - Block device réplication
# Installation
apt install drbd-utils
# /etc/drbd.d/data.res
resource data {
protocol C;
on node1 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.10:7789;
meta-disk internal;
}
on node2 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.11:7789;
meta-disk internal;
}
}
# Initialiser
drbdadm create-md data
drbdadm up data
drbdadm primary --force data # Sur le primaire
# Statut
drbdadm status
cat /proc/drbd
GlusterFS - Système de fichiers distribué
# Installation
apt install glusterfs-server
# Créer le cluster
gluster peer probe node2
gluster pool list
# Créer un volume répliqué
gluster volume create gv0 replica 2 \
node1:/data/brick1 \
node2:/data/brick1
gluster volume start gv0
# Monter
mount -t glusterfs node1:/gv0 /mnt/gluster
Réplication PostgreSQL
# postgresql.conf (Primary)
wal_level = replica
max_wal_senders = 3
wal_keep_size = 64MB
# pg_hba.conf
host replication replicator 192.168.1.0/24 md5
# Sur le replica
pg_basebackup -h primary -D /var/lib/postgresql/14/main -U replicator -P
# postgresql.conf (Replica)
primary_conninfo = 'host=primary user=replicator password=secret'