diff --git a/HowtoHaproxy.md b/HowtoHaproxy.md index 1ea0b20b..bb21082a 100644 --- a/HowtoHaproxy.md +++ b/HowtoHaproxy.md @@ -3,7 +3,9 @@ title: Howto HAProxy category: web HA --- - +* + +HAProxy est un puissant *load balancer* pour les protocoles TCP/HTTP/HTTPS. Il gère la répartition de charge et la tolérance de panne. Son principal auteur est Willy Tarreau, un développeur actif du noyau Linux. HAProxy est écrit en langage C, il est optimisé pour Linux, mais tourne également sous BSD. Des sites web mondiaux l'utilisent (Twitter, Github, Reddit, Airbnb, etc.)/ ## Installation @@ -11,13 +13,36 @@ category: web HA # apt install haproxy ~~~ -Sous Debian Jessie, la version est la 1.5.8, si nécessaire une version plus récente est disponible via les backports. - ## Configuration -La configuration se passe dans le fichier `haproxy.cfg` se trouvant dans `/etc/haproxy` sous Debian. +La configuration se passe dans le fichier `/etc/haproxy/haproxy.cfg` : -Voici un exemple de configuration : +~~~ +global + log 127.0.0.1 local5 debug +defaults + mode http +listen www + bind *:80 + balance roundrobin + option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www.example.com + stats uri /haproxy-stats + stats auth foo:bar + server www00 192.0.2.1:80 maxconn 50 check inter 10s + server www01 192.0.2.2:80 maxconn 50 check inter 10s +~~~ + +On note l'activation des logs en *debug* ce qui permet de voir **toutes** les requêtes. +Attention, il faut donc que le démon `syslog` ait un paramétrage sur la facilité `local5` (ou autre selon votre configuration). +Pour `rsyslog` cela se fait ainsi dans `rsyslog.conf` : + +~~~ +local5.* -/var/log/haproxy.log +~~~ + +## Configuration avancée + +### Exemple d'une configuration avec frontend/backend HTTP ~~~ global @@ -30,16 +55,6 @@ global group haproxy daemon - # Default SSL material locations - ca-base /etc/ssl/certs - crt-base /etc/ssl/private - - # Default ciphers to use on SSL-enabled listening sockets. - # For more information, see ciphers(1SSL). This list is from: - # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/ - ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS - ssl-default-bind-options no-sslv3 - defaults log global mode http @@ -81,23 +96,21 @@ backend myback server web03 192.0.2.3:80 check observe layer4 weight 100 ~~~ -Pour activer les logs en mode debug, notamment utilisé pour voir **toutes** les requêtes, il faut remplacer notice par debug. +### SSL ~~~ -log /dev/log local5 debug + # Default SSL material locations + ca-base /etc/ssl/certs + crt-base /etc/ssl/private + + # Default ciphers to use on SSL-enabled listening sockets. + # For more information, see ciphers(1SSL). This list is from: + # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/ + ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS + ssl-default-bind-options no-sslv3 ~~~ -Attention, il faut donc que le démon `syslog` ait un paramétrage sur la facilité `local5` (ou autre selon votre configuration). -Pour `rsyslog` cela se fait ainsi dans `rsyslog.conf` : - -~~~ -local5.* -/var/log/haproxy.log -~~~ - -### Exemple de configuration avancée - - * Gestion de différents backend en fonction du domaine - * Association d'un serveur à un utilisateur à l'aide d'un cookie (« sticky session ») +### Exemple avec plusieurs backends et du « sticky session » ~~~ global @@ -135,6 +148,81 @@ backend domain2 server web02 192.0.2.2:80 cookie web02 check ~~~ +### Exemple en mode TCP + +~~~ +listen memcached 127.0.0.1:11211 + option tcp-check + server nosql00 192.0.2.3:11211 check + server nosql01 192.0.2.4:11211 check backup +~~~ + +### Exemple pour MySQL + +~~~ +listen mysql 127.0.0.1:3306 + option httpchk + server sql00 192.0.2.1:3306 check port 8306 + server sql01 192.0.2.2:3306 check port 8306 backup +~~~ + +On note l'option **httpchk** qui va permettre de faire un check en HTTP et vérifier des conditions avancées (réplication OK, etc.) grâce à différents scripts (inspirés de ce [vieux blog post](http://sysbible.org/2008/12/04/having-haproxy-check-mysql-status-through-a-xinetd-script/)). + +`/etc/xinetd.d/mysqlchk` : + +~~~ +service mysqlchk { + flags = REUSE + socket_type = stream + port = 80 + wait = no + user = root + server = /root/mysqlchk + log_on_failure += USERID + disable = no + only_from = 192.0.2.0/27 +} +~~~ + +`/root/mysqlchk` : + +~~~ +MYSQL_HOST="127.0.0.1" +MYSQL_PORT="3306" +MYSQL_USERNAME="mysqlchk" +MYSQL_PASSWORD="PASSWORD" +TMP_FILE="/tmp/mysqlchk.out" +ERR_FILE="/tmp/mysqlchk.err" + +/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME \ + --password=$MYSQL_PASSWORD -e"show databases;" > $TMP_FILE 2> $ERR_FILE + +uptime=`cat /proc/uptime | cut -f 1 -d .` + +if [ "$(/bin/cat $TMP_FILE)" != "" ] && ( /root/nrpe-check-mysql-slave.sh >/dev/null || [ $uptime -gt 3600 ] ); then + # mysql is fine, return http 200 + /bin/echo -e "HTTP/1.1 200 OK\r\n" + /bin/echo -e "Content-Type: Content-Type: text/plain\r\n" + /bin/echo -e "\r\n" + /bin/echo -e "MySQL is running.\r\n" + /bin/echo -e "\r\n" +else + # mysql is fine, return http 503 + /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n" + /bin/echo -e "Content-Type: Content-Type: text/plain\r\n" + /bin/echo -e "\r\n" + /bin/echo -e "MySQL is *down*.\r\n" + /bin/echo -e "\r\n" +fi +~~~ + +`/root/nrpe-check-mysql-slave.sh` : + +~~~ +#!/bin/sh +exec /usr/lib/nagios/plugins/check_mysql --check-slave -u debian-sys-maint -p PASSWORD -w 5 -c 60 +~~~ + ### HTTP basic authentication Pour mettre en place une authentification HTTP basique au niveau d'HAProxy, définir dans la section globale une liste d'utilisateur, soit avec un mot de passe en clair soit avec un mot de passe chiffré :