Instalação do Keepalived, responsável pelo endereço Ip Virtual que receberá as requisições dos clientes:
1- Instalar o kernel-devel referente ao seu kernel em uso
2- Faça o download da versão mais atual do Keepalived em http://www.keepalived.org/download.html e copie na pasta /usr/src.
cd /usr/src
tar xzpvf keepalived-*.*.**.tar.gz
cd keepalived-*.*.**
./configure; make
make install
ln -s /usr/local/sbin/keepalived /usr/sbin/keepalived
ln -s /usr/local/etc/sysconfig/keepalived /etc/sysconfig/keepalived
ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
3- Conteúdo dos arquivos de configuração e script de inicialização do keepalived:
/etc/keepalived/keepalived.conf
vrrp_script chk_haproxy { # Requires keepalived-1.1.13
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}
global_defs {
notification_email {
admin@dominio.com
}
smtp_server 127.0.0.1
lvs_id ServerA
}
vrrp_instance virtual {
interface eth0
state MASTER
virtual_router_id 39
priority 110
virtual_ipaddress {
10.1.50.36 #Endereço IP Virtual
}
track_script {
chk_haproxy
}
}
/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D"
/etc/init.d/keepalived
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon keepalived --oknodo ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
reload() {
echo -n $"Reloading $prog: "
killproc keepalived -1
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
exit 1
esac
exit $RETVAL
4- Ajuste de inicialização dos serviços nos respectivos Runlevels:
chkconfig --add keepalived
chkconfig --level 2345 keepalived on
OK. Keepalived Instalado e configurado corretamente.
Agora partiremos para a instalação do HAProxy e alguns ajustes para que o mesmo funcione na mesma porta em que o apache está rodando, no caso de estarem rodando na mesma máquina:
1- Faça o download do pacote RPM do HAProxy em http://rpm.pbone.net/index.php3/stat/4/idpl/6806415/com/haproxy-1.3.14.3-1.i586.rpm.html e instale na máquina:
rpm -hiv haproxy-*.*.**.rpm2- Conteúdo do arquivo de configuração do HAProxy:
/etc/haproxy/haproxy.cfg
# haproxy.cfg file example3- Ajuste do syslog para logging do HAProxy:
global
log 127.0.0.1 local0 err
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch #redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webfarm 10.1.50.36:80
mode http
stats enable
stats auth someuser:somepassword
balance roundrobin
cookie JSESSIONID
option httpclose
option forwardfor
option httpchk HEAD /server-status
server webA 10.1.50.38:80 cookie A check
server webB 10.1.50.39:80 cookie B check
/etc/sysconfig/syslog
SYSLOGD_OPTIONS="-m 0 -r"
/etc/syslog.conf
# Log HaproxyOK. HAProxy Instalado e configurado corretamente.
local0.* /var/log/haproxy.log
local1.* /var/log/haproxy.log
Falta agora ajustar o apache para escutar apenas no seu endereço ip principal, modificando apenas a linha abaixo no arquivo de configuração principal do Apache2:
Onde está:
Listen 80
Modificar para:
Listen 10.1.50.38:80Para acessar a página de estatísticas do HAProxy basta acessar o link http://endereço_ip_virtual/haproxy?stats
Pronto! Seus servidores estão configurados com os serviços de Keepalived, HAProxy, servindo páginas do Apache2 e sendo balanceados pelo HAProxy utilizando o endereço ip virtual do serviço Keepalived.
Enjoy.