1. 测试环境配置列表:
操作系统:Linux_Centos6.7_64
Keepalived版本:1.4.2
IPCC-Master
IP:172.16.12.95
Hostname:xuniipcc
IPCC-Slave
IP:172.16.12.94
Hostname:ipcc
2. 卸载防火墙
service iptablesstop
chkconfig iptablesoff
rpm -e--nodeps iptables
3. 安装keepalived
将keepalived-1.4.2.tar.gz上传到/usr/local/src目录下
cd /usr/local/src
解压缩:tar –zxvfkeepalived-1.4.2.tar.gz
cd keepalived-1.4.2
./configure
出现以下图样即为执行命令成功
注:如在执行此命令时,出现checking错误,请自行将未装上的依赖包装上,装完后再次执行此命令即可
make && make install
出现以下图像即为执行成功
cp /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived/etc/rc.d/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/
设置开机启动服务
echo "/etc/init.d/keepalivedstart" >> /etc/rc.local
4. 配置keepalived
4.1备份原有配置文件:cp/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
4.2、将主备对应配置文件及服务检测脚本放入/etc/keepalived文件夹中
完整配置可参考下图:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server127.0.0.1 #修改部分
smtp_connect_timeout 30
router_id red1 #修改部分
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#此步骤为需要调用脚本暂时可不配
vrrp_script chk_http_port {
script"/opt/check_tomcat.sh"
interval 2
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100 #修改部分
advert_int 1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress {
172.16.12.96 #修改部分,浮动ip
}
#此部分暂时不配
track_script {
chk_http_port
}
}
注:未给出的部分,不用修改,保持不变
4.3、配置完成后,分别在主备机上启动keepalived,启动命令/etc/init.d/keepalived start
4.4、输入命令psaux | grep keepalived,如keepalived有三个进程即启动成功
5. 测试
登录IPCCMaster服务器,查看ip配置,各项服务正常,浮动ip出现:
当chk_service返回不正常值时,浮动ip切换到Slave上,实现切换:
六、脚本
6.1、Master
chek_service.sh:
#!/bin/bash
#args
Begin=`date +"%Y%m%d %H:%M:%S"`
hisanccnum=`service hisancc status |wc -l`
cmsnum=`service cms status |wc -l`
tomcatnum=`service tomcat status |wc -l`
hisancc_result="`service hisancc status`"
cms_result="`service cms status`"
tomcat_result="`service tomcat status`"
str="error"
str3=""
str4=""
str5="p"
result="pass"
echo $result
echo $hisanccnum
echo $cmsnum
echo $tomcatnum
#hisancc_chk
for((i=1;i<="$hisanccnum";i++));
do
str3="`echo "$hisancc_result"|awk '{print $3}'| sed -n $i$str5`"
str4="`echo "$hisancc_result"|awk '{print $1}'| sed -n $i$str5`"
if [ "running..." = "$str3" ];then
echo $str4 is runnning
echo $result
else
echo $str4 is stopped!
result="fail"
echo $result
echo $str4 is stopped!:$Begin >> /etc/keepalived/log.log
fi
done
echo -------------------------------------------------------
#cms_chek
for((i=3;i<="$cmsnum";i++));
do
str3="`echo "$cms_result"|awk '{print $4}'| sed -n $i$str5`"
str4="`echo "$cms_result"|awk '{print $1}'| sed -n $i$str5`"
if [ "RUNNING" = "$str3" ];then
echo $str4 is runnning
echo $result
else
echo $str4 is stopped!
result="fail"
echo $result
echo $str4 is stopped!:$Begin >> /etc/keepalived/log.log
fi
done
echo -------------------------------------------------------
#tomcat_chk
str4="`echo "$tomcat_result"|awk '{print $4}'| sed -n 1$str5`"
echo $str4
if [ "running," = "$str4" ];then
echo tomcat is runnning
echo $result
else
echo tomcat is stopped!
result="fail"
echo $result
echo tomcat is stopped!:$Begin >> /etc/keepalived/log.log
fi
echo -------------------------------------------------------
echo -------------------------------------------------------
echo checkresult:$result
#check if the sehll is run normal
if [ "pass" = "$result" ];then
exit 0
else
exit 1
fi
keepalived.conf文件:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.16.12.95
smtp_connect_timeout 30
router_id xuniipcc
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk {
script "/etc/keepalived/chek_service.sh"
interval 2
weight -5
}
vrrp_instance VI_1 {
state master
interface eth0
virtual_router_id 51
priority 102
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.12.96
}
track_script {
chk
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
6.2、Slave
keepalived.conf文件:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.16.12.94
smtp_connect_timeout 30
router_id ipcc
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_cms {
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.12.96
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}