lvs的三种模式:
NAT模式
DR 直接路由
TUN 隧道模式
本文使用DR模式。
## lvs----架构,--
DR模式
public---router------ld---(1,realserver01; 2,realserver 02)
使用四台虚拟机实验,全部桥接,-----真实机器作为client实验
client: 10.212.182.2 gw: 10.212.182.1
router eth0: 10.212.182.1 ,eth1 : 10.212.183.1
ld eth0: 10.212.183.2 eth0:0 10.212.183.3
realserver01: 10.212.183.4 lo:0 10.212.183.3
realserver02: 10.212.183.5 lo:0 10.212.183.3
---------------------------------------------
client:
ifconfig eth0 10.212.182.2
route add default gw 10.212.182.1
----------------------
router:
ifconfig eth0 10.212.182.1 netmask 255.255.255.0 broadcast 10.212.182.255 up
ifconfig eth1 10.212.183.1 netmask 255.255.255.0 broadcast 10.212.183.255 up
开启包转发
echo 1 > /proc/sys/net/ipv4/ip_forward
---------------------------------------
ld:
ifconfig eth0 10.212.183.2 netmask 255.255.255.0 broadcast 10.212.183.255 up
ifconfig eth0:0 10.212.183.3 netmask 255.255.255.255 broadcast 10.212.183.3 up
同样开启包转发
echo 1 > /proc/sys/net/ipv4/ip_forward
先增加服务
ipvsadm -A -t 10.212.183.3:80 -s rr
-A 添加服务 -t 服务所使用的虚拟IP地址:端口 (虚拟地址工业环境应该是公网ip) -s 算法 (rr 算法就是你一个,我一个)
把这个服务和后面的server pool进行关联
ipvsadm -a -t 10.212.183.3:80 -r 10.212.183.4 -g
ipvsadm -a -t 10.212.183.3:80 -r 10.212.183.5 -g
-a 添加real server -r real server的ip -g (DR模式) -m是NAT模式
######-----------------------------------
realserver01----------------------
所有realserver的配置脚本实现,当然首先需要安装httpd
#!/bin/bash
#written by booduklee
# time: 2011-06-27
# this is used to configure realserver in the lvs
echo "please linuxjishu/9952.html target=_blank class=infotextkey>mount your cd to /mntn"
yum=/etc/yum.repos.d
VIP=10.212.183.3
ROUTE=10.212.183.1
mkdir /tmp/repo_bak
find $YUM -name "*.repo" -exec mv {} /tmp/repo_bak ;
cat >> $YUM/server.repo << !
[base]
name=server
baseurl=file:///mnt/Server
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
!
yum clean all
yum install httpd -y
rm -rf $YUM/server.repo
mv /tmp/repo_bak/* $YUM
rm -rf /tmp/repo_bak
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
route add -host $VIP dev lo:0
route add default gw $ROUTE
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "VIP start"
;;
stop)
ifconfig lo:0 down
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_announce
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "VIP stop"
;;
*)
echo "USAGE start | stop"
exit 1
;;
esac
---------------------测试---------------------------
client#elinks http://10.212.183.3/index.html --dump
hello,this is realserver02
client#elinks http://10.212.183.3/index.html --dump
hello ,this is realserver01
client#elinks http://10.212.183.3/index.html --dump
hello,this is realserver02
client#elinks http://10.212.183.3/index.html --dump
hello ,this is realserver01