1. 程式人生 > >lvs-nat與dr配置

lvs-nat與dr配置

lvs dr lvs net

1. lvs-nat模式

1.1 nat的實現方式

??通過將請求報文的目標地址和目標端口修改為挑選出某RS的RIP和PORT來實現
??

1.2 nat實現的要點

(1) RIP和DIP應該使用私網地址,RS的網關應該指向DIP(在同一物理網絡中);
(2) 請求和響應報文都要經由director轉發;極高負載的場景中,Director可能會成為系統瓶頸;
(3) 支持端口映射;
(4) VS必須為Linux,RS可以是任意的OS;
(5) RS的RIP與Director的DIP必須在同一IP網絡;

1.3 lvs-net配置

Director: 本機的80端口不能對外提供服務
VIP配置:
??172.18.253.120/16(對外提供服務的地址)

DIP配置:
??IP/NETMASK:192.168.144.254/24(vmnet11)
??開啟核心轉發功能,指令中,參數值為1時啟用ip轉發,為0時禁止ip轉發,在NAT模式下此操作是必須的
??方法一:echo "1" >/proc/sys/net/ipv4/ip_forward
??方法二:vim /etc/sysctl.conf,增加一行:net.ipv4.ip_forward = 1保存退出執行sysctl -p使配置生效。

RS1: 192.168.144.222/24(vmnet11) 安裝httpd,並制作測試頁,內容為RS1

RS2: 192.168.135.200/24(vmnet11) 安裝httpd,並制作測試頁,內容為RS2

1.4 拓撲圖

技術分享圖片

1.5 各服務器具體配置:

Director的VIP網卡手動配置(vmnet11)
??IP:192.168.144.254
??NETMASK:255.255.255.0

RS1網卡手動配置為如下(vmnet11)
??IP:192.168.144.222
??NETMASK:255.255.255.0
??GATEWAY:192.168.144.254
RS2網卡手動配置為如下(vmnet11)
??IP:192.168.144.220
??NETMASK:255.255.255.0
??GATEWAY:192.168.144.254

網卡配置好之後,互ping測試網絡是否通暢

1.6 在Director上配置LVS

#]~ ipvsadm -A -t 172.18.253.120:80 -s rr
#]~ ipvsadm -a -t 172.18.253.120:80 -r 192.168.144.222:80 -m    
#因nat模式支持端口映射,RS的端口可修改為其它端口
#]~ ipvsadm -a -t 172.18.253.120:80 -r 192.168.144.200:80 -m

1.7 查看當前LVS狀況:

技術分享圖片

1.8 測試:

技術分享圖片
因LVS調試方法為round robin,故每次訪問的RS不同。


2. lvs-dr模式

direct routing(直接路由) LVS默認類型

2.1 實現方式:

??通過為請求報文重新封裝一個MAC首部進行轉發,源MAC是DRP所在接口的MAC,目標MAC是挑選出的RS的RIP所在接口的MAC地址。;IP首部不會發生變化(源IP為CIP,目標IP始終為VIP);

2.2 lvs-dr實現要點

在各主機(Director,RS)均需配置VIP;因此,要解決地址的沖突問題,目標是讓各RS上的VIP不可見,僅用於接收目標地址為VIP的報文,同時可作為響應報文的源地址。
為達到這一目標,有三種解決方案:
??(1)在前端的網關接口上靜態綁定,需要有路由管理權限
??(2)在各RS上使用arptables,添加規則,拒絕自己的VIP向外通告以及響應arp地址解析的請求。
??(3)在各RS上修改內核參數,來限制arp響應和通告。

2.3 lvs-dr實現要點中的apr_ignore及apr_announce解釋

限制響應級別:
??arp_ignore:控制系統在收到外部的arp請求時,是否要返回arp響應
??Define different modes for sending replies in response to received ARP requests that resolve local target IP addresses:
??0:(default): reply for any local target IP address, configured on any interface
使用本地所有接口上配置的地址進行響應
??1:reply only if the target IP address is local address configured on the incoming interface
????僅在請求的目標IP在本地主機的接入報文的接口上時,才給予響應。
??2:reply only if the target IP address is local address configured on the incoming interface and both with the sender‘s IP address are part from same subnet on this interface
????只響應目的IP地址為接收網卡上的本地地址的arp請求,並且arp請求的源IP必須和接收網卡同網段。
??3:do not reply for local addresses configured with scope host, only resolutions for global and link addresses are replied
????如果ARP請求數據包所請求的IP地址對應的本地地址其作用域(scope)為主機(host),則不回應ARP響應數據包,如果作用域為全局(global)或鏈路(link),則回應ARP響應數據包。

??4-7 – reserved
??保留位
??8:do not reply for all local addresses .The max value from conf/{all,interface}/arp_ignore is used when ARP request is received on the {interface}
????所有網絡都不響應
限制通告級別:arp_announce
??0:(default) Use any local address, configured on any interface
??默認,把本地所有接口信息向所有接口公告

??1:Try to avoid local addresses that are not in the target‘s subnet for this interface. This mode is useful when target hosts reachable via this interface require the source IP address in ARP requests to be part of their logical network configured on the receiving interface. When we generate the request we will check all our subnets that include the target IP and will preserve the source address if it is from such subnet. If there is no such subnet we select source address according to the rules for level 2.
??盡量避免向非本網絡通告

??2:Always use the best local address for this target. In this mode we ignore the source address in the IP packet and try to select local address that we prefer for talks with the target host. Such local address is selected by looking for primary IP addresses on all our subnets on the outgoing interface that include the target IP address. If no suitable local address is found we select the first local address we have on the outgoing interface or on all other interfaces, with the hope we will receive reply for our request and even sometimes no matter the source IP address we announce. The max value from conf/{all,interface}/arp_announce is used.
??忽略IP數據包的源IP地址,選擇該發送網卡上最合適的本地地址作為arp請求的源IP地址。

2.4 設計要點

1、各主機有一個接口即可,但需要在同一物理網絡中
2、RIP的網關不能指向DIP,RIP和DIP通常應該在同一網絡,但此二者未必會與VIP在同一網絡。
3、如果基於內核參數來設定,各RS需要先設置內核參數,再設置VIP及路由。

2.5 拓撲結構

技術分享圖片

2.6 dr模式中各服務器配置:

RS1與RS2上設置內核參數腳本:

#!/bin/bash
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
esac

Director地址配置:
ens33: 172.18.253.120/16 broadcast :172.18.253.120
ens33:0 172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119
配置網卡命令:

~]# ifconfig ens33:0 172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119

RS1地址配置
ens33:172.18.253.121/16

~]# lo:0:172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119

添加路由表:

~]# route add -host 172.18.253.119 dev lo:0

開啟httpd服務,編輯文件/var/www/html/index.html,內容為RS1
RS2地址配置
ens33:172.18.253.122/16
lo:0:172.18.253.119 netmask 255.255.255.255 broadcast 172.18.253.119
添加路由表:

~]# route add -host 172.18.253.119 dev lo:0

開啟httpd服務,編輯文件/var/www/html/index.html,內容為RS2

2.7 定義LVS

~]# ipvsadm -A -t 172.18.253.119:80 -s rr
~]# ipvsadm -a -t 172.18.253.119:80 -r 172.18.253.121:80 -g
~]# ipvsadm -a -t 172.18.253.119:80 -r 172.18.253.122:80 -g

2.6.3 註:

RS上的設置也可直接通過腳本全部實現:

#!/bin/bash
vip=172.18.253.119
case $1 in
        start)
                echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
                echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
                /sbin/ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
                /sbin/route add -host $vip dev lo:0
                echo "real server start OK"
                ;;
        stop)
                echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
                echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
                echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
                echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
                echo "real server stop ok"
                ;;
esac

lvs-nat與dr配置