1.          安装keepalived所需要的依赖包

yum -y install popt-devel openssl-devel

 

2.         下载安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.i686/

make &&make install

 

3.         设定keepalived配置文件和启动文件,监控文件

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/sbin/keepalived /sbin/

 

vi /etc/keepalived/keepalived.conf

 

vrrp_script chk_http_port {

script "/etc/keepalived/check_haproxy.sh"

interval 2

weight 2

 

global_defs {

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER           #备机上改为BAKCUP

interface eth0

virtual_router_id 51

priority 150             #备机上优先级改为低于150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

 

track_script {

chk_http_port

}

 

virtual_ipaddress {

192.168.148.200

}

}

}

 

 

设定监控文件

vi /etc/keepalived/check_haproxy.sh

#!/bin/bash

A=`ps -C haproxy --no-header |wc -l`

if [ $A -eq 0 ];then

/etc/init.d/haproxy restart

sleep 3

if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then

/etc/init.d/keepalived stop

fi

fi

 

4.         启动keepalived服务并查看虚拟IP

##启动

/etc/init.d/keepalived start

##查看配置文件中的虚拟IP

ip add list

2: eth0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:2b:59:50 brd ff:ff:ff:ff:ff:ff

    inet 192.168.148.100/24 brd 192.168.148.255 scope global eth0

    inet 192.168.148.200/32 scope global eth0

    inet6 fe80::20c:29ff:fe2b:5950/64 scope link

       valid_lft forever preferred_lft forever

 

5.         在备机部署haproxy+keepalived

在主机上打包并复制到备机:

tar zcvf haproxy.tgz haproxy/

tar zcvf keepalived.tgz keepalived/

tar zcvfP /etc/keepalived.tgz /etc/keepalived/

scp /etc/keepalived.tgz test2:/etc/

scp haproxy.tgz keepalived.tgz test2:/usr/local/

scp /etc/init.d/haproxy test2:/etc/init.d/

在备机上解压缩并复制一些启动文件

tar zxvfP keepalived.tgz -C /etc/

tar zxvf keepalived.tgz -C /usr/local/

tar zxvf haproxy.tgz -C /usr/local/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/sbin/keepalived /sbin/

 

 

6.         在备机修改keepalived配置文件并启动

vi /etc/keepalived/keepalived.conf

state BACKUP           #备机上改为BAKCUP

priority 120           #备机上优先级改为120

 

/etc/init.d/keepalived start (由于存在check_haproxy.sh,haproxy会自动启动)

 

7.         查看主备机器的VRRP通信状态是否正常

 查看var/log/messages,如果看到都是MASTER STATUS,或者ip add list都可以看到VIP,基本上可以说明VRRP通信有问题,

Nov  6 10:28:10 test1 Keepalived_vrrp[7941]: VRRP_Instance(VI_1) setting protocol VIPs.

Nov  6 10:28:10 test1 Keepalived_vrrp[7941]: VRRP_Instance(VI_1) Entering MASTER STATE

Nov  6 10:28:09 test1 Keepalived_vrrp[7941]: VRRP_Instance(VI_1) Transition to MASTER STATE

 

可以用tcpdump ni eth0 vrrp,看到主备2台服务器都在发VRRP包,正常情况下只有MASTER发送VRRP

[root@test1 local]# tcpdump -nni eth0 vrrp

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

10:30:13.455292 IP 192.168.148.100 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 152, authtype simple, intvl 1s, length 20

10:30:14.456310 IP 192.168.148.100 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 152, authtype simple, intvl 1s, length 20

10:30:15.457330 IP 192.168.148.100 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 152, authtype simple, intvl 1s, length 20

 

检查iptables防火墙,需要在2台服务器上都加入以下2条:

MASTER:

-A INPUT -s 192.168.148.110 -j ACCEPT

BACKUP:

-A INPUT -s 192.168.148.100 -j ACCEPT

 

8.         进行主备机的HA切换测试

 MASTER上停止keepalived服务

/etc/init.d/keepalived stop

 

查看BAKCUP /var/log/messages,是否切换到MASTER

Nov  6 10:37:27 test2 Keepalived_vrrp[3824]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.148.200

Nov  6 10:37:22 test2 Keepalived_healthcheckers[3823]: Netlink reflector reports IP 192.168.148.200 added

Nov  6 10:37:22 test2 Keepalived_vrrp[3824]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.148.200

Nov  6 10:37:22 test2 Keepalived_vrrp[3824]: VRRP_Instance(VI_1) setting protocol VIPs.

Nov  6 10:37:22 test2 Keepalived_vrrp[3824]: VRRP_Instance(VI_1) Entering MASTER STATE

Nov  6 10:37:21 test2 Keepalived_vrrp[3824]: VRRP_Instance(VI_1) Transition to MASTER STATE

 

查看虚拟IP是否启动

ip add list

2: eth1: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:a6:8a:61 brd ff:ff:ff:ff:ff:ff

    inet 192.168.148.110/24 brd 192.168.148.255 scope global eth1

    inet 192.168.148.200/32 scope global eth1

    inet6 fe80::20c:29ff:fea6:8a61/64 scope link

       valid_lft forever preferred_lft forever

 

以上说明切换正常,HAproxy可以在备机正常使用

由于keepalived默认是抢占模式,MASTER恢复后会进行再次切换,可以在配置文件里设置

nopreempt 来禁用这个功能

 

 

 

 

参考资料

http://www.cnblogs.com/dkblog/archive/2011/07/06/2098949.html