LVS负载均衡DR模式实现
LVS負(fù)載均衡之DR模式配置
DR 模式架構(gòu)圖:
操作步驟
實驗環(huán)境準(zhǔn)備:(centos7平臺)
所有服務(wù)器上配置
# systemctl stop firewalld //關(guān)閉防火墻 # sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //關(guān)閉selinux,重啟生效 # setenforce 0 //關(guān)閉selinux,臨時生效 # ntpdate 0.centos.pool.ntp.org //時間同步 注意:realserver的網(wǎng)關(guān)需要指向DIP步驟一:配置 router
1)打開 ip_forward
[root@router ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@router ~]# sysctl -p2)添加防火墻規(guī)則,指定客戶端進(jìn)來的規(guī)則,(此處使用 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的)網(wǎng)卡進(jìn)來訪問80的包,DNAT到 10.10.10.110(也就是 LVS 調(diào)度器的 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),這樣可以實現(xiàn) LVS 調(diào)度器能回客戶端 如果不加這條的話,也可以在LVS 調(diào)度器上添加路由(route add default gw 10.10.10.120 指一個網(wǎng)關(guān)回去,因為 DNAT 的目標(biāo)機(jī)器需要一個網(wǎng)關(guān)才能回給 client)[root@router ~]# iptables-save > /etc/sysconfig/iptables [root@router ~]# systemctl start iptables.service [root@router ~]# systemctl enable iptables.service步驟二:配置 LVS 調(diào)度器
1)安裝ipvsadm
[root@lvs-director ~]# yum install ipvsadm -y2)配置調(diào)度規(guī)則
[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 調(diào)度器就會把數(shù)據(jù)包調(diào)給 10.10.10.11 或 10.10.10.12 時,就只修改 MAC 地址,不修改目標(biāo) 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 03)保存在文件中,設(shè)置為開機(jī)啟動
[root@lvs-director ~]# [root@lvs-director ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm [root@lvs-director ~]# systemctl start ipvsadm [root@lvs-director ~]# systemctl enable ipvsadm4) 由于下面會在 web服務(wù)器上面添加一個子接口 lo:0 10.10.10.110網(wǎng)卡,這樣就會到導(dǎo)致 lvs 調(diào)度器過去的包可以成果過去,但是不會回來,因為回來時它會直接查找自己的 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 還是會出現(xiàn)ping 不通的情況,因為ping的時候 10.10.10.110和10.10.10.111掩碼相同,優(yōu)先級一樣。而用225.225.225.128路由選擇會優(yōu)先使用10.10.10.111去ping
步驟三:配置realserver
?在 realserver(web01和web02)上安裝 nginx,并在不同的 web 服務(wù)器上建立不同的主頁內(nèi)容(方便測試),并啟動。
1) 在 web01 服務(wù)器配置
[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 nginx2) 在 web02 服務(wù)器配置
[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 nginx3) 添加vip (不論后端有幾個web服務(wù)器,都需要做)
# ifconfig lo:0 10.10.10.110 netmask 255.255.255.255 //注意掩碼為4個255,想永久生效,可以寫一個 ifcfg-lo:0 的網(wǎng)卡配置文件即可。 最好不要寫成 ifconfig lo:0 10.10.10.110/32 的形式,用ifconfig 查掩碼會出現(xiàn)四個0。
這一步是非常重要的,因為路由方式扔過來的包,目標(biāo) IP 不變,也就是說還是 10.10.10.120,只是通過找 10.10.10.11 或 10.10.10.12 的 MAC 地址扔過來的。
所以 web 服務(wù)器上也需要有一個 10.10.10.120 這個 IP 來解析;用 lo 網(wǎng)卡來虛擬就是為了盡量不要與 lvs 網(wǎng)卡造成 ARP 廣播問題。
這里 netmask 為什么是4個 255,而不是 255.255.255.0?
如果為 255.255.255.0,那么 10.10.10.0/24 整個網(wǎng)絡(luò)都無法和web服務(wù)器通訊。
4)?真實服務(wù)器把默認(rèn)路由指向 router 同物理網(wǎng)段的 IP,可以臨時加也可以直接寫在配置文件里面,這里上面的環(huán)境準(zhǔn)備已經(jīng)寫在了配置文件。?(web1 和 web2 都需要做) 臨時加示例:
# route add default gw 10.10.10.1205) 抑制 web 服務(wù)器上 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步驟四:在客戶機(jī)上測試
[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從測試結(jié)果可以看出,輪循調(diào)度給后端web服務(wù)器了。至此dr模式就完成了。
?
LVS 概念篇參考 ->點我
NAT 模式實現(xiàn)參考 ->點我
?
轉(zhuǎn)載于:https://www.cnblogs.com/yanjieli/p/10709160.html
總結(jié)
以上是生活随笔為你收集整理的LVS负载均衡DR模式实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue中的keep-alive
- 下一篇: Jython 安装使用