1. 主服务器(master):虚拟IP-192.168.13.120
2. 备份服务器(backup):虚拟IP-192.168.13.120
3. 真实服务器1(real1):192.168.13.102
4. 真实服务器2(real2):192.168.13.105
5. 真实服务器3(real3):192.168.13.107
( 测试可以省去备份服务器以及真实服务器 2 、 3)主备和真实服务器的真实IP不重要,一下所写真实IP均为了举例方便,在相同网段即可本文所有内容的下载地址(ipvsadm,keepalived,keepalived.config,lvsdr.sh,realdr.sh)如下:
链接: https://pan.baidu.com/s/1qYVhhF2 密码: mtj9
# modprobe –l | grep ipvs
如果在/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs下有很多ip_vs*这样的文件说明ipvs模块已经加入,如果没有,请自行升级内核T T从下载内容包中找到ipvsadm-1.24.tar.gz,执行如下命令(运行命令出错的说明编译环境存在问题,请更新下载GCC)
# tar zxvf ipvsadm-1.24.tar.gz
# cd ipvsadm-1.24
# make && make install
# whereis ipvsadm
如果存在如下图的数据信息则说明安装成功从下载内容包中找到ipvsadm-1.24.tar.gz,执行如下命令(运行命令出错的说明编译环境存在问题,请更新下载GCC)
#tar -zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure --prefix=/usr/local/keepalived
#make;make install
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/本脚本的主要作用是在主备服务器上创建虚拟IP,并通过该虚拟IP+使用端口路由到真实服务器IP上,其中命令”/sbin/ipvsadm -C”是清理路由表
注:由于本人项目使用了80 3131 5151三个端口,所以配置了三个,实际使用请按需增删
#vim /sbin/lvsdr.sh
!/bin/bash VIP=192.168.13.120 RIP1=192.168.13.102 RIP2=192.168.13.107 RIP3=192.168.13.105 /etc/rc.d/init.d/functions case "$1" in start) echo "start LVS of DirectorServer" #Set the Virtual IP Address /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth0:1 #Clear IPVS Table /sbin/ipvsadm -C #Set Lvs Add route link /sbin/ipvsadm -A -t $VIP:80 -s wrr -p 60 /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g /sbin/ipvsadm -a -t $VIP:80 -r $RIP3 -g /sbin/ipvsadm -A -t $VIP:3131 -s wrr -p 60 /sbin/ipvsadm -a -t $VIP:3131 -r $RIP1 -g /sbin/ipvsadm -a -t $VIP:3131 -r $RIP2 -g /sbin/ipvsadm -a -t $VIP:3131 -r $RIP3 -g /sbin/ipvsadm -A -t $VIP:5151 -s wrr -p 60 /sbin/ipvsadm -a -t $VIP:5151 -r $RIP1 -g /sbin/ipvsadm -a -t $VIP:5151 -r $RIP2 -g /sbin/ipvsadm -a -t $VIP:5151 -r $RIP3 -g #Run Lvs /sbin/ipvsadm ;; stop) echo "Close LVS Directorserver" /sbin/ifconfig eth0:1 down /sbin/ipvsadm -C ;; *) echo "Usage0{start|stop}" exit 1 esac· 分配权限
#/sbin/lvsdr.sh stop
#ipvsadm –ln
要完成主备虚拟IP映射,必须在真实服务器上运行下面的脚本启动,建议将该脚本加入启动项中开机启动,不然每次重启都要运行该脚本。需要注意:执行servicenetwork restart命令后需要重新运行该脚本
#vim /sbin/realdr.sh
!/bin/bash VIP=192.168.13.120 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 echo "1">/proc/sys/net/ipv4/conf/default/arp_ignore echo "2">/proc/sys/net/ipv4/conf/default/arp_announce echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore echo "2">/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p` 分配权限
该配置文件主要核心是监听我们指定其监听的端口,实时监测各个真实服务器(real1,real2,real3)的指定端口是否能连通,如果连接异常则将其从路由表中剔除,如果恢复则重新加入到路由表中。
详细参数说明请参见官网或其他资料,其中state MASTER参数在备份服务器中要改为state BACKUP。
#vim /etc/keepalived/keepalived.conf!Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.13.120 } } virtual_server 192.168.13.120 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 60 inhibit_on_failure protocol TCP real_server 192.168.13.102 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.107 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.105 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 192.168.13.120 3131 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 60 protocol TCP real_server 192.168.13.102 3131 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.107 3131 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.105 3131 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 192.168.13.120 5151 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 60 protocol TCP real_server 192.168.13.102 5151 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.107 5151 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.13.105 5151 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } }
*@!不要忘记先把路由脚本lvsdr.sh启动起来
#/sbin/lvsdr.sh start
#/etc/rc.d/init.d/keepalived start
查看此时路由状态,执行命令ipvsadm –ln,得到下图,从图中列表可以看出只有real1(IP 192.168.13.102)是连通的多进行几次测试就会知道其原理了,从而可以对配置文件进行更合适的修改
【主备服务器】
#vim /etc/rc.local
添加如下内容:
/etc/rc.d/init.d/keepalived restart
/sbin/lvsdr.sh start
【真实服务器】
#vim /etc/rc.local
添加如下内容:
添加/sbin/realdr.sh start