为了节约公网地址,DR模型下VIP可用公网地址,RIP用内网地址,将网关都指向同一个路由器,以此来实现DR模型的LVS负载均衡


实验拓补图如下:

搭建VIP和RIP不在同一网段的DR模型的LVS_第1张图片


(1)模拟环境

192.168.1.X模拟为外网; 172.16.0.X模拟为内网

Client(Gateway):

eth0:192.168.1.15(Host-Only)

eth1:172.16.0.13(Host-Only)


Director:

VIP:192.168.1.10(eth0:0)

Gateway:172.16.0.13

DIP:172.16.0.8(eth0,Host-Only)


RS1:

RIP:172.16.0.11(eth0,Host-Only)

Gateway:172.16.0.13

VIP:192.168.1.10(lo:0)

搭建web服务:

# echo “RS1.ikki.com” > /var/www/html/index.html
# curl http://172.16.0.11/index.html


RS2:

RIP:172.16.0.12(eth0,Host-Only)

Gateway:172.16.0.13

VIP:192.168.1.10(lo:0)

搭建web服务:

# echo “RS2.ikki.com” > /var/www/html/index.html
# curl http://172.16.0.12/index.html


(2)配置RS1和RS2

# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
# ifconfig lo:0 192.168.1.10 netmask 255.255.255.255 broadcast 192.168.1.10
# route add -host 192.168.1.10 dev lo:0


(3)配置ipvs服务

Director:

# ifconfig eth0:0 192.168.1.10 netmask 255.255.255.0 up
# echo 1 > /proc/sys/net/ipv4/ip_forward
# ipvsadm -C
# ipvsadm -A -t 192.168.1.10:80 -s rr
# ipvsadm -a -t 192.168.1.10:80 -r 192.168.1.11 -g
# ipvsadm -a -t 192.168.1.10:80 -r 192.168.1.12 -g


(4)测试

Client:测试访问: 

# curl http://192.168.1.10/index.html

Director:查看负载状态:

# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.10:80 rr
 -> 172.16.0.12:80               Route   1      0          4   
 -> 172.16.0.11:80               Route   1      0          5