LVS 小实验实现

nat模式 

在负载均衡服务器端有两块网卡。。。192.168.1.142和192.168.142.1

192.168.142.2和192.168.142.3为web服务器

yum install -y ipvsadm

net.ipv4.ip_forward = 1写到    vi /etc/sysctl.conf 

ipvsadm -A -t 192.168.1.142:80 -s rr
ipvsadm -a -t 192.168.1.142:80 -r 192.168.142.2:80 -m -w 1
ipvsadm -a -t 192.168.1.142:80 -r 192.168.142.3:8080 -m -w 1
ipvsadm -L -n
ipvsadm-save -n > /opt/lvsnat.sh

ipvsadm-restore < /opt/lvsnat.sh
cat /opt/lvsnat.sh
ipvsadm --set 1 5 300

curl 192.168.1.142:80

 

 

 

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

tun 隧道模式  实现 

lvs server 192.168.1.142     

集群192.168.1.148和192.168.1.144

vip为192.168.1.145

 

使用脚本实现

server 端

vi server.sh

echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/ifconfig tunl0 192.168.1.145 broadcast 192.168.1.145 netmask 255.255.255.255 up
/sbin/route add -host 192.168.1.145 dev tunl0
/sbin/ipvsadm -A -t 192.168.1.145:80 -s wrr
/sbin/ipvsadm -a -t 192.168.1.145:80 -r 192.168.1.148:80 -i -w 1
/sbin/ipvsadm -a -t 192.168.1.145:80 -r 192.168.1.144:80 -i -w 1

 

 

client端为

/sbin/ifconfig tunl0 up
/sbin/ifconfig tunl0 192.168.1.145 broadcast 192.168.1.145 netmask 255.255.255.255 up
echo 1 > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/tunl0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter

/sbin/route add -host 192.168.1.145 dev tunl0

 

 

 

测试访问192.168.1.145

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

路由模式

脚本

server:

/sbin/ifconfig eth0:0 192.168.1.145 broadcast 192.168.1.145 netmask 255.255.255.255 up
route add -host 192.168.1.145 dev eth0:0
ipvsadm -C
/sbin/ipvsadm -A -t 192.168.1.145:80 -s wrr
/sbin/ipvsadm -a -t 192.168.1.145:80 -r 192.168.1.148:80 -g -w 1
/sbin/ipvsadm -a -t 192.168.1.145:80 -r 192.168.1.144:80 -g -w 1

 

 

client:

/sbin/ifconfig lo:0 192.168.1.145 broadcast 192.168.1.145 netmask 255.255.255.255 up
route add -host 192.168.1.145 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

 

 

访问192.168.1.145测试

 

 

posted @ 2017-02-21 11:17  *奥特*  阅读(235)  评论(0编辑  收藏  举报