Documentation HAProxy web1 et web2 avec MySQL
Haute disponibilité
Objectif : Serveur disponible à tout moment « ou presque ».
PCA : plan de continuité d'activité.
Infrastructure virtuelle
3 machines virtuelles.
Machine HAProxy
- Username : vboxuser
- Password : changeme
Nom de la machine
La nommer haproxyweb.
Ne pas installer LAMP sur cette machine.
Configuration réseau HAProxy (Netplan)
ls /etc/netplan
sudo nano /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens18:
dhcp4: false
dhcp6: false
addresses: [172.17.4.12/24] #IP haproxyweb réseau internet
routes:
- to: 0.0.0.0/0
via: 172.17.4.254 #passerelle
metric: 100
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
ens19:
addresses: [172.17.4.13/24] #IP haproxyweb réseau interne
dhcp4: false
dhcp6: false
sudo netplan try
sudo netplan apply
Installation et configuration de HAProxy
Modifier le fichier de configuration :
sudo nano /etc/haproxy/haproxy.cfg
Ajouter les lignes suivantes :
frontend http_front
bind *:80
stats uri /stats
default_backend http_back
backend http_back
balance leastconn
cookie SERVERID insert indirect nocache
server web1 172.17.4.14:80 cookie web1 check
server web2 172.17.4.15:80 cookie web2 check
Enregistrer avec Ctrl+X, puis Y, puis Entrée.
Tests de connectivité depuis HAProxy
ping 172.17.4.14
ping 172.17.4.15
ping google.com
Machine web1
- Username : vboxuser
- Password : changeme
Nom de la machine
Mettre : web1.
Installation du serveur LAMP
sudo apt install lamp-server^
Configuration réseau web1 (Netplan)
ls /etc/netplan
sudo nano /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens18:
addresses: [172.17.4.14/24]
dhcp4: false
dhcp6: false
sudo netplan try
sudo netplan apply
Modification de la page d'accueil web1
sudo nano /var/www/html/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2022-03-22
See: https://launchpad.net/bugs/1966004
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Bienvenue</title>
</head>
<body>
Bienvenue sur web1
</body>
<style type="text/css" media="screen">
* {
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
body, html {
padding: 3px 3px 3px 3px;
background-color: #D8DBE2;
font-family: Ubuntu, Verdana, sans-serif;
font-size: 11pt;
text-align: center;
}
div.main_page {
position: relative;
display: table;
width: 800px;
margin-bottom: 3px;
margin-left: auto;
margin-right: auto;
padding: 0px 0px 0px 0px;
border-width: 2px;
border-color: #212738;
border-style: solid;
background-color: #FFFFFF;
}
</style>
</html>
Passerelle par défaut web1
sudo nano /etc/netplan/00-installer-config.yaml
sudo chmod 600 /etc/netplan/00-installer-config.yaml
network:
version: 2
ethernets:
ens18:
addresses:
- 172.17.4.14/24
routes:
- to: 0.0.0.0/0
via: 172.17.4.13
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
sudo netplan try
sudo netplan apply
Tests web1
| Commande |
Résultat attendu |
ping 172.17.4.13 |
# ping haproxyweb |
ping 172.17.4.15 |
# ping web2 |
Machine web2
- Username : vboxuser
- Password : changeme
Nom de la machine
Mettre : web2.
Installation du serveur LAMP
sudo apt install lamp-server^
Configuration réseau web2 (Netplan)
ls /etc/netplan
sudo nano /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens18:
addresses: [172.17.4.15/24]
dhcp4: false
dhcp6: false
sudo netplan try
sudo netplan apply
Modification de la page d'accueil web2
sudo nano /var/www/html/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2022-03-22
See: https://launchpad.net/bugs/1966004
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Bienvenue</title>
</head>
<body>
Bienvenue sur web2
</body>
<style type="text/css" media="screen">
* {
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
body, html {
padding: 3px 3px 3px 3px;
background-color: #D8DBE2;
font-family: Ubuntu, Verdana, sans-serif;
font-size: 11pt;
text-align: center;
}
div.main_page {
position: relative;
display: table;
width: 800px;
margin-bottom: 3px;
margin-left: auto;
margin-right: auto;
padding: 0px 0px 0px 0px;
border-width: 2px;
border-color: #212738;
border-style: solid;
background-color: #FFFFFF;
}
</style>
</html>
Passerelle par défaut web2
sudo nano /etc/netplan/00-installer-config.yaml
sudo chmod 600 /etc/netplan/00-installer-config.yaml
network:
version: 2
ethernets:
ens18:
addresses:
- 172.17.4.15/24
routes:
- to: 0.0.0.0/0
via: 172.17.4.13
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
sudo netplan try
sudo netplan apply
Tests web2
| Test : |
ping 172.17.4.13 |
# ping haproxyweb |
ping 172.17.4.14 |
# ping web1 |
Accès à internet pour web1 et web2
Route par défaut sur web1 et web2
sudo ip route add default via 172.17.4.13
ip route
Il doit y avoir :
default via 172.17.4.13 dev ens18
Configuration du routage et du NAT sur HAProxy
Activer le routage IPv4 :
sudo nano /etc/sysctl.conf
Décommenter la ligne :
net.ipv4.ip_forward=1
Configuration du NAT avec iptables
sudo iptables -t nat -L -n -v
Il doit y avoir une règle de type :
MASQUERADE all -- 172.17.4.0/24 anywhere
Sinon, ajouter la règle :
sudo iptables -t nat -A POSTROUTING -s 172.17.4.0/24 -o ens18 -j MASQUERADE
Vérifier la valeur de ip_forward :
sudo sysctl net.ipv4.ip_forward
Il doit être à 1, sinon :
sudo sysctl -w net.ipv4.ip_forward=1
Rendre la configuration iptables persistante :
sudo apt install iptables-persistent
sudo netfilter-persistent save
Test final :
Accès à HAProxy depuis la machine physique
Sur HAProxy, l'interface ens18 est configurée en inet.
Depuis un navigateur de la machine physique :
http://172.17.4.12
http://172.17.4.12/stats
La première URL permet d'accéder au service web équilibré, la seconde à la page de statistiques HAProxy (stats uri /stats).
Note : Utiliser systemd-networkd pour debian car il n'utilise pas netplan
sudo systemctl enable systemd-networkd --now
sudo systemctl enable systemd-resolved --now
sudo nano /etc/systemd/network/10-ethernet.network
[Match]
Name=l'interface
[Network]
Address=IP/masque
Gateway=IP
DNS=1.1.1.1 8.8.8.8
sudo systemctl restart systemd-networkd