准备三台虚拟,均为CentOS6.5 x86_64
注意,配置过程中,保持端口的一致性。
director (eth0 192.168.1.189, vip eth0:0: 192.168.1.18) Director 78
real server1 (eth0 192.168.1.207, vip lo:0: 192.168.1.18) Server1 78
real server2 (eth0 192.168.1.208, vip lo:0: 192.168.1.18) Server2 78
1、配置静态ip、安装测试httpd (两台realserver都要做)
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# echo "Server1_aaa111"> /var/www/html/index.html 写入数据
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] ~]# curl 192.168.1.207:78 (修改端口)单个测试结果,也可web端测试
Server1_aaa111
[[email protected] ~]# curl 192.168.1.208:78
Server2_bbb222
2、ipvsadm安装和测试(director)
[[email protected] ~]# yum list |grep ipvsadm
ipvsadm.x86_64 1.26-4.el6
[[email protected] ~]# yum install -y ipvsadm
[[email protected] ~]# ipvsadm 加载 ipvsadm ,也可以使用 modprobe ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[[email protected] ~]# lsmod |grep ip_vs 查看内核模块,有输出则代表 ipvsadm 已经加载进内核当中,此时 LVS 安装完毕
3、DR新增配置文件/usr/local/sbin/lvs_dr.sh
[[email protected] ~]# vim /usr/local/sbin/lvs_dr.sh 详细配置见附件
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up 新增eth0:0虚拟网卡
/sbin/ipvsadm -A -t $VIP:78 -s rr rr轮询方式
/sbin/ipvsadm -a -t $VIP:78 -r $RIP1:78 -g g代表DR模式
[[email protected] ~]# sh /usr/local/sbin/lvs_dr.sh
[[email protected] ~]# ipvsadm -l
4、RS配置文件/usr/local/sbin/lvs.sh
两台rs上需要做如下配置。
新建配置文件:/usr/local/sbin/lvs.sh,使用lo端口做虚拟ip配置,并添加路由,指定特定的网络出口
[[email protected] ~]# vim /usr/local/sbin/lvs.sh
[[email protected] ~]# sh /usr/local/sbin/lvs_rs.sh
查看,多出一个lo:0端口和ip地址。
5、测试结果
后端通过curl测试,三台均正常显示。
注意
web端测试 :http://192.168.1.18:78/
虚拟ip测试结果:注意该地址有缓存,要多尝试几次,可以实现没刷新一次切换一次。
6、关机重启之后,要再次执行脚本,关闭防火墙。
a、检查各个ip地址
b、检查防火墙
c、检查Director是否加载
7、附件
[[email protected] sbin]# cat /usr/local/sbin/lvs_dr.sh
#!/bin/sh
# description: Start LVS of Director server
VIP=192.168.1.18
RIP1=192.168.1.207
RIP2=192.168.1.208
source /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of Director Server"
# set the Virtual IP Address and sysctl parameter
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
echo "1" >/proc/sys/net/ipv4/ip_forward
#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
/sbin/ipvsadm -A -t $VIP:78 -s rr
/sbin/ipvsadm -a -t $VIP:78 -r $RIP1:78 -g
/sbin/ipvsadm -a -t $VIP:78 -r $RIP2:78 -g
#Run LVS
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
-------------------------------------------------------------
[[email protected] ~]# cat /usr/local/sbin/lvs.sh
#!/bin/bash
#description: RealServer‘s script
VIP=192.168.1.18
source /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac