1.规划
1.1服务器环境规划
负载服务器master及WEB服务器1真实IP:10.10.195.53
负载服务器backup及WEB服务器2真实IP:10.10.195.190
负载服务器虚拟ip:10.10.195.212
1.2软件环境规划
操作系统:RedHatEnterpriseLinuxServerrelease5.6(Tikanga)
keepalived:keepalived-1.2.19
Java:jdk-1.7.0_79
Tomcat:apache-tomcat-7.0.64
2.负载服务器配置
这里只叙述如何安装配置keepalived,至于java以及tomcat的安装及配置,这里不赘述。
2.1安装keepalived
[~]tar-zxvfkeepalived-1.2.19.tar.gz
[~]cdkeepalived-1.2.19
[keepalived-1.2.19]./configure--prefix=/usr/local/keepalived--disable-fwmark
#(如果直接输入./configure有可能报错:configure:error:NoSO_MARKdeclarationinheaders)
[keepalived-1.2.19]make
[keepalived-1.2.19]makeinstall
2.2配置keepalived服务
[keepalived-1.2.19]cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
[keepalived-1.2.19]cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
[keepalived-1.2.19]cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
[keepalived-1.2.19]mkdir/etc/keepalived
[keepalived-1.2.19]cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
[keepalived-1.2.19]chkconfig--addkeepalived
[keepalived-1.2.19]chkconfigkeepalivedon
重启\启动\关闭\查看状态keepalived
servicekeepalivedrestart
servicekeepalivedstart
servicekeepalivedstop
servicekeepalivedstatus
2.3配置keepalived.conf文件
[~]mv/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak
[~]vi/etc/keepalived//keepalived.conf
主备机的keepalived.conf文件大致相同,只是注意红色标注的地方。
主机配置:
global_defs{
router_idNodeA
}
vrrp_scriptchk_http_port{
script"/opt/tomcat.pid"
interval5
weight2
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth0
virtual_router_id52
priority150
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port
}
virtual_ipaddress{
10.10.195.212
}
}
备机配置:
global_defs{
router_idNodeB
}
vrrp_scriptchk_http_port{
script"/opt/tomcat.pid"
interval5
weight2
}
vrrp_instanceVI_1{
stateBACKUP
interfaceeth0
virtual_router_id52
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port
}
virtual_ipaddress{
10.10.195.212
}
}
2.4配置/opt/tomcat.pid
#!/bin/bash
#description:checktomcatserviceanddecidewhetherstopthekeepalivedornot
#editedbyzzh:2015-10-14
CATALINA_HOME=/users/shr/apache-tomcat-7.0.64
JAVA_HOME=/users/shr/util/JavaDir/jdk
exportCATALINA_HOME
exportJAVA_HOME
psax--width=1000|grep"org.apache.catalina.startup.Bootstrapstart"|grep-v"grep"|awk'{printf$1""}'|wc|awk'{print$2}'>tomcat_process_count.txt
readline< tomcat_process_count.txt
start_tomcat=$CATALINA_HOME/bin/startup.sh
if[${line}-lt1]
then
echo-n"===Startingtomcat===:"
${start_tomcat}
#:sudoservicetomcatstart
echo"===tomcatstartok.==="
sleep3
#checkthetomcatstatus.
psax--width=1000|grep"org.apache.catalina.startup.Bootstrapstart"|grep-v"grep"|awk'{printf$1""}'|wc|awk'{print$2}'>tomcat_process_count.txt
readline2< tomcat_process_count.txt
if[${line2}-lt1]
then
sudoservicekeepalivedstop
fi
fi
rmtomcat_process_count.txt
#shellend.
3.日志查看
3.1正常启动
输入tail-f/var/log/messages查看启动keepalived日志:
(MASTER:10.10.195.53)
Sep2915:49:16shrKeepalived[5536]:StartingKeepalivedv1.2.19(09/21,2015)
Sep2915:49:16shrKeepalived_healthcheckers[5538]:NetlinkreflectorreportsIP10.10.195.53added
Sep2915:49:16shrKeepalived_healthcheckers[5538]:NetlinkreflectorreportsIP10.10.195.53added
Sep2915:49:16shrKeepalived_healthcheckers[5538]:RegisteringKernelnetlinkreflector
Sep2915:49:16shrKeepalived_healthcheckers[5538]:RegisteringKernelnetlinkcommandchannel
Sep2915:49:16shrKeepalived_healthcheckers[5538]:Openingfile'/etc/keepalived/keepalived.conf'.
Sep2915:49:16shrKeepalived_healthcheckers[5538]:Configurationisusing:6572Bytes
Sep2915:49:16shrKeepalived[5537]:StartingHealthcheckchildprocess,pid=5538
Sep2915:49:16shrKeepalived_healthcheckers[5538]:UsingLinkWatchkernelnetlinkreflector...
Sep2915:49:16shrKeepalived_vrrp[5539]:NetlinkreflectorreportsIP10.10.195.53added
Sep2915:49:16shrKeepalived_vrrp[5539]:NetlinkreflectorreportsIP10.10.195.53added
Sep2915:49:16shrKeepalived[5537]:StartingVRRPchildprocess,pid=5539
Sep2915:49:16shrKeepalived_vrrp[5539]:RegisteringKernelnetlinkreflector
Sep2915:49:16shrKeepalived_vrrp[5539]:RegisteringKernelnetlinkcommandchannel
Sep2915:49:16shrKeepalived_vrrp[5539]:RegisteringgratuitousARPsharedchannel
Sep2915:49:16shrKeepalived_vrrp[5539]:Openingfile'/etc/keepalived/keepalived.conf'.
Sep2915:49:16shrKeepalived_vrrp[5539]:Configurationisusing:36541Bytes
Sep2915:49:16shrKeepalived_vrrp[5539]:UsingLinkWatchkernelnetlinkreflector...
Sep2915:49:16shrKeepalived_vrrp[5539]:VRRPsockpool:[ifindex(2),proto(112),unicast(0),fd(10,11)]
Sep2915:49:17shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)TransitiontoMASTERSTATE
Sep2915:49:17shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)Receivedlowerprioadvert,forcingnewelection
Sep2915:49:18shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)EnteringMASTERSTATE
Sep2915:49:18shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)settingprotocolVIPs.
Sep2915:49:18shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)SendinggratuitousARPsoneth0for10.10.195.212
Sep2915:49:18shrKeepalived_vrrp[5539]:NetlinkreflectorreportsIP10.10.195.212added
Sep2915:49:18shrKeepalived_healthcheckers[5538]:NetlinkreflectorreportsIP10.10.195.212added
Sep2915:49:23shrKeepalived_vrrp[5539]:VRRP_Instance(VI_1)SendinggratuitousARPsoneth0for10.10.195.212
(BACKUP:10.10.195.190)
Sep2915:46:25server1Keepalived[18218]:StartingKeepalivedv1.2.19(09/25,2015)
Sep2915:46:25server1Keepalived_healthcheckers[18220]:NetlinkreflectorreportsIP10.10.195.190added
Sep2915:46:25server1Keepalived_healthcheckers[18220]:NetlinkreflectorreportsIP10.10.195.190added
Sep2915:46:25server1Keepalived_healthcheckers[18220]:RegisteringKernelnetlinkreflector
Sep2915:46:25server1Keepalived_healthcheckers[18220]:RegisteringKernelnetlinkcommandchannel
Sep2915:46:25server1Keepalived_healthcheckers[18220]:Openingfile'/etc/keepalived/keepalived.conf'.
Sep2915:46:25server1Keepalived_healthcheckers[18220]:Configurationisusing:6682Bytes
Sep2915:46:25server1Keepalived[18219]:StartingHealthcheckchildprocess,pid=18220
Sep2915:46:25server1Keepalived[18219]:StartingVRRPchildprocess,pid=18221
Sep2915:46:25server1Keepalived_vrrp[18221]:NetlinkreflectorreportsIP10.10.195.190added
Sep2915:46:25server1Keepalived_vrrp[18221]:NetlinkreflectorreportsIP10.10.195.190added
Sep2915:46:25server1Keepalived_healthcheckers[18220]:UsingLinkWatchkernelnetlinkreflector...
Sep2915:46:25server1Keepalived_vrrp[18221]:RegisteringKernelnetlinkreflector
Sep2915:46:25server1Keepalived_vrrp[18221]:RegisteringKernelnetlinkcommandchannel
Sep2915:46:25server1Keepalived_vrrp[18221]:RegisteringgratuitousARPsharedchannel
Sep2915:46:25server1Keepalived_vrrp[18221]:Openingfile'/etc/keepalived/keepalived.conf'.
Sep2915:46:25server1Keepalived_vrrp[18221]:Configurationisusing:36651Bytes
Sep2915:46:25server1Keepalived_vrrp[18221]:UsingLinkWatchkernelnetlinkreflector...
Sep2915:46:25server1Keepalived_vrrp[18221]:VRRP_Instance(VI_1)EnteringBACKUPSTATE
Sep2915:46:25server1Keepalived_vrrp[18221]:VRRPsockpool:[ifindex(2),proto(112),unicast(0),fd(10,11)]
3.2主备切换
l当在主机(MASTER:10.10.195.53)中输入sudoservicekeepalivedstop,此时就会进行主备切换,主机切换成备机。
主机(MASTER:10.10.195.53)输出如下
Oct1413:25:09shrKeepalived_vrrp[26683]:VRRP_Instance(VI_1)sending0priority
Oct1413:25:09shrKeepalived_vrrp[26683]:VRRP_Instance(VI_1)removingprotocolVIPs.
Oct1413:25:09shrKeepalived_healthcheckers[26682]:NetlinkreflectorreportsIP10.10.195.212removed
Oct1413:25:09shrKeepalived[26681]:StoppingKeepalivedv1.2.19(09/21,2015)
备机(BACKUP:10.10.195.190)输出如下
Oct1413:19:58server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)TransitiontoMASTERSTATE
Oct1413:19:59server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)EnteringMASTERSTATE
Oct1413:19:59server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)settingprotocolVIPs.
Oct1413:19:59server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)SendinggratuitousARPsoneth0for10.10.195.212
Oct1413:19:59server1Keepalived_healthcheckers[30889]:NetlinkreflectorreportsIP10.10.195.212added
Oct1413:19:59server1Keepalived_vrrp[30890]:NetlinkreflectorreportsIP10.10.195.212added
Oct1413:20:04server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)SendinggratuitousARPsoneth0for10.10.195.212
l当在主机(MASTER:10.10.195.53)中输入sudoservicekeepalivedstart,此时就会切换成主机。备机(BACKUP:10.10.195.190)输出如下信息:
Oct1413:25:11server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)Receivedhigherprioadvert
Oct1413:25:11server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)EnteringBACKUPSTATE
Oct1413:25:11server1Keepalived_vrrp[30890]:VRRP_Instance(VI_1)removingprotocolVIPs.
Oct1413:25:11server1Keepalived_healthcheckers[30889]:NetlinkreflectorreportsIP10.10.195.212removed
Oct1413:25:11server1Keepalived_vrrp[30890]:NetlinkreflectorreportsIP10.10.195.212removed
4.查看虚拟ip
可以通过ipaddshow命令查看添加的虚拟ip:
[shr@shrbin]$ipaddshow
1:lo:mtu16436qdiscnoqueue
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
2:eth0:mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:6b:f2:a8brdff:ff:ff:ff:ff:ff
inet10.10.195.53/24brd10.10.195.255scopeglobaleth0
inet10.10.195.212/32scopeglobaleth0
5.常见错误
5.1常见错误一:
/var/log/messageshasthousandsoferrorslikethis:
Jun2809:18:32rustKeepalived_vrrp:receiveaninvalidipnumbercount
associatedwithVRID!
Jun2809:18:32rustKeepalived_vrrp:bogusVRRPpacketreceivedoneth0!!!
Jun2809:18:32rustKeepalived_vrrp:VRRP_Instance(VI_1)Droppingreceived
VRRPpacket...
Thebackupdirectorstartsup,butdoesn'tlistenonthevirtualaddresses
atall.Its/var/log/messageshasthousandsoferrorslikethis:
Jun2806:25:05styeKeepalived_vrrp:receiveaninvalidipnumbercount
associatedwithVRID!
Jun2806:25:05styeKeepalived_vrrp:bogusVRRPpacketreceivedoneth0!!!
Jun2806:25:05styeKeepalived_vrrp:VRRP_Instance(VI_1)ignoringreceived
advertisment...
解决方法:
改变配置文件/etc/keepalived/keepalived.conf中virtual_router_id为另一个值即可。
(changedthevridtoanothernumberanditworkedfine).
5.2常见错误二:
主机执行到VRRP_Instance(VI_1)EnteringBACKUPSTATE之后没有执行VRRP_Instance(VI_1)settingprotocolVIPs。
解决方法:
可能是配置文件keepalived.conf中{左边没有空格。
附
linux添加tomcat服务
在/etc/init.d中输入sudovimtomcat
脚本具体内容如下:
#!/bin/bash
#chkconfig:23451090
#description:StartsandStopsthetomcatdaemon
#editedbyZZH:2015-10-14
CATALINA_HOME=/users/shr/apache-tomcat-7.0.64
JAVA_HOME=/users/shr/util/JavaDir/jdk
exportCATALINA_HOME
exportJAVA_HOME
start_tomcat=$CATALINA_HOME/bin/startup.sh
stop_tomcat=$CATALINA_HOME/bin/shutdown.sh
if[!-f$CATALINA_HOME/bin/catalina.sh]
then
echo"===Tomcatisnotavailable.==="
exit
fi
start()
{
echo-n"===Startingtomcat===:"
${start_tomcat}
echo"===tomcatstartok.==="
}
stop()
{
echo-n"===Shuttingdowntomcat===:"
${stop_tomcat}
echo"===tomcatstopok.==="
}
status()
{
psax--width=1000|grep"org.apache.catalina.startup.Bootstrapstart"|grep-v"grep"|awk'{printf$1""}'|wc|awk'{print$2}'>tomcat_process_count.txt
readline< tomcat_process_count.txt
rmtomcat_process_count.txt
if[$line-gt0]
then
echo-n"tomcat(pid="
psax--width=1000|grep"org.apache.catalina.startup.Bootstrapstart"|grep-v"grep"|awk'{printf$1""}'|awk'{print$1}'>tomcat_process_pid.txt
readpid< tomcat_process_pid.txt
rmtomcat_process_pid.txt
echo-n$pid
echo-n")isrunning..."
echo
else
echo"tomcatisstopped"
fi
}
case"$1"in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep1
start
;;
status)
status
;;
*)
echo"Usage:$0{start|stop|restart}"
exit1
esac
exit0
#shellend.
在终端输入:
sudochmod755tomcat
sudochkconfig--addtomcat
tomcat启动servicetomcatstart
tomcat关闭servicetomcatstop
tomcat重启servicetomcatrestart
tomcat状态查看servicetomcatstatus