实验环境
haproxy keepalived 主:192.168.154.128
haproxy keepalived 备:192.168.154.133
vip:192.168.154.136
web:192.168.154.130:80 192.168.154.131:80
haproxy keepalived 备:192.168.154.133
vip:192.168.154.136
web:192.168.154.130:80 192.168.154.131:80
一.安装haproxy
haproxy的安装请参考https://blog.csdn.net/qq_30256711/article/details/80702128
二.安装配置keepalived
在主备haproxy上安装haproxy后,安装keeplived
tar -zxvf keepalived-1.1.17.tar.gz
#ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux
#cd keepalived-1.1.17
#./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.18-128.el5-i686/
#make && make install
#cd /etc/keepalived/
#mv keepalived.conf keepalived.conf.default
keepalived配置文件如下
! Configuration File for keepalived
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
global_defs {
notification_email {
185604832@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_1 #备haproxy上此处为2
}
notification_email {
185604832@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_1 #备haproxy上此处为2
}
vrrp_instance VI_1 {
state MASTER #备haproxy上此处为BACKUP
interface eth0
virtual_router_id 51
priority 150 #备haproxy上此处为100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
state MASTER #备haproxy上此处为BACKUP
interface eth0
virtual_router_id 51
priority 150 #备haproxy上此处为100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.154.136
}
}
virtual_ipaddress {
192.168.154.136
}
}
}
vi /etc/keepalived/check_haproxy.sh #编写keepalived监控haproxy的脚本,如果haproxy停止运行了,就把haproxy启动,如果haproxy启动不了,就关闭keepalived,放弃资源给备用haproxy
#!/bin/bash
A=`ps -C haproxy --no-header|wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header|wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
A=`ps -C haproxy --no-header|wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header|wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
chmod 755 /etc/keepalived/check_haproxy.sh