LVS+Keepalived DR模式(附脚本)
keepalived官方网站:http://www.keepalived.org/
Keepalived stable tarball
- Keepalived for Linux - Version 1.2.13 - May 13, 2014
以keepalived-1.2.13.tar.gz为例
说明:
1.配置好本地yum源,编译源码包会依赖相关库文件
2.把以下脚本和keepalived源码包放在相同目录
3.添加脚本的可执行权限
PS:脚本执行完毕后会直接将keepalived通过chkconifg加入开机启动,相关目录如下
/usr/local/src/keepalived-1.2.13
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
/usr/sbin/keepalived
/etc/init.d/keepalived
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
上面放大标红的部分是chkconfig keepalived on时在/etc/rc.d/rc{2,3,4,5}.d/这4个级别里生成的开机启动和关机停止顺序,也就是会生成如:/etc/rc.d/rc3.d/S21keepalived这个链接以便在3运行级别时在21这个顺序启动keepalived
我的实验环境如下:
OS:CentOS6.5 64bit
两台LVS调度器:
VIP:192.168.10.100
主(MASTER,优先级100): 192.168.10.101
备(BACKUP,优先级80):
查看调度结果,watch -n 1 ipvsadm -L -n
两台http服务器:
VIP:192.168.10.100
RealServer1:192.168.10.11
RealServer2:192.168.10.12
客户端:
CIP:192.168.10.2
压力测试, ab -n 10000 192.168.10.100/
下面是脚本源码定义的一些变量,有需要的可心自行修改。
注意:脚本2,脚本4是直接生成配置文件重启仍然生效,脚本3和脚本5只是当前生效重启后需再次运行。
#Define E-mail & Smtp_Server
Mail2=
Mail3=
Smtp_Server=
#Define keepalived "MASTER" or "BACKUP"
State_Flag_Priority=100
#State_Flag=BACKUP
#State_Flag_Priority=80
#Define Virtual Server IP & Listen Port
VIP1=192.168.10.100
VIP1_Listen_Port=80
VIP2=
VIP2_Listen_Port=
#Define Real Server IP & Listen Port
VIP1_RIP1=192.168.10.11
VIP1_RIP1_Listen_Port=80
VIP1_RIP2=192.168.10.12
VIP1_RIP2_Listen_Port=80
VIP1_RIP3=
VIP1_RIP3_Listen_Port=
VIP2_RIP1=
VIP2_RIP1_Listen_Port=
VIP2_RIP2=
VIP2_RIP2_Listen_Port=
VIP2_RIP3=
VIP2_RIP3_Listen_Port=
#Define Weight
Weight_VIP1_RIP1=1
Weight_VIP1_RIP2=2
Weight_VIP1_RIP3=3
Weight_VIP2_RIP1=
Weight_VIP2_RIP2=
Weight_VIP2_RIP3=
#Define Schedule & Model
three
Mode_VIP1=DR
Schedule_VIP2=
Mode_VIP2=
脚本1:调度器keepalived安装脚本源码:
############################################
#!/bin/bash
#To install keepalived automatically
#Made by liujun,2014/08/15
############################################
#Define system environment PATH
export PATH=$PATH
#Source function library
. /etc/rc.d/init.d/functions
#Define Keepalived Version
Keepalived_Version=1.2.13
Install_Directory=/usr/local/src
#Install the build-essential
PACKAGE="kernel-devel gcc openssl-devel popt-devel libnl-devel net-snmp-devel"
for i in $PACKAGE
do
done
echo -e " "
echo -e "... ... "
sleep 1
echo -e "... ... "
echo -e " "
#Build and install
tar -xvf keepalived-$Keepalived_Version.tar.gz -C /usr/local/src
cd /usr/local/src/keepalived-$Keepalived_Version
./configure
--prefix=/usr
--sysconf=/etc
--with-kernel-dir=/usr/src/kernels/$(uname -r)
--enable-snmp
--enable-sha1
make && make install
echo -e " "
echo -e "e[31;1mkeepalivede[0m is e[32;1minstallede[0m"
echo -e " "
#Autostart on init 3/5
#cp $Install_Directory/keepalived-$Keepalived_Version/keepalived/etc/init.d/keepalived.init /etc/rc.d/rc3.d/S99keepalived
#cp $Install_Directory/keepalived-$Keepalived_Version/keepalived/etc/init.d/keepalived.init /etc/rc.d/rc5.d/S99keepalived
chkconfig keepalived on
chkconfig --list keepalived
###################################################
##########################
#Build keepalived.conf
##########################
#Define E-mail & Smtp_Server
Mail1=
Mail2=
Mail3=
Smtp_Server=
#Define keepalived "MASTER" or "BACKUP"
State_Flag=MASTER
State_Flag_Priority=100
#State_Flag=BACKUP
#State_Flag_Priority=80
#Define Virtual Server IP & Listen Port
VIP1=192.168.10.100
VIP1_Listen_Port=80
VIP2=
VIP2_Listen_Port=
#Define Real Server IP & Listen Port
VIP1_RIP1=192.168.10.11
VIP1_RIP1_Listen_Port=80
VIP1_RIP2=192.168.10.12
VIP1_RIP2_Listen_Port=80
VIP1_RIP3=
VIP1_RIP3_Listen_Port=
VIP2_RIP1=
VIP2_RIP1_Listen_Port=
VIP2_RIP2=
VIP2_RIP2_Listen_Port=
VIP2_RIP3=
VIP2_RIP3_Listen_Port=
#Define Weight
Weight_VIP1_RIP1=1
Weight_VIP1_RIP2=2
Weight_VIP1_RIP3=3
Weight_VIP2_RIP1=
Weight_VIP2_RIP2=
Weight_VIP2_RIP3=
#Define Schedule & Model
Schedule_VIP1=wlc
Mode_VIP1=DR
Schedule_VIP2=
Mode_VIP2=
mv /etc/keepalived/keepalived.conf{,.bak}
cat >/etc/keepalived/keepalived.conf <<HERE
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
}
virtual_server $VIP1 $VIP1_Listen_Port {
!
!
!
!
!
!
!
!
}
!virtual_server $VIP2 $VIP2_Listen_Port {
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!}
HERE
echo -e " "
/etc/init.d/keepalived start
sleep 1
echo -e " "
/etc/init.d/keepalived restart
###################################################
脚本2:调度器子接口及路由配置脚本(永久生效)
############################################
#!/bin/bash
#To contribute to lvs_dr
#Made by liujun,2014/08/11
############################################
#Define system environment PATH
export PATH=$PATH
#Source function library
. /etc/rc.d/init.d/functions
#Define Variable
Sysconfig_Dir=/etc/sysconfig/network-scripts
VIP=192.168.10.100
Eth=lo
Eth_Child_Num=0
#################
#Define functions
#################
network_restart() {
echo -e "e[31;1mNetworkinge[0m e[32;1mrestarte[0m "
/etc/init.d/network restart
}
start_begin() {
echo -e "e[31;1mActivinge[0m e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mAddinge[0m route rules to e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mChanginge[0m
echo -e "... ..."
sleep 0.5
}
start_end() {
sleep 0.5
echo -e " "
echo -e "ifcfg-e[32;1m$Eth:$Eth_Child_Nume[0m is e[31;1mactived!e[0m $(ifconfig $Eth:$Eth_Child_Num)"
echo -e "e[31;1mRoute rulese[0m is e[32;1mcreatede[0m $(route -ne|grep $VIP)"
echo -e "e[31;1mKernel argse[0m has been e[32;1mattached!e[0m $(sysctl -p 2>/dev/null|grep arp_)"
}
stop_begin() {
echo -e "e[31;1mRemovinge[0m route rules from e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mInactivinge[0m
sleep 0.5
echo -e "e[31;1mRestoringe[0m
echo -e "... ..."
}
stop_end() {
sleep 0.5
echo -e " "
echo -e "The e[31;1mroute rulese[0m is e[32;1mremovede[0m "
echo -e "The ifcfg-e[31;1m$Eth:$Eth_Child_Nume[0m is e[32;1minactivede[0m "
echo -e "e[31;1mKernel argse[0m has been e[32;1mrestored!e[0m $(sysctl -p >/dev/null 2>&1|grep arp_)"
sleep 0.5
}
status() {
echo -e " "
if [ "$(grep net.ipv4.conf.lo.arp_ignore /etc/sysctl.conf)" == "" ];then
fi
}
start() {
#Build /etc/sysconfig/network-scripts/ifcfg-lo:0
start_begin
if ! [ -f $Sysconfig_Dir/ifcfg-$Eth:$Eth_Child_Num ];then
DEVICE=$Eth:$Eth_Child_Num
BOOTPROTO=static
IPADDR=$VIP
NETMASK=255.255.255.255
BROADCAST=$VIP
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
HERE
fi
#Build /etc/sysconfig/network-scripts/route-lo:0
if ! [ -f $Sysconfig_Dir/route-$Eth:$Eth_Child_Num ];then
cat >$Sysconfig_Dir/route-$Eth:$Eth_Child_Num <<HERE
$VIP
HERE
fi
#Modify kernel args
if [ "$(grep net.ipv4.conf.lo.arp_ignore /etc/sysctl.conf)" == "" ];then
echo "
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2">>/etc/sysctl.conf
fi
network_restart
start_end
}
stop() {
stop_begin
rm -rf $Sysconfig_Dir/ifcfg-$Eth:$Eth_Child_Num >/dev/null 2>&1
rm -rf $Sysconfig_Dir/route-$Eth:$Eth_Child_Num >/dev/null 2>&1
sed -i '/net.ipv4.conf.lo.arp_ignore = 1/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.all.arp_ignore = 1/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.lo.arp_announce = 2/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.all.arp_announce = 2/ d' /etc/sysctl.conf
stop_end
network_restart
}
#How to use
case $1 in
start)
stop)
restart)
status)
*)
esac
exit $?
脚本3:调度器子接口及路由配置脚本(临时生效)
############################################
#!/bin/bash
#To contribute to lvs_dr
#Made by liujun,2014/08/11
############################################
#Define system environment PATH
export PATH=$PATH
#Source function library
. /etc/rc.d/init.d/functions
VIP=192.168.10.100
#Define
start() {
}
stop() {
}
case $1 in
start)
stop)
restart)
*)
esac
exit $?
脚本4:RealServer子接口,路由配置及内核参数修改脚本(永久生效)
############################################
#!/bin/bash
#modify kernel args to contribute to lvs_dr
#Made by liujun,2014/08/11
############################################
#Define system environment PATH
export PATH=$PATH
#Source function library
. /etc/rc.d/init.d/functions
#Define Variable
Sysconfig_Dir=/etc/sysconfig/network-scripts
VIP=192.168.10.100
Eth=lo
Eth_Child_Num=0
#################
#Define functions
#################
network_restart() {
echo -e "e[31;1mNetworkinge[0m e[32;1mrestarte[0m "
/etc/init.d/network restart
}
start_begin() {
echo -e "e[31;1mActivinge[0m e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mAddinge[0m route rules to e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mChanginge[0m
echo -e "... ..."
sleep 0.5
}
start_end() {
sleep 0.5
echo -e " "
echo -e "ifcfg-e[32;1m$Eth:$Eth_Child_Nume[0m is e[31;1mactived!e[0m $(ifconfig $Eth:$Eth_Child_Num)"
echo -e "e[31;1mRoute rulese[0m is e[32;1mcreatede[0m $(route -ne|grep $VIP)"
echo -e "e[31;1mKernel argse[0m has been e[32;1mattached!e[0m $(sysctl -p 2>/dev/null|grep arp_)"
}
stop_begin() {
echo -e "e[31;1mRemovinge[0m route rules from e[32;1m$Eth:$Eth_Child_Nume[0m "
sleep 0.5
echo -e "e[31;1mInactivinge[0m
sleep 0.5
echo -e "e[31;1mRestoringe[0m
echo -e "... ..."
}
stop_end() {
sleep 0.5
echo -e " "
echo -e "The e[31;1mroute rulese[0m is e[32;1mremovede[0m "
echo -e "The ifcfg-e[31;1m$Eth:$Eth_Child_Nume[0m is e[32;1minactivede[0m "
echo -e "e[31;1mKernel argse[0m has been e[32;1mrestored!e[0m $(sysctl -p >/dev/null 2>&1|grep arp_)"
sleep 0.5
}
status() {
echo -e " "
if [ "$(grep net.ipv4.conf.lo.arp_ignore /etc/sysctl.conf)" == "" ];then
fi
}
start() {
#Build /etc/sysconfig/network-scripts/ifcfg-lo:0
start_begin
if ! [ -f $Sysconfig_Dir/ifcfg-$Eth:$Eth_Child_Num ];then
DEVICE=$Eth:$Eth_Child_Num
BOOTPROTO=static
IPADDR=$VIP
NETMASK=255.255.255.255
BROADCAST=$VIP
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
HERE
fi
#Build /etc/sysconfig/network-scripts/route-lo:0
if ! [ -f $Sysconfig_Dir/route-$Eth:$Eth_Child_Num ];then
cat >$Sysconfig_Dir/route-$Eth:$Eth_Child_Num <<HERE
$VIP
HERE
fi
#Modify kernel args
if [ "$(grep net.ipv4.conf.lo.arp_ignore /etc/sysctl.conf)" == "" ];then
echo "
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2">>/etc/sysctl.conf
fi
network_restart
start_end
}
stop() {
stop_begin
rm -rf $Sysconfig_Dir/ifcfg-$Eth:$Eth_Child_Num >/dev/null 2>&1
rm -rf $Sysconfig_Dir/route-$Eth:$Eth_Child_Num >/dev/null 2>&1
sed -i '/net.ipv4.conf.lo.arp_ignore = 1/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.all.arp_ignore = 1/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.lo.arp_announce = 2/ d' /etc/sysctl.conf
sed -i '/net.ipv4.conf.all.arp_announce = 2/ d' /etc/sysctl.conf
stop_end
network_restart
}
#How to use
case $1 in
start)
stop)
restart)
status)
*)
esac
exit $?
脚本5:RealServer子接口,路由配置及内核参数修改脚本(临时生效)
############################################
#!/bin/bash
#modify kernel args to contribute to lvs_dr
#Made by liujun,2014/08/11
############################################
#Define system environment PATH
export PATH=$PATH
#Source function library
. /etc/rc.d/init.d/functions
VIP=192.168.10.100
#Define
start() {
}
stop() {
}
case $1 in
start)
stop)
restart)
*)
esac
exit $?