LVS負載均衡之DR模式配置
DR 模式架構圖:
操作步驟
實驗環境准備:(centos7平台)
所有服務器上配置
# systemctl stop firewalld //關閉防火牆 # sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //關閉selinux,重啟生效 # setenforce 0 //關閉selinux,臨時生效 # ntpdate 0.centos.pool.ntp.org //時間同步 注意:realserver的網關需要指向DIP
步驟一:配置 router
1)打開 ip_forward
[root@router ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@router ~]# sysctl -p
2)添加防火牆規則,指定客戶端進來的規則,(此處使用 iptables 做的,也可以換成 firewalld來做)
[root@router ~]# iptables -F [root@router ~]# yum install iptables-services iptables [root@router ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -i ens33 -j DNAT --to-destination 10.10.10.110 //這條表示從 ens33(也就是192.168.1.31的)網卡進來訪問80的包,DNAT到 10.10.10.110(也就是 LVS 調度器的 IP) [root@router ~]# [root@router ~]# iptables -t nat -A POSTROUTING -p tcp --dport 80 -o ens37 -j SNAT --to-source 10.10.10.120 //這條表示(為了客戶端 192.168.1.35 訪問 192.168.1.31 變成 10.10.10.120 訪問10.10.10.110),這樣可以實現 LVS 調度器能回客戶端 如果不加這條的話,也可以在LVS 調度器上添加路由(route add default gw 10.10.10.120 指一個網關回去,因為 DNAT 的目標機器需要一個網關才能回給 client) [root@router ~]# iptables-save > /etc/sysconfig/iptables [root@router ~]# systemctl start iptables.service [root@router ~]# systemctl enable iptables.service
步驟二:配置 LVS 調度器
1)安裝ipvsadm
[root@lvs-director ~]# yum install ipvsadm -y
2)配置調度規則
[root@lvs-director ~]# ipvsadm -A -t 10.10.10.110:80 -s rr [root@lvs-director ~]# ipvsadm -a -t 10.10.10.110:80 -r 10.10.10.11:80 -g //這里的 -g 就是表示使用直接路由模式,LVS 調度器就會把數據包調給 10.10.10.11 或 10.10.10.12 時,就只修改 MAC 地址,不修改目標 IP 直接路由過去 [root@lvs-director ~]# ipvsadm -a -t 10.10.10.110:80 -r 10.10.10.12:80 -g [root@lvs-director ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.10.10.110:80 rr -> 10.10.10.11:80 Route 1 0 0 -> 10.10.10.12:80 Route 1 0 0
3)保存在文件中,設置為開機啟動
[root@lvs-director ~]# [root@lvs-director ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm [root@lvs-director ~]# systemctl start ipvsadm [root@lvs-director ~]# systemctl enable ipvsadm
4) 由於下面會在 web服務器上面添加一個子接口 lo:0 10.10.10.110網卡,這樣就會到導致 lvs 調度器過去的包可以成果過去,但是不會回來,因為回來時它會直接查找自己的 lo:0的10.10.10.110。所以需要加一個子接口 掩碼給到 255.255.255.128。
[root@lvs-director ~]# ifconfig ens33:0 10.10.10.111 netmask 255.255.255.128
注意:如果用掩碼 255.255.255.0 還是會出現ping 不通的情況,因為ping的時候 10.10.10.110和10.10.10.111掩碼相同,優先級一樣。而用225.225.225.128路由選擇會優先使用10.10.10.111去ping
步驟三:配置realserver
在 realserver(web01和web02)上安裝 nginx,並在不同的 web 服務器上建立不同的主頁內容(方便測試),並啟動。
1) 在 web01 服務器配置
[root@web01 ~]# yum install nginx -y [root@web01 ~]# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html [root@web01 ~]# systemctl start nginx [root@web01 ~]# systemctl enable nginx
2) 在 web02 服務器配置
[root@web02 ~]# yum install nginx -y [root@web02 ~]# echo "`hostname` `ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'`" > /usr/share/nginx/html/index.html [root@web02 ~]# systemctl start nginx [root@web02 ~]# systemctl enable nginx
3) 添加vip (不論后端有幾個web服務器,都需要做)
# ifconfig lo:0 10.10.10.110 netmask 255.255.255.255 //注意掩碼為4個255,想永久生效,可以寫一個 ifcfg-lo:0 的網卡配置文件即可。
最好不要寫成 ifconfig lo:0 10.10.10.110/32 的形式,用ifconfig 查掩碼會出現四個0。
這一步是非常重要的,因為路由方式扔過來的包,目標 IP 不變,也就是說還是 10.10.10.120,只是通過找 10.10.10.11 或 10.10.10.12 的 MAC 地址扔過來的。
所以 web 服務器上也需要有一個 10.10.10.120 這個 IP 來解析;用 lo 網卡來虛擬就是為了盡量不要與 lvs 網卡造成 ARP 廣播問題。
這里 netmask 為什么是4個 255,而不是 255.255.255.0?
如果為 255.255.255.0,那么 10.10.10.0/24 整個網絡都無法和web服務器通訊。
4) 真實服務器把默認路由指向 router 同物理網段的 IP,可以臨時加也可以直接寫在配置文件里面,這里上面的環境准備已經寫在了配置文件。 (web1 和 web2 都需要做) 臨時加示例:
# route add default gw 10.10.10.120
5) 抑制 web 服務器上 IP 沖突問題 (web1 和 web2 都需要做)
# vim /etc/sysctl.conf net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 # sysctl -p
步驟四:在客戶機上測試
[root@client ~]# curl 192.168.1.31 web01 10.10.10.11 [root@client ~]# curl 192.168.1.31 web02 10.10.10.12 [root@client ~]# curl 192.168.1.31 web01 10.10.10.11 [root@client ~]# curl 192.168.1.31 web02 10.10.10.12
從測試結果可以看出,輪循調度給后端web服務器了。至此dr模式就完成了。
LVS 概念篇參考 ->點我
NAT 模式實現參考 ->點我