1、安装lvs:
依次安装libnl-devel-1.1.4-2.el6.x86_64.rpm、popt-devel-1.13-7.el6.x86_64.rpm、popt-static-1.13-7.el6.x86_64.rpm三个rpm包。
2、tar -xvf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
make;make install
3、配置director服务器:
1)mkdir /usr/local/lvs
2) vi /usr/local/lvs/directorserver
3) 编写启动脚本:
VIP=【vip】
RIP1=【realserverip】
RIP2=【realserverip】
RIP3=【realserverip】
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of DirectorServer"
#Set the Virtual IP Address
/sbin/ifconfig eth2:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth2:0
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -g -w 1
#Run Lvs
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
4)chmod 755 /usr/local/lvs/directorserver;cd /usr/local/lvs/directorserver.sh;./directorserver start
5)ipvsadm --set 5 100 300
4、配置realserver服务器:
1)mkdir /usr/local/lvs
2) vi /usr/local/lvs/realserver
3) 编写脚本:
RS_VIP=【vip】
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $RS_VIP netmask 255.255.255.255 broadcast $RS_VIP
/sbin/route add -host $RS_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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $RS_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
exit 0
4)chmod 755 /usr/local/lvs/realserver;cd /usr/local/lvs/;./realserver start
注意:
*在启动docker容器的时候必须把lvs的vip及端口和realserver的rip及端口都隐射给容器,否则lvs按dr模式启动过后是无法转发成功的。
*在docker容器所在的宿主机,必须要保证iptables服务启动正常,且转发的开关已经打开(参考docker安装文档)。
依次安装libnl-devel-1.1.4-2.el6.x86_64.rpm、popt-devel-1.13-7.el6.x86_64.rpm、popt-static-1.13-7.el6.x86_64.rpm三个rpm包。
2、tar -xvf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
make;make install
3、配置director服务器:
1)mkdir /usr/local/lvs
2) vi /usr/local/lvs/directorserver
3) 编写启动脚本:
VIP=【vip】
RIP1=【realserverip】
RIP2=【realserverip】
RIP3=【realserverip】
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of DirectorServer"
#Set the Virtual IP Address
/sbin/ifconfig eth2:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth2:0
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -g -w 1
#Run Lvs
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
/sbin/ipvsadm -C
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
4)chmod 755 /usr/local/lvs/directorserver;cd /usr/local/lvs/directorserver.sh;./directorserver start
5)ipvsadm --set 5 100 300
4、配置realserver服务器:
1)mkdir /usr/local/lvs
2) vi /usr/local/lvs/realserver
3) 编写脚本:
RS_VIP=【vip】
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $RS_VIP netmask 255.255.255.255 broadcast $RS_VIP
/sbin/route add -host $RS_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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $RS_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
exit 0
4)chmod 755 /usr/local/lvs/realserver;cd /usr/local/lvs/;./realserver start
注意:
*在启动docker容器的时候必须把lvs的vip及端口和realserver的rip及端口都隐射给容器,否则lvs按dr模式启动过后是无法转发成功的。
*在docker容器所在的宿主机,必须要保证iptables服务启动正常,且转发的开关已经打开(参考docker安装文档)。