haproxy+keepalived实现高可用负载均衡(实例配置)

2019-10-15 10:25:51刘景俊


! Configuration File for keepalived
global_defs {
   router_id LVA_DEVEL
}
vrrp_script chk_http_port {
   script "/root/check_haproxy.sh"
   interval 2
   weight  2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
track_script {
    chk_http_port
}
    virtual_ipaddress {
        192.168.2.100
    }
}

调用脚本check_haproxy.sh内容:

#!/bin/bash
A=`ip a | grep 192.168.2.100 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep | awk '{print $2}'`
if [ $A -gt 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
else
kill -9 $B
fi

5、两台负载机器我就不多介绍了,用的是系统自带的apache
登录192.168.5.54上操作:
echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
登录192.168.5.57上操作:
echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start

6、测试步骤

启动MASTER上的keepalived服务,再启动BACKUP上的keepalived服务。
确定MASTER上是否有192.168.2.100地址,用ip a查看即可!
然后手动杀掉MASTER上的haproxy进程,看看是否能马上恢复进程?
停止MASTER上的keepalived服务,确认BACKUP是否接管VIP地址?
最后再启动MASTER上的keepalived服务,再确认MASTER是否再接管VIP地址?
总结:我这测试都没问题,如果有问题请给我留言!

原文:http://blog.chinaunix.net/uid-23916356-id-3387261.html