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 软件环境规划
操作系统:Red Hat Enterprise Linux Server release 5.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 -zxvf keepalived-1.2.19.tar.gz
[~] cd keepalived-1.2.19
[keepalived-1.2.19] ./configure --prefix=/usr/local/keepalived --disable-fwmark
#(如果直接输入./configure有可能报错:configure:error: No SO_MARK declaration in headers)
[keepalived-1.2.19] make
[keepalived-1.2.19] make install
3.2 主备切换
l 当在主机(MASTER:10.10.195.53)中输入 sudoservice keepalived stop,此时就会进行主备切换,主机切换成备机。
主机(MASTER:10.10.195.53)输出如下
Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) sending 0 priority
Oct 14 13:25:09 shr Keepalived_vrrp[26683]:VRRP_Instance(VI_1) removing protocol VIPs.
Oct 14 13:25:09 shrKeepalived_healthcheckers[26682]: Netlink reflector reports IP 10.10.195.212removed
Oct 14 13:25:09 shr Keepalived[26681]:Stopping Keepalived v1.2.19 (09/21,2015)
备机(BACKUP:10.10.195.190)输出如下
Oct 14 13:19:58 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 14 13:19:59 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for10.10.195.212
Oct 14 13:19:59 server1 Keepalived_healthcheckers[30889]:Netlink reflector reports IP 10.10.195.212 added
Oct 14 13:19:59 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 added
Oct 14 13:20:04 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for10.10.195.212
l 当在主机(MASTER:10.10.195.53)中输入sudoservice keepalived start,此时就会切换成主机。备机(BACKUP:10.10.195.190)输出如下信息:
Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Received higher prio advert
Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 14 13:25:11 server1Keepalived_vrrp[30890]: VRRP_Instance(VI_1) removing protocol VIPs.
Oct 14 13:25:11 server1Keepalived_healthcheckers[30889]: Netlink reflector reports IP 10.10.195.212removed
Oct 14 13:25:11 server1Keepalived_vrrp[30890]: Netlink reflector reports IP 10.10.195.212 removed
4. 查看虚拟ip
可以通过ip add show命令查看添加的虚拟ip:
[shr@shr bin]$ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ff
inet 10.10.195.53/24 brd 10.10.195.255 scope global eth0
inet 10.10.195.212/32scope global eth0
5. 常见错误
5. 1常见错误一:
/var/log/messages has thousands of errorslike this:
Jun 28 09:18:32 rust Keepalived_vrrp:receive an invalid ip number count
associated with VRID!
Jun 28 09:18:32 rust Keepalived_vrrp: bogusVRRP packet received on eth0 !!!
Jun 28 09:18:32 rust Keepalived_vrrp:VRRP_Instance(VI_1) Dropping received
VRRP packet...
The backup director starts up, but doesn'tlisten on the virtual addresses
at all. Its /var/log/messages has thousands of errors like this:
Jun 28 06:25:05 stye Keepalived_vrrp:receive an invalid ip number count
associated with VRID!
Jun 28 06:25:05 stye Keepalived_vrrp: bogusVRRP packet received on eth0 !!!
Jun 28 06:25:05 stye Keepalived_vrrp:VRRP_Instance(VI_1) ignoring received
advertisment...<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
解决方法:
改变配置文件/etc/keepalived/keepalived.conf中virtual_router_id为另一个值即可。
(changed the vrid to another number and it worked fine).
5.2 常见错误二:
主机执行到VRRP_Instance(VI_1) Entering BACKUP STATE之后没有执行VRRP_Instance(VI_1)setting protocol VIPs。
解决方法:
可能是配置文件keepalived.conf中{左边没有空格。
附
linux添加tomcat服务
在/etc/init.d中输入 sudo vim tomcat
脚本具体内容如下: