1. 程式人生 > >LVS 負載均衡叢集部署之 DR 模式

LVS 負載均衡叢集部署之 DR 模式

一、DR 模式工作原理

         image

如圖,LVS-DR的工作原理,在圖中已經說明,下面,我們來列舉 LVS-DR 模式特點:

1、RIP 可以使用私有地址,也可以使用公網地址,如果使用公網地址,則可以直接對 RIP 進行訪問。

2、所有請求的資料包必須經過 Director Server,但是所有響應的資料包必須不能經過 Director Server,這樣使得在高負載網路環境中,降低了 Director Server 的壓力。

3、Real Server 伺服器的閘道器絕對不能指定為 DIP,因為,響應資料包不需要經過 Director Server。

4、Real Server 和 Director Server 必須在同一物理網路中。

5、需要在 Real Server 的 lo 網絡卡上繫結 VIP。

二、LVS-DR 模式部署

1、IP 地址分配

image

2、Real ServerA 伺服器配置

a、VIP 繫結

[[email protected] ~]# cd /etc/sysconfig/network-scripts/

[[email protected] network-scripts]# cp ifcfg-lo{,:0}

[[email protected] network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0

IPADDR=192.168.20.134

NETMASK=255.255.255.255

NETWORK=192.168.20.134

BROADCAST=192.168.20.134

ONBOOT=yes

NAME=lo:0

[[email protected] network-scripts]# ifup ifcfg-lo:0

[[email protected] network-scripts]# ifconfig

如圖:

image

b、修改核心引數,使得客戶端傳送到 VIP 的請求只有排程伺服器響應,Real Server A(web伺服器)不要響應。

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.all.arp_ignore

[[email protected] ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.lo.arp_ignore

[[email protected] ~]# echo "net.ipv4.conf.lo.arp_ignore = 1" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.all.arp_announce

[[email protected] ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.lo.arp_announce

[[email protected] ~]# echo "net.ipv4.conf.lo.arp_announce = 2" >> /etc/sysctl.conf

[[email protected] ~]# sysctl –p

如圖:

image

3、Real ServerB 伺服器配置:

a、VIP 繫結

[[email protected] ~]# cd /etc/sysconfig/network-scripts/

[[email protected] network-scripts]# cp ifcfg-lo{,:0}

[[email protected] network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0

IPADDR=192.168.20.134

NETMASK=255.255.255.255

NETWORK=192.168.20.134

BROADCAST=192.168.20.134

ONBOOT=yes

NAME=lo:0

[[email protected] network-scripts]# ifup ifcfg-lo:0

[[email protected] network-scripts]# ifconfig

如圖:

image

b、修改核心引數,使得客戶端傳送到 VIP 的請求只有排程伺服器響應,Real ServerB (web伺服器)不要響應。

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.all.arp_ignore

[[email protected] ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.lo.arp_ignore

[[email protected] ~]# echo "net.ipv4.conf.lo.arp_ignore = 1" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.all.arp_announce

[[email protected] ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf

[[email protected] ~]# sysctl -a | grep net.ipv4.conf.lo.arp_announce

[[email protected] ~]# echo "net.ipv4.conf.lo.arp_announce = 2" >> /etc/sysctl.conf

[[email protected] ~]# sysctl –p

如圖:

image

說明,以上4個核心引數的含義為:

arp_ignore 定義了對目標地址為本機 IP 的  ARP 詢問的不同應答模式。

         0:響應任意網絡卡上接收到的對本機IP地址的arp請求(包括環回網絡卡上的地址),而不管該目的IP是否在接收網絡卡上。

         1:只響應目的IP地址為接收網絡卡上的本地地址的arp請求。

         2:只響應目的IP地址為接收網絡卡上的本地地址的arp請求,並且arp請求的源IP必須和接收網絡卡同網段。

         3:如果ARP請求資料包所請求的IP地址對應的本地地址其作用域(scope)為主機(host),則不迴應ARP響應資料包,如果作用域為全域性(global)或鏈路(link),則迴應ARP響應資料包。

         4~7:預留。

         8:不迴應所有的arp請求。

arp_announce 對網路介面上發出的 ARP 請求包中的源IP地址作出相應的限制;主機會根據這個引數值的不同選擇使用 IP 資料包的源 IP 或當前網路介面卡的IP地址作為ARP請求包的源IP地址。

         0:本機所有IP地址都向任何一個介面通告ARP報文。

         1:儘量僅向該網絡卡迴應與該網段匹配的ARP報文。

         2:只向該網絡卡迴應與該網段匹配的ARP報文。

4、Director Server  伺服器配置:

a、VIP 繫結

[[email protected] ~]# cd /etc/sysconfig/network-scripts/

[[email protected] network-scripts]# cp ifcfg-ens33{,:0}

[[email protected] network-scripts]# vim ifcfg-ens33:0

TYPE=Ethernet

BOOTPROTO=none

DEFROUTE=yes

NAME=ens33:0

DEVICE=ens33:0

ONBOOT=yes

IPADDR=192.168.20.135

PREFIX=24

[[email protected] network-scripts]# ifup ens33:0

[[email protected] network-scripts]# ifconfig

如圖:

image

b、ipvsadm 安裝及策略建立

[[email protected] ~]# yum –y install ipvsadm

[[email protected] ~]# ipvsadm -A -t 192.168.20.135:80 -s lc
[[email protected] ~]# ipvsadm -a -t 192.168.20.135:80 -r 192.168.20.132:80 -g -w 1
[[email protected] ~]# ipvsadm -a -t 192.168.20.135:80 -r 192.168.20.133:80 -g -w 2

如圖:

image

說明:LVS 預設工作模式為 DR 模式,因此,我們在配置策略的時候可以直接寫成 [[email protected] ~]#  ipvsadm -a -t 192.168.20.135:80 -r 192.168.20.132:80 -w 1 這樣的,也可以使用 –g 引數來指定為 DR 模式。

5、驗證,在瀏覽器中訪問 http://192.168.20.135

imageimage