通过直接路由实现虚拟服务器(VS/DR)

跟VS/TUN方法相同,VS/DR利用大多数Internet服务的非对称特点,负载调度器中只负责调度请求,而服务器直接将响应返回给客户,可以极大地提高整个集群系统的吞吐量。VS/DR的体系结构如图所示:调度器和服务器组都必须在物理上有一个网卡通过不分断的局域网相连,如通过高速的交换机或者HUB相连。VIP地址为调 度器和服务器组共享,调度器配置的VIP地址是对外可见的,用于接收虚拟服务的请求报文;所有的服务器把VIP地址配置在各自的Non-ARP网络设备 上,它对外面是不可见的,只是用于处理目标地址为VIP的网络请求。

wKioL1V28duDkVJbAAGzkyn5zTQ578.jpg



DR基本架构实现


环境准备:

设备
软件环境
网络环境
Director_server(dirdr)
CentOS+IPVS
rip eth0_192.168.1.121
vip eth0:0_192.168.1.200
real_server_a(rsa)
CentOS
rip eth0_192.168.1.118
vip lo:0_192.168.1.200
real_server_b(rsb)
CentOS
rip eth0_192.168.1.119
vip lo:0_192.168.1.200


操作方案:

# 在dir上配置
[root@dir ~]# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.1.200
rs1=192.168.1.118
rs2=192.168.1.119
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1

[root@dir ~]# chmod a+x /usr/local/sbin/lvs_dr.sh
[root@dir ~]# /bin/bash /usr/local/sbin/lvs_dr.sh
[root@dir ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:80 rr
  -> 192.168.1.118:80             Route   1      0          0
  -> 192.168.1.119:80             Route   1      0          0

[root@dir ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:3E:24
          inet addr:192.168.1.121  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe55:3e24/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:700 errors:0 dropped:0 overruns:0 frame:0
          TX packets:474 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:68287 (66.6 KiB)  TX bytes:76635 (74.8 KiB)
          Interrupt:18 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:55:3E:24
          inet addr:192.168.1.200  Bcast:192.168.1.200  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)



# 在rsa与rsb上配置
[root@rsa ~]# vim /usr/local/sbin/lvs_dr_rs.sh
#! /bin/bash
vip=192.168.1.200
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip 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

[root@rsa ~]# chmod a+x /usr/local/sbin/lvs_dr_rs.sh
[root@rsa ~]# /bin/bash /usr/local/sbin/lvs_dr_rs.sh
[root@rsa ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:56:5C:C5
          inet addr:192.168.1.118  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe56:5cc5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:549 errors:0 dropped:0 overruns:0 frame:0
          TX packets:424 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:55100 (53.8 KiB)  TX bytes:61251 (59.8 KiB)
          Interrupt:18 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:160 (160.0 b)  TX bytes:160 (160.0 b)

lo:0      Link encap:Local Loopback
          inet addr:192.168.1.200  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1


浏览器测试:

wKioL1V3AfexagjeAACAsWoBmRw122.jpg


wKiom1V3AGDSom3aAACIdgLgNqE124.jpg




在Linux客户端测试效果

[root@client ~]# curl 192.168.1.200
This is test page,this is A.
[root@client ~]# curl 192.168.1.200
This is test page,this is B.
[root@client ~]# curl 192.168.1.200
This is test page,this is A.
[root@client ~]# curl 192.168.1.200
This is test page,this is B.
[root@client ~]# curl 192.168.1.200
This is test page,this is A.
[root@client ~]# curl 192.168.1.200
This is test page,this is B.