User Tools

Site Tools


informatica:linux:haproxy

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
informatica:linux:haproxy [2012/06/20 14:34] javiinformatica:linux:haproxy [2012/12/20 07:15] javi
Line 2: Line 2:
  
 balancer load balancer high availability reverse proxy balancer load balancer high availability reverse proxy
 +
 +http://code.google.com/p/haproxy-docs/wiki/source
 +
  
 ===== Instalacion ===== ===== Instalacion =====
 +
 +==== Paquete debian ====
  
   sudo aptitude update; sudo aptitude install haproxy   sudo aptitude update; sudo aptitude install haproxy
  
 +
 +
 +==== Fuentes ====
 +
 +Importante para obtener soporte SSL
 +
 +1. (Opcional) Instalar dependencias:
 +
 +  sudo aptitude update; sudo aptitude install build-essential libpcre3-dev libssl-dev
 +
 +2. Descargar:
 +
 +  cd /usr/local; sudo wget -c --tries=0 http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev15.tar.gz
 +
 +3. Descomprimir
 +
 +  sudo tar xvfz haproxy-1.5-dev15.tar.gz; sudo ln -s haproxy-1.5-dev15 haproxy
 +
 +4. Compilar:
 +
 +  cd /usr/local/haproxy; make TARGET=linux2628 USE_STATIC_PCRE=1 USE_OPENSSL=1 
 +  sudo make install
  
 ===== Config ===== ===== Config =====
 +
 +http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
  
 En este ejemplo: En este ejemplo:
  
-  | 10.0.0.15 | Balanceador de carga (haproxy) | +  | 10.0.0.15 | Balanceador de carga (haproxy) 
-  | 10.0.0.4 | Servidor web 1 | +  | 10.0.0.4 | Servidor web 1 | Escucha en TCP 666 
-  | 10.0.0.5 | Servidor web 2 |+  | 10.0.0.5 | Servidor web 2 | Escucha en TCP 666 |
  
   sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old   sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old
Line 52: Line 81:
         server webA 10.0.0.5:80 cookie B check         server webA 10.0.0.5:80 cookie B check
  
 +listen mysql balancer.dev.jj.com:3306
 +        mode tcp
 +        balance roundrobin
 + timeout connect 50000ms
 + timeout client 500000ms
 + timeout server 500000ms
 + # Requires 'usuario' to be granted on mysql backend
 + option mysql-check usuario
 + log global
 + server mysql-1.dev.jj.com mysql-2.dev.jj.com:3306
 + server mysql-2.dev.jj.com mysql-3.dev.jj.com:3306
  
 listen test 10.0.0.15:666 listen test 10.0.0.15:666
Line 61: Line 101:
  
 Ejemplo TCP (da un warning si mantenemos  Ejemplo TCP (da un warning si mantenemos 
 +
  
 ===== Servicio ===== ===== Servicio =====
Line 69: Line 110:
  
   wget -O - http://10.0.0.15   wget -O - http://10.0.0.15
 +
 +
 +===== Logging =====
 +
 +http://kevin.vanzonneveld.net/techblog/article/haproxy_logging/
 +
 +1. Crear una entrada en rsyslog para haproxy
 +
 +  sudo vim /etc/rsyslog.d/haproxy.conf
 +
 +Con el siguiente contenido:
 +
 +<code>
 +$ModLoad imudp
 +$UDPServerRun 514
 +$UDPServerAddress 127.0.0.1
 + 
 +# ..and in any case, put these two in /etc/rsyslog.d/haproxy.conf:
 +local1.* -/var/log/haproxy_1.log
 +
 +& ~ 
 +# & ~ means not to put what matched in the above line anywhere else for the rest of the rules
 +# http://serverfault.com/questions/214312/how-to-keep-haproxy-log-messages-out-of-var-log-syslog
 +</code>
 +
 +2. Reiniciar rsyslogd:
 +
 +  sudo /etc/init.d/rsyslog restart
 +
 +3. Editar:
 +
 +  sudo vim /etc/haproxy/haproxy.cfg
 +
 +Y dejar la siguiente linea dentro de la seccion 'global':
 +
 +<code>
 +global
 +        log             127.0.0.1       local1 debug
 +</code>
 +
 +4. Reiniciar haproxy y ver el log:
 +
 +<code>
 +sudo /etc/init.d/haproxy restart
 +sudo tail -F /var/log/haproxy_1.log
 +</code>
 +
 +5. (Opcional) rotar logs:
 +
 +<code>
 +/etc/logrotate.d/haproxy
 +
 +/var/log/haproxy*.log
 +{
 +    rotate 4
 +    weekly
 +    missingok
 +    notifempty
 +    compress
 +    delaycompress
 +    sharedscripts
 +    postrotate
 +        reload rsyslog >/dev/null 2>&1 || true
 +    endscript
 +}
 +</code>
 +
 +
 +
 +===== HTTP + Apache logging IP cliente =====
 +
 +1. En el balanceador de carga (haproxy) anyadir la directiva 'option forwardfor':
 +
 +<code>
 +
 +/etc/haproxy/haproxy.cfg
 +
 +...
 +listen www balancer.dev.jj.com:8000
 +        mode http
 +        balance roundrobin
 +        server www-1.dev.jj.com www-1.dev.jj.com:80
 +        server www-2.dev.jj.com www-2.dev.jj.com:80
 +        option httplog
 +        option logasap
 +        option forwardfor
 +        log global
 +...
 +</code>
 +
 +2. En el backend (www-1.dev.jj.com, en este caso Apache):
 +
 +<code>
 +/etc/apache2/apache2.conf
 +
 +...
 +LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
 +...
 +
 +</code>
 +
 +
 +===== Estadisticas =====
 +
 +1. Habilitar estadisticas. TODO: revisar config y hacer lo mismo a traves de peticiones HTTP en lugar de socket
 +
 +  sudo vim /etc/haproxy/haproxy.cfg
 +
 +Y anyadir/editar la siguiente linea:
 +
 +<code>
 +global
 +        stats socket /tmp/haproxy_stats level admin
 +</code>
 +
 +2. Reiniciar haproxy
 +
 +  sudo /etc/init.d/haproxy restart
 +
 +3. Instalar socat
 +
 +  sudo aptitude update; sudo aptitude install socat
 +
 +4. Convertirse en root y probar comandos:
 +
 +  clear;echo "show stat" | socat unix-connect:/tmp/haproxy_stats stdio
 +
 +Listado completo de comandos en la seccion '9.2. Unix Socket commands' de http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
 +
 +==== Deshabilitar/habilitar un nodo ====
 +
 +Requiere tener las estadisticas habilitadas y ser root.
 +
 +**Deshabilitar**:
 +
 +  clear;echo "disable server mysql/mysql-3.dev.jj.com" | socat unix-connect:/tmp/haproxy_stats stdio
 +
 +**Habilitar**:
 +
 +  clear;echo "enable server mysql/mysql-3.dev.jj.com" | socat unix-connect:/tmp/haproxy_stats stdio
 +
informatica/linux/haproxy.txt · Last modified: 2015/04/13 20:19 by 127.0.0.1