負載均衡群集工作模式–直接路由(Direct Routing)
簡稱DR模式,採用半開放式的網路結構,與TUN模式的結構類似,但各節點並不是分散在各地,而是與排程器位於同一個物理網路
負載排程器與各節點伺服器通過本地網路連線,不需要建立專用的IP隧道
(1)
為方便進行原理分析,將 Client與群集機器放在同一網路中,封包流經的路線為1-2-3-4
(2)
Client向目標VIP發出請求, Director(負載均衡器)接收。此時IP包頭及資料框頭資訊為:
(3)
Director根據負載均衡演演算法選擇 Realserver_1,不修改也不封裝IP報文,而是將資料框的MAC地址改為RealServer_1的MAC地址,然後在區域網上傳送。IP包頭及資料框頭資訊如下:
(4)
RealServer_1收到這個幀,解封裝後發現目標IP與本機匹配( RealServer事先繫結了VIP),於是處理這個報文。隨後重新封裝報文,傳送到區域網。此時IP包頭及資料框頭資訊為:
(5)
Client將收到回覆報文。 Client認為得到正常的服務,而不會知道是哪一臺伺服器處理的
注意:如果跨網段,那麼報文通過路由器經由 terne返回給使用者
(1)
在LVS-DR負載均衡叢集中,負載均衡器與節點伺服器都要設定相同的VIP地址
(2)
在區域網中具有相同的IP地址,勢必會造成各伺服器ARP通訊的紊亂
當一個ARP廣播傳送到LVS-DR叢集時,因為負載均衡器和節點伺服器都是連線到相同的網路上的,它們都會接收到ARP廣播
此時只有前端的負載均衡器進行響應,其他節點伺服器不應該響應ARP廣播
(3)
對節點伺服器進行處理,使其不響應針對VIP的ARP請求
使用虛介面lo:0承載ⅥP地址
設定核心引數 arp_ignore=1:系統只響應目的IP為本地IP的ARP請求
(4)
RealServe返回報文(源IP是VIP)經路由器轉發,在重新封裝報文時需要先獲取路由器的MAC地址
傳送ARP請求時, Linux預設使用IP包的源IP地址(即VIP)作為ARP請求包中的源IP地址,而不使用傳送介面(例如ens33)的IP地址
(5)
路由器收到ARP請求後,將更新ARP表項
原有的VIP對應 Director的MAC地址會被更新為ⅥP對應RealServer的MAC地址
(6)
此時新來的請求報文,路由器根據ARP表項,會將該報文轉發給RealServer,從而導致 Director的VIP失效
修改/etc/sysctl.conf檔案
對節點伺服器進行處理,使其不響應針對VIP的ARP請求
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
系統不使用IP包的源地址來設定ARP請求的源地址,而選擇傳送介面的IP地址
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
VMware軟體
一臺centos7作為LVS,並採用虛介面。ip地址分別為192.168.100.21 ; 192.168.100.100
兩臺centos7作為Apache伺服器,ip地址分別為192.168.100.22 ;192.168.100.23
一臺centos7作為NFS儲存,ip地址為192.168.100.24
注意:ip地址網路部分設定出現問題分可留言
真機存取192.168.100.100的網址,通過DR模式漂移地址,輪詢的存取到Apache1和Apache2主機
搭建出nfs網路檔案儲存服務,實驗負載均衡
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.255
[root@localhost network-scripts]# ifup ifcfg-ens33:0
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.21 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::3069:1a3d:774b:18f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:11:0d:16 txqueuelen 1000 (Ethernet)
RX packets 1170 bytes 110973 (108.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 683 bytes 81523 (79.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.100 netmask 255.255.255.255 broadcast 192.168.100.100
ether 00:0c:29:11:0d:16 txqueuelen 1000 (Ethernet)
省略部分內容
對於 DR 群集模式來說,由於 LVS 負載排程器和各節點需要共用 VIP 地址,應該關閉 Linux 核心的重定向引數響應
伺服器不是一臺路由器,那麼它不會傳送重定向,所以可以關閉該功能
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost network-scripts]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]#yum -y install ipvsadm
[root@localhost /]# ipvsadm -v
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
[root@localhost ~]# ipvsadm -A -t 192.168.100.100:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.22:80 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.23:80 -g -w 1
[root@localhost ~]# ipvsadm-save > /opt/ipvsadm
[root@localhost ~]# cat /opt/ipvsadm
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 192.168.100.22:http -g -w 1
-a -t localhost.localdomain:http -r 192.168.100.23:http -g -w 1
首先檢視nfs-utils 和rpcbind 是否安裝,若沒有用yum安裝即可
安裝好後啟動兩個服務
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24(rw,sync)
/opt/52xit 192.168.100.0/24(rw,sync)
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# echo "this is www.51xit.top" > /opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.top" > /opt/52xit/index.html
防火牆和核心防護均關閉,檢視是否安裝nfs-utils
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig
省略部分內容
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
省略部分內容
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.100 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p
前面設定兩臺節點伺服器都一樣
下面分別掛載兩臺節點伺服器
[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /var/www/html/
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 6 12:23:03 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=a1c935eb-f211-43a5-be35-2a9fef1f6a89 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
192.168.100.24:/opt/51xit/ /var/www/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start httpd
測試登入是否正常
[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.24:/opt/52xit /var/www/html/
[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 6 12:23:03 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=a1c935eb-f211-43a5-be35-2a9fef1f6a89 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/cdrom /mnt iso9660 defaults 0 0
192.168.100.24:/opt/52xit/ /var/www/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start httpd
測試登入是否正常
真機瀏覽器輸入192.168.100.100
重新輸入一遍
驗證成功