系统版本:centos6.4 64bit
软件版本:keepalived-1.2.2.tar.gz
注意:
1.防火强
Iptables �CF
Service iptables stop
2.Selinux
Setenforce 0
3.依赖包安装
yum install make gcc c++ openssl-devel popt-devel
一、keepalived安装
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure �Cprefix=/usr/local/keepalived
Make && make install
二、配置文件修改
Master配置:
!Configuration File for keepalived
global_defs{
router_id TEST
}
vrrp_scriptcheck_httpd {
script "/root/check_httpd.sh"
interval 2
weight 2
}
vrrp_instanceVI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
# nopreempt
advert_int 1
mcast_src_ip 192.168.30.131
authentication {
auth_type PASS
auth_pass 8888
}
track_script {
check_httpd
}
virtual_ipaddress {
192.168.30.110
}
}
Backup 配置文件
!Configuration File for keepalived
global_defs{
router_id TEST
}
vrrp_scriptcheck_httpd {
script "/root/check_httpd.sh"
interval 2
weight 2
}
vrrp_instanceVI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 88
advert_int 1
mcast_src_ip 192.168.30.132
authentication {
auth_type PASS
auth_pass 8888
}
track_script {
check_httpd
}
virtual_ipaddress {
192.168.30.110
}
}
Check_httpd.sh脚本
#!/bin/bash
PID=`psaux | grep apache | grep -v grep | wc -l`
echo $PID
if [ $PID-eq 0 ]
then
killall keepalived
fi
exit 0
三、测试
Master 和backup 都启动httpd服务
1、master和backup都启动keepalived 日志输出分别为
master:
Oct 18 00:24:32 centos Keepalived: StartingKeepalived v1.2.2 (10/17,2013)
Oct 18 00:24:32 centos Keepalived: StartingVRRP child process, pid=7030
Oct 18 00:24:32 centos Keepalived_vrrp:Registering Kernel netlink reflector
Oct 18 00:24:32 centos Keepalived_vrrp:Registering Kernel netlink command channel
Oct 18 00:24:32 centos Keepalived_vrrp:Registering gratutious ARP shared channel
Oct 18 00:24:33 centos Keepalived_vrrp:Opening file '/etc/keepalived/keepalived.conf'.
Oct 18 00:24:33 centos Keepalived_vrrp:Configuration is using : 63606 Bytes
Oct 18 00:24:33 centos Keepalived_vrrp:Using LinkWatch kernel netlink reflector...
Oct 18 00:24:33 centos Keepalived_vrrp: VRRPsockpool: [ifindex(2), proto(112), fd(10,11)]
Oct 18 00:24:33 centos Keepalived_vrrp:VRRP_Script(check_httpd) succeeded
Oct 18 00:24:34 centos Keepalived_vrrp:VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 18 00:24:35 centos Keepalived_vrrp:VRRP_Instance(VI_1) Entering MASTER STATE
Oct 18 00:24:35 centos Keepalived_vrrp:VRRP_Instance(VI_1) setting protocol VIPs.
Oct 18 00:24:35 centos Keepalived_vrrp:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.110
Oct 18 00:24:40 centosKeepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for192.168.30.110
Backup:
Oct 18 00:24:38 centos Keepalived: StartingKeepalived v1.2.2 (10/17,2013)
Oct 18 00:24:38 centos Keepalived: StartingVRRP child process, pid=1382
Oct 18 00:24:38 centos Keepalived_vrrp:Registering Kernel netlink reflector
Oct 18 00:24:38 centos Keepalived_vrrp:Registering Kernel netlink command channel
Oct 18 00:24:38 centos Keepalived_vrrp:Registering gratutious ARP shared channel
Oct 18 00:24:38 centos Keepalived_vrrp:Opening file '/etc/keepalived/keepalived.conf'.
Oct 18 00:24:38 centos Keepalived_vrrp:Configuration is using : 63604 Bytes
Oct 18 00:24:38 centos Keepalived_vrrp:Using LinkWatch kernel netlink reflector...
Oct 18 00:24:38 centos Keepalived_vrrp:VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 18 00:24:38 centos Keepalived_vrrp: VRRPsockpool: [ifindex(3), proto(112), fd(10,11)]
Oct 18 00:24:38 centosKeepalived_vrrp: VRRP_Script(check_httpd) succeeded
2、master 模仿 httpd故障然后 master 和 backup日志如下
master:
Oct 18 00:27:43 centos Keepalived:Terminating on signal
Oct 18 00:27:43 centos Keepalived: StoppingKeepalived v1.2.2 (10/17,2013)
Oct 18 00:27:43 centos Keepalived_vrrp:Terminating VRRP child process on signal
Oct 18 00:27:43 centosKeepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
backup:
Oct 18 00:27:47 centos Keepalived_vrrp:VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 18 00:27:48 centos Keepalived_vrrp:VRRP_Instance(VI_1) Entering MASTER STATE
Oct 18 00:27:48 centos Keepalived_vrrp:VRRP_Instance(VI_1) setting protocol VIPs.
Oct 18 00:27:48 centos Keepalived_vrrp:VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.30.110
Oct 18 00:27:53 centosKeepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for192.168.30.110
注:
该文档为自己做测试撰写,欢迎拍砖。