LVS的dr模型的工作原理

使用LVS的dr模型負載均衡兩臺web服務器。

條件:

    只有一個公網地址。172.10.0.9

    RIP使用私有地址

如下圖所示:

wKiom1QawQCSKuynAAKGcRcK5Jc775.jpg

路由器處理數據報文的流程

    用戶訪問集羣服務的數據報文到達路由器後,通過路由決策發現從路由器的 eth1 網卡可以到達:172.10.0.0/16網絡,(eth1 網卡配置有地址:172.10.0.1)路由器發現是本地網絡的通訊(基於MAC地址通信的),於是路由器向172.10.0.0/16網絡發送ARP地址解析廣播請求:請求172.10.0.9對應的MAC地址,在172.10.0.0/16網絡的所有主機都可以收到ARP地址解析請求。如果該網絡的某臺主機配置有路由器請求解析的IP地址,就把MAC地址告訴,路由器。路由器就會使用該MAC地址把數據報文發送給該主機。


LVS負載均衡原理:

    訪問集羣服務的請求都要先到達Director,由Director根據調度算法。把訪問集羣服務的請求均衡到後端的RealServer.

根據上述分析,要做下述一些配置:

1、請求報文流入

 因爲,我們的 Directory、Realserver1、Realserver2 都配置有VIP地址,所以基於本地通訊的原理

       和LVS負載均衡的工作原理考慮:

 目的:要控制路由器把用戶請求訪問集羣服務的數據報文只送到Director,

       不會把數據報文送到 RealServer.

 手段:通過控制無論任何時候路由器的ARP緩存中:VIP對應的MAC地址都是Director的VIP地址所在的

       網卡的MAC地址。這樣保證路由器一定會把數據報文送到Director

 方法:

   A)、在路由器做靜態地址綁定:
   B)、aprtables,阻止路由器發送的ARP地址解析請求(請求VIP對應的MAC)進入RealServer
   C)、修改ReserServer主機的內核參數,將RS上的VIP配置在lo接口的別名上,並限制其不能響應
        對VIP地址解析請求,也不能把MAC地址通告出去。

LVS的dr模型,Director通過修改請求訪問集羣服務的報文的MAC地址實現把數據報文分發到後端的RealServer.

 所以,要保證Director能夠基於MAC地址與後端的RealServer進行通信。

 方法:Director與RealServer在同一個物理網絡。


2、響應報文流出

當RealServer 構建完響應報文之後,直接把報文送給客戶端。

 因爲,客戶端請求的是VIP,所以,RealServer 構建的響應的數據報文的源IP是CIP(10.10.0.1)目標IP是VIP(172.10.0.9),客戶端纔會接收。

 所以,在RealServer中通過主機路由指定使用配置在lo:0的VIP對響應的數據報文進行封裝。再通過eth0把響應的數據報文送達客戶端。

由於RealServer的RIP是私有網絡,不能把數據報文送達客戶端。所以給RealServer指定一個默認網關,RealServer把不屬於本網絡的數據報文發給默認網關由默認網關把數據報文送達客戶端。

LVS工作流程如下圖:

wKioL1QaxgyAYwvFAAM_8kQI7YE526.jpg

一、配置路由:

1、配置通往 172.10.0.0/16 網絡路由

在路由器配置到達172.10.0.0/16網終的出口

[root@route ~]# ifconfig eth1 172.10.0.1 up
[root@route ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.10.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

添加一條網絡路由

到達 172.10.0.0/16 網絡從路由器的 eth1 接口172.10.0.1 出去。

也就是說,172.10.0.1 是與該網絡直接相連的(基於MAC地址通訊)。

[root@route ~]# route add -net 172.10.0.0/16 gw 172.10.0.1 eth1

查看配置網絡路由後

[root@route ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.10.0.0      172.10.0.1      255.255.0.0     UG    0      0        0 eth1  ------> 網絡路由。數據報文通過eth1接口可以到這172.10.0.0/16網絡
172.10.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1 -------> Gateway 網關是0.0.0.0  這是本地網絡,
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

2、再添加一條 RIP能夠到達10.10.0.1 的路由(Reral Server 通夠把響應的數據報文送達客戶端的路由)

在路由器配置到達192.168.60.0/24網絡的出口

[root@route ~]# ifconfig eth1:0 192.168.60.1 up

配置前

[root@route ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
172.10.0.0      172.10.0.1      255.255.0.0     UG    0      0        0 eth1
172.10.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

增加到達192.168.60.0/24網絡的路由

[root@route ~]# route add -net 192.168.60.0/24 gw 192.168.60.1 eth1:0

配置後

[root@route ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    192.168.60.1    255.255.255.0   UG    0      0        0 eth1
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
172.10.0.0      172.10.0.1      255.255.0.0     UG    0      0        0 eth1
172.10.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0

說明:

    Gateway 網關是0.0.0.0  這是本地網絡,

    Destination 爲: 0.0.0.0 說明這是默認網關。

路由器的地址配置如下:

[root@route ~]# ifconfig | grep -B 1 "[[:space:]]*inet[[:space:]]"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:15:5C
          inet addr:10.10.0.88  Bcast:10.255.255.255  Mask:255.0.0.0
--
eth1      Link encap:Ethernet  HWaddr 00:0C:29:79:15:66
          inet addr:172.10.0.1  Bcast:172.10.255.255  Mask:255.255.0.0
--
eth1:0    Link encap:Ethernet  HWaddr 00:0C:29:79:15:66
          inet addr:192.168.60.1  Bcast:192.168.60.255  Mask:255.255.255.0
--
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0

3、打開linux主機的轉發功能

[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward


二、配置Director

1、配置DIR地址

[root@director ~]# ifconfig eth0 192.168.60.4 up

2、配置VIP地址

[root@director ~]# ifconfig eth0:0 172.10.0.9 up

查看Director地IP地址配置情況

[root@director ~]# ifconfig  | grep -B 1 "[[:space:]]*inet[[:space:]]"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1B:03:49
          inet addr:192.168.60.4  Bcast:192.168.60.255  Mask:255.255.255.0
--
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:1B:03:49
          inet addr:172.10.0.9  Bcast:172.10.255.255  Mask:255.255.0.0
--
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0

由於後端RealServer構建響應的數據報文沒有經過Director,所以,Director無需配置任何路由條目。

[root@director ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
172.10.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

查看在路由器的arp 表

[root@route ~]# arp -e
Address                  HWtype  HWaddress           Flags Mask            Iface
10.10.0.1                ether   00:50:56:C0:00:01   C                     eth0
192.168.60.9             ether   00:0C:29:FC:C7:44   C                     eth1
192.168.60.4             ether   00:0C:29:1B:03:49   C                     eth1
172.10.0.9               ether   00:0C:29:1B:03:49   C                     eth1

說明:

    當一臺主機加入一個網絡時,它會向該網絡發出ARP通告。所以在路由器使用【arp -e】命令會查看到剛纔配置的地址對應的MAC地址。


三、配置 RealServer

通過在後端的每個RealServer配置內核參數的方式,保證路由器的ARP緩衝中只有Director的VIP的MAC對應條目。

ARP有兩種工作方式:

        (A)、ARP廣播解析請求,向網絡中發送ARP廣播請求解析IP地址對應的MAC地址,
             從而實現數據報文的傳輸的。
            arp_ignore = 1 請求解析的IP地址,恰好是配置在ARP廣播報文進來的網卡接口,
                           就響應ARP解析請求。
	(B)、一個主機加入一個網絡,該主機會發出ARP通告。自己的IP地址是什麼,自己的MAC地
	    址是什麼.由於在linux系統中IP地址屬於內核的,默認會把該主機的所有IP地址都通
	    告出去的。
            arp_announce = 2  僅通告與網絡直連的接口的地址(IP地址與MAC地址),

這樣就實現了,RealServer不能向外通告配置在lo:0的VIP地址,也不會響應對VIP的ARP地址解析請求。

1、配置 real2 server

(1)、配置RIP地址

[root@real2 ~]# ifconfig eth0 192.168.60.9 up

(2)、配置內核參數,限制網卡對ARP協議的行爲。

[root@real2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@real2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@real2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@real2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

(3)、配置VIP

[root@real2 ~]# ifconfig lo:0 172.10.0.9 netmask 255.255.255.255 broadcast  172.10.0.9 up

(4)、查看resl2的VIP是否向外通告了?

[root@route ~]# ping -c 1 172.10.0.9
PING 172.10.0.9 (172.10.0.9) 56(84) bytes of data.
64 bytes from 172.10.0.9: icmp_seq=1 ttl=64 time=0.227 ms

--- 172.10.0.9 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.227/0.227/0.227/0.000 ms
[root@route ~]# arp -e
Address                  HWtype  HWaddress           Flags Mask            Iface
10.10.0.1                ether   00:50:56:C0:00:01   C                     eth0
192.168.60.9             ether   00:0C:29:FC:C7:44   C                     eth1
192.168.60.4             ether   00:0C:29:1B:03:49   C                     eth1
172.10.0.9               ether   00:0C:29:1B:03:49   C                     eth1

從上面結果可以看出,沒有向外通告。

(5)、配置主機路由,使得RealServer構建響應報文使用配置在lo:0 的VIP地址作爲源地址封裝數據報文。在linux中,數據報文從那塊網卡出去,數據報文的源地址就是該網卡的IP地址

[root@real2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

配置主機路由

[root@real2 ~]# route  add  -host 10.10.0.9 dev lo:0

配置後

[root@real2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.0.9       0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

(6)、添置默認路由,讓RealServer能夠把數據包送到路由器,再由路由器送給客戶端。

[root@real2 ~]# route add default gw 192.168.60.1
[root@real2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.0.9       0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.60.1    0.0.0.0         UG    0      0        0 eth0

(7)、測試是不能夠PING通網關

[root@real2 ~]# ping -c 1 192.168.60.1
PING 192.168.60.1 (192.168.60.1) 56(84) bytes of data.
64 bytes from 192.168.60.1: icmp_seq=1 ttl=64 time=0.177 ms

--- 192.168.60.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.177/0.177/0.177/0.000 ms

(8)、在RealServer的配置

[root@real2 ~]# ifconfig | grep -B 1  "[[:space:]]*inet[[:space:]]"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:FC:C7:44
          inet addr:192.168.60.9  Bcast:192.168.60.255  Mask:255.255.255.0
--
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
--
lo:0      Link encap:Local Loopback
          inet addr:172.10.0.9  Mask:255.255.255.255

(9)、查看路由器的arp緩存,是否有RealServer的VIP---MAC條目

[root@route ~]# arp -e
Address                  HWtype  HWaddress           Flags Mask            Iface
10.10.0.1                ether   00:50:56:C0:00:01   C                     eth0
192.168.60.8             ether   00:0C:29:20:12:03   C                     eth1
192.168.60.9             ether   00:0C:29:FC:C7:44   C                     eth1
192.168.60.4             ether   00:0C:29:1B:03:49   C                     eth1
172.10.0.9               ether   00:0C:29:1B:03:49   C                     eth1


2、配置 real1 server

(1)、配置RIP地址

[root@real1 ~]# ifconfig eth0 192.168.60.8 up

(2)、配置內核參數,限制網卡對ARP協議的行爲。

[root@real1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@real1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@real1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@real1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

(3)、配置VIP

[root@real1 ~]# ifconfig lo:0 172.10.0.9 netmask 255.255.255.255 broadcast 172.10.0.9 up

(4)、查看resl2的VIP是否向外通告了?

[root@route ~]# ping -c 1 172.10.0.9
PING 172.10.0.9 (172.10.0.9) 56(84) bytes of data.
64 bytes from 172.10.0.9: icmp_seq=1 ttl=64 time=0.298 ms

--- 172.10.0.9 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.298/0.298/0.298/0.000 ms
[root@route ~]# arp -e
Address                  HWtype  HWaddress           Flags Mask            Iface
10.10.0.1                ether   00:50:56:C0:00:01   C                     eth0
192.168.60.8             ether   00:0C:29:20:12:03   C                     eth1
192.168.60.9             ether   00:0C:29:FC:C7:44   C                     eth1
172.10.0.9               ether   00:0C:29:1B:03:49   C                     eth1

從上面看出,real1沒有把VIP向通告

(5)、配置主機路由,使得RealServer構建響應報文使用配置在lo:0 的VIP地址作爲源地址封裝數據報文。在linux中,數據報文從那塊網卡出去,數據報文的源地址就是該網卡的IP地址

[root@real1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

添加主機路由,封裝響應報文

[root@real1 ~]# route add -host 172.10.0.9 dev lo:0
[root@real1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.10.0.9      0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

(6)、添置默認路由,讓RealServer能夠把數據包送到路由器,再由路由器送給客戶端CIP

[root@real1 ~]# route add default gw 192.168.60.1
[root@real1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.60.1    0.0.0.0         UG    0      0        0 eth0
172.10.0.9      0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

(7)、測試能夠PING通網關

[root@real1 ~]# ping -c 1  192.168.60.1
PING 192.168.60.1 (192.168.60.1) 56(84) bytes of data.
64 bytes from 192.168.60.1: icmp_seq=1 ttl=64 time=0.281 ms

--- 192.168.60.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.281/0.281/0.281/0.000 ms

(8)、在RealServer的配置

[root@real1 ~]# ifconfig | grep -B 1  "[[:space:]]*inet[[:space:]]"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:20:12:03
          inet addr:192.168.60.8  Bcast:192.168.60.255  Mask:255.255.255.0
--
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
--
lo:0      Link encap:Local Loopback
          inet addr:172.10.0.9  Mask:255.255.255.255


四、啓動RealServer的httpd服務

1、啓動real2

[root@real2 httpd-2.4.1]# ./bin/apachectl start
[root@real2 httpd-2.4.1]# netstat -apntl | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      2238/httpd

2、啓動real1

[root@real1 httpd-2.4.1]# ./bin/apachectl start
[root@real1 httpd-2.4.1]# netstat -anptl | grep "httpd"
tcp        0      0 :::80                       :::*                        LISTEN      2138/httpd

五、配置lvs 

1、定義集羣服務

[root@director ~]# ipvsadm -A -t 172.10.0.9:80 -s rr

2、向集羣服務添加RealServer

[root@director ~]# ipvsadm -a -t 172.10.0.9:80  -r 192.168.60.9:80 -g -w 1
[root@director ~]# ipvsadm -a -t 172.10.0.9:80  -r 192.168.60.8:80 -g -w 1

3、查看ipvs規則

[root@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  172.10.0.9:80 rr
  -> 192.168.60.8:80              Route   1      0          0
  -> 192.168.60.9:80              Route   1      0          0


六、訪問測試

wKiom1Qayg_CIcfLAAEpP9Locpg087.jpg

從上面訪問結果可以看出,LVS已經做訪問集羣調度了

一直刷新,查看LVS的狀態如下圖:

wKioL1QaypeAKc9KAAYSNGpBsno246.jpg

從lvs的調度狀態可以看出,LVS的dr模型已經可以完成調度了。

OK!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章