Linux下HAProxy+Keepalived双机高可用方案

Keepalived 的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除, 当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的 web服务器。

HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整 合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。

系统环境: CenOS 6.5x86_64 Desktop install 将selinux and iptables 设置为disabled

主要用到软件:
haproxy-1.5.14.tar.gz
keepalived-1.1.17.tar.gz
nginx-1.7.8.tar.gz

图1 为基本的架构图:

Linux下HAProxy+Keepalived双机高可用方案

图2 为IP地址分配。

Linux下HAProxy+Keepalived双机高可用方案

一:安装过程,在两台HA机器上分别keepalived:
#tar -zxvf keepalived-1.2.17.tar.gz
#ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux
#cd keepalived-1.2.17
[root@test25 keepalived-1.2.17]#./configure--with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
[root@test25keepalived-1.2.17]#make
[root@test25keepalived-1.2.17]#make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir/etc/keepalived/
cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp/usr/local/sbin/keepalived /usr/sbin/
 
修改配置文件为:
Master:
#vim/etc/keepalived/ keepalived.conf
# catkeepalived.conf
! ConfigurationFile for keepalived
vrrp_scriptcheck_haproxy {
        script"/etc/keepalived/check_haproxy.sh"
        interval 2
        weight 2
}
global_defs {
            notification_email {
            root@localhost
}
        notification_email_fromkeepalived@server62.example.com
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
        state MASTER
        interface eth0
        virtual_router_id 151
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
virtual_ipaddress{
        192.168.1.201
    }
track_script {
        check_haproxy
    }
}
 
BACKUP:
 
# catkeepalived.conf
 
! ConfigurationFile for keepalived
vrrp_scriptcheck_haproxy {
        script"/etc/keepalived/check_haproxy.sh"
        interval 2
        weight 2
    }
 
global_defs {
        notification_email {
        root@localhost
    }
        notification_email_fromkeepalived@server63.example.com
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
    }
 
vrrp_instanceVI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 151
        priority 50
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
 
virtual_ipaddress{
        192.168.1.201
    }
track_script {
        check_haproxy
    }
}
 

主机上:
#vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy.cfg
echo "haproxy start"
sleep 3
if [ `ps -C haproxy --no-header | wc -l`-eq 0 ];then
/etc/init.d/keepalived stop
echo "keepalived stop"
fi
fi
 
备机上:
#!/bin/bash
A=`ip a | grep 10.2.32.201 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep| awk '{print $2}'`
if [ $A -gt 0 ];then
/usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy.cfg
else
kill -9 $B
fi
 
#两台机器分别执行:chmod 755 /etc/keepalived/check_haproxy.sh

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/21809ecf84cd70eb58ada5b6e37e1247.html