DIP:172.28.95.220
VIP:172.28.95.245
RIP:172.28.95.221(222)
负载调度器:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@LVS-GS001 ipvsadm-1.24]# ifconfig eth1:1 172.28.95.245 netmask 255.255.255.0 up [root@LVS-GS001 ipvsadm-1.24]# ifconfig eth1 Link encap:Ethernet HWaddr 00:15:5D:C7:83:7D inet addr:172.28.95.220 Bcast:172.28.95.255 Mask:255.255.240.0 eth1:1 Link encap:Ethernet HWaddr 00:15:5D:C7:83:7D inet addr:172.28.95.245 Bcast:172.28.95.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:9 Base address:0xc000 [root@LVS-GS001 ipvsadm-1.24]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@LVS-GS001 ipvsadm-1.24]# ipvsadm --help [root@LVS-GS001 ipvsadm-1.24]# ipvsadm --set 30 5 60 #--set tcp tcpfin udp set connection timeout values [root@LVS-GS001 ipvsadm-1.24]# ipvsadm -A -t 172.28.95.245:80 -s wrr -p 20 #-A add virtual service with options #-s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc. [root@LVS-GS001 ipvsadm-1.24]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.28.95.245:80 wrr persistent 20 [root@LVS-GS001 ipvsadm-1.24]# ipvsadm -a -t 172.28.95.245:80 -r 172.28.95.221:80 -g -w 1 [root@LVS-GS001 ipvsadm-1.24]# ipvsadm -a -t 172.28.95.245:80 -r 172.28.95.222:80 -g -w 1 [root@LVS-GS001 ipvsadm-1.24]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.28.95.245:80 wrr persistent 20 -> 172.28.95.222:80 Route 1 0 0 -> 172.28.95.221:80 Route 1 0 0 |
脚本实现:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org VIP=( 22.22.22.22 ) RIP=( 44.44.44.44 ) start() { # Analyzing lvs if [ `ipvsadm -Ln | wc -l` -gt 3 ]; then ipvsadm -C fi # config eth0 and vip for ((i=0;i<${#VIP[*]};i++)) do ifconfig eth0:$i ${VIP[$i]}/24 up #route add -host ${VIP} dev eth0 ipvsadm --set 30 5 60 ipvsadm -A -t ${VIP[$i]}:80 -s wrr # config rip for ((p=0;p<${#RIP[*]};p++)) do ipvsadm -a -t ${VIP[$i]}:80 -r ${RIP[$p]}:80 -g -w 1 done done } stop() { for ((i=0;i<${#VIP[*]};i++)) do ifconfig eth0:$i ${VIP[$i]}/24 down #route add -host ${VIP} dev eth0 #ipvsadm --set 30 5 60 ipvsadm -D -t ${VIP[$i]}:80 done } case "$1" in start) start && exit 0 ;; stop) stop || exit 2 ;; *) echo "USAGE: $0 start|stop" ;; esac |
Real-Server:
0 1 2 3 4 |
[root@RealServer001 ~]# ifconfig lo:221 172.28.95.245:80 netmask 255.255.255.255 up [root@RealServer001 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore [root@RealServer001 ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce [root@RealServer001 ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore [root@RealServer001 ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org VIP=( 22.22.22.22 ) start() { if [ `ifconfig | grep 65536 | wc -l` -gt 1 ]; then echo "Loopback interface has been configured" exit 1 fi for ((i=0;i<${#VIP[@]};i++)) do ifconfig lo:$i ${VIP[$i]}/32 up # route add -host ${VIP[$i]} dev lo done 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 } stop() { if [ `ifconfig | grep lo | wc -l` -eq 1 ]; then echo "Loopback interface is not configured" exit 1 fi for ((i=0;i<${#VIP[@]};i++)) do ifconfig lo:$i down done 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 } case "$1" in start) start && exit 0 ;; stop) stop || exit 2 ;; *) echo "Usage: $0 start|stop" ;; esac |
调度器和服务器组都必须在物理上有一个网卡通过不分断的局域网相连,如通过高速的交换机或者HUB相连。VIP地址为调度器和服务器组共享,调度 器配置的VIP地址是对外可见的,用于接收虚拟服务的请求报文;所有的服务器把VIP地址配置在各自的Non-ARP网络设备上,它对外面是不可见的,只 是用于处理目标地址为VIP的网络请求。
它的连接调度和管理与VS/NAT和VS/TUN中的一样,它的报文转发方法又有不同,将报文直接路由给目标服务器。在VS/DR 中,调度器根据各个服务器的负载情况,动态地选择一台服务器,不修改也不封装IP报文,而是将数据帧的MAC地址改为选出服务器的MAC地址,再将修改后 的数据帧在与服务器组的局域网上发送。因为数据帧的MAC地址是选出的服务器,所以服务器肯定可以收到这个数据帧,从中可以获得该IP报文。当服务器发现 报文的目标地址VIP是在本地的网络设备上,服务器处理这个报文,然后根据路由表将响应报文直接返回给客户。 SourceByrd's Weblog-https://note.t4x.org/system/linux-virtual-server-direct-routing/
官方文档:http://www.linuxvirtualserver.org/zh/lvs3.htmlSourceByrd's Weblog-https://note.t4x.org/system/linux-virtual-server-direct-routing/ SourceByrd's Weblog-https://note.t4x.org/system/linux-virtual-server-direct-routing/