溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

搭建 LVS+HA網(wǎng)站服務群集

發(fā)布時間:2020-07-14 19:30:24 來源:網(wǎng)絡 閱讀:404 作者:張九冫 欄目:系統(tǒng)運維

搭建keepalived+DR/NAT模式的高可用web群集,這篇博文以keepalived+DR的環(huán)境來搭建一個高可用的web服務群集。

相關(guān)技術(shù)文檔可以去本人主頁查看:https://blog.51cto.com/14227204?,https://blog.51cto.com/14227204/2438901

環(huán)境如下:

搭建 LVS+HA網(wǎng)站服務群集


一、環(huán)境分析:

1、2個調(diào)度器和2個web節(jié)點使用同一個網(wǎng)段地址,可以直接和外網(wǎng)通信。為了共享存儲的

安全性,一般將web節(jié)點和存儲服務器規(guī)劃到內(nèi)網(wǎng)環(huán)境,所以web節(jié)點必須有兩個及以上

網(wǎng)卡的接口。

2、我這里資源有限,也為了配置方便,所以調(diào)度器和web節(jié)點分別只有兩個,在web訪問請

求量不大的情況下,足夠了,但是若訪問請求比較大,那么最少要分別配置三個調(diào)度器和

web節(jié)點,如果只有兩個web節(jié)點的話,訪問量又比較大,那么一旦有一個宕機了,那剩下

一個獨苗必定會因為扛不住激增的訪問請求,而被打死。

3、準備系統(tǒng)映像,以便安裝相關(guān)服務。

4、自行配置防火墻策略和除了VIP之外的IP地址(我這里直接關(guān)閉了防火墻)。

5、keepalived會自動調(diào)用IP_vs模塊,所以無需手動加載。

二、開始搭建:

配置主調(diào)度器:

[root@lvs1?/]#?yum?-y?install?ipvsadm?keepalived????????#?安裝keepalived?和?ipvsadm管理工具
[root@lvs1?keepalived]#?vim?/etc/sysctl.conf????????#?調(diào)整內(nèi)核參數(shù),關(guān)閉ICMP重定向
...........
net.ipv4.conf.all.send_redirects?=?0
net.ipv4.conf.default.send_redirects?=?0
net.ipv4.conf.ens33.send_redirects?=?0
[root@lvs1?/]#?sysctl?-p???????????????????????#?刷新使配置生效
net.ipv4.conf.all.send_redirects?=?0
net.ipv4.conf.default.send_redirects?=?0
net.ipv4.conf.ens33.send_redirects?=?0
[root@lvs1?/]#?cd?/etc/keepalived/
[root@lvs1?keepalived]#?cp?keepalived.conf?keepalived.conf.bak????????#?復制一份keepalived?主配文件作為備份,以免修改時出錯
[root@lvs1?/]#?vim?/etc/keepalived/keepalived.conf???????????#?編輯主配文件
!?Configuration?File?for?keepalived

global_defs?{
???notification_email?{
?????acassen@firewall.loc
?????failover@firewall.loc?????????????#?當出錯時,將報錯信息發(fā)送到的收件人地址,可根據(jù)需要填寫
?????sysadmin@firewall.loc
???}
???notification_email_from?Alexandre.Cassen@firewall.loc????#發(fā)件人姓名、地址(可不做修改)???
???smtp_server?192.168.200.1
???smtp_connect_timeout?30
???router_id?LVS1????????????????#本服務器的名稱改一下,在群集中所有調(diào)度器名稱里必須唯一
}??

vrrp_instance?VI_1?{
????state?MASTER?????????????#?設為主調(diào)度器
????interface?ens33??????????#承載VIP地址的物理網(wǎng)卡接口根據(jù)實際情況改一下
????virtual_router_id?51???????
????priority?100??????????#?主調(diào)度器的優(yōu)先級
????advert_int?1
????authentication?{?????????#?主?從熱備認證信息
????????auth_type?PASS
????????auth_pass?1111
????}
????virtual_ipaddress?{????????#?指定群集?VIP地址
????????200.0.0.100
????}???
}

virtual_server?200.0.0.100?80?{??????#?虛擬服務器地址(VIP)?端口
????delay_loop?15????????????????#?健康檢查的間隔時間
????lb_algo?rr????????????????#?輪詢調(diào)度算法
????lb_kind?DR??????????????#?指定工作模式,這里為DR,也可改為NAT
????!?persistence_timeout?50??????????#為了一會測試看到效果,將連接保持這行前加“?!”將該行注釋掉
????protocol?TCP
????
????real_server?200.0.0.3?80?{??????#?web節(jié)點的地址及端口
????????weight?1
????????TCP_CHECK?{
????????????connect_port?80
????????????connect_timeout?3
????????????nb_get_retry?3
????????????????????????delay_before_retry?3
????????}
????}
????????real_server?200.0.0.4?80?{?????#?另一?web節(jié)點地址及端口
????????????????weight?1
????????????????TCP_CHECK?{
????????????????????????connect_port?80??????????#?配置連接端口
????????????????????????connect_timeout?3
????????????????????????nb_get_retry?3
????????????????????????delay_before_retry?3
????????}
????}
}
[root@lvs1?/]#?systemctl?restart?keepalived?
[root@lvs1?/]#?systemctl?enable?keepalived

主調(diào)度器到這就告一段落配置完成了:

配置從調(diào)度器:


[root@localhost?/]#?yum?-y?install?keepalived?ipvsadm
[root@localhost?/]#?scp?root@200.0.0.1:/etc/sysctl.conf?/etc/???????#?可通過scp命令將配置較繁雜的復制過來
root@200.0.0.1's?password:?
sysctl.conf?????????????
??????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????100%??566?????0.6KB/s???00:00????[root@localhost?/]#?sysctl?-p
[root@localhost?/]#?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?/]#?vim?/etc/keepalived/keepalived.conf?
......................
router_id?LVS2???????????????????????#?route-id?要不一樣
vrrp_instance?VI_1?{
????state?BACKUP???????????????#?狀態(tài)改為?BACKUP?最好大寫
????interface?ens33??????????????#?網(wǎng)卡如果一樣的話可不更改
????virtual_router_id?51
????priority?90????????????????#?優(yōu)先級要比主調(diào)度器小
????advert_int?1
????authentication?{
????????auth_type?PASS
????????auth_pass?1111
????}
????virtual_ipaddress?{???????????????#?就需要改這些其他配置均與主調(diào)度器相同
????????200.0.0.100
????}
}
[root@localhost?/]#?systemctl?enable?keepalived
[root@localhost?/]#?systemctl?restart??keepalived?????????#?重啟服務使配置生效

若需要部署多個從調(diào)度器,按照以上這個從(備份)調(diào)度器配置即可

web1節(jié)點配置:


[root@web1?/]#?cd?/etc/sysconfig/network-scripts/
[root@web1?network-scripts]#?cp?ifcfg-lo?ifcfg-lo:0
[root@web1?network-scripts]#?vim?ifcfg-lo:0
DEVICE=lo:0
IPADDR=200.0.0.100???????????????#?VIP??地址
NETMASK=255.255.255.255??????????#?掩碼為1
ONBOOT=yes
[root@web1?network-scripts]#?ifup?lo:0??????#?啟動虛接口
[root@web1?network-scripts]#?ifconfig?lo:0??????#?查看配置有無生效
lo:0:?flags=73<UP,LOOPBACK,RUNNING>??mtu?65536
????????inet?200.0.0.100??netmask?255.255.255.255
????????loop??txqueuelen?1??(Local?Loopback)
[root@web1?/]#?route?add?-host?200.0.0.100?dev?lo:0????????????#?添加本地路由
[root@web1?/]#?vim?/etc/rc.local???????????????#設置開機自動,添加這條路由記錄??????????????
????????????????................................
/sbin/route?add?-host?200.0.0.100?dev?lo:0
[root@web1?/]#?vim?/etc/sysctl.conf?????#?調(diào)整/proc參數(shù),關(guān)閉?ARP響應
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@web1?/]#?sysctl?-p?????????????#?刷新使配置生效
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@web1?/]#?yum?-y?install?httpd
[root@web1?/]#?echo?test1.com?>?/var/www/html/index.html
[root@web1?/]#?systemctl?start?httpd
[root@web1?/]#?systemctl?enable?httpd

web2節(jié)點和web1節(jié)點配置相同,這里我就省略了,但是這里我為了方便看出驗證效果,將web2的測試文件寫為test2.com

搭建 LVS+HA網(wǎng)站服務群集

搭建 LVS+HA網(wǎng)站服務群集

若訪問到的是同一頁面,在排除配置上錯誤的情況下,可以打開多個網(wǎng)頁,或者稍等一會再刷新,因為它可能有一個保持連接的時間,所以會存在延遲。

三、搭建 NFS 共享存儲服務:

[root@nfs?/]#?mkdir?opt/wwwroot
[root@nfs?/]#?vim?/etc/exports???????#?編寫配置文件
/opt/wwwroot????192.168.1.0/24(rw,sync,no_root_squash)
[root@nfs?/]#?systemctl?restart?nfs?????????#?重啟服務使配置生效
[root@nfs?/]#?systemctl?restart?rpcbind
[root@nfs?/]#?showmount?-e??????????????????#?查看本機發(fā)布的掛載目錄
Export?list?for?nfs:
/opt/wwwroot?192.168.1.0/24
[root@nfs?/]#?echo?nfs.test.com?>?/opt/wwwroot/index.html

所有節(jié)點掛載共享存儲目錄:

[root@web1?/]#?showmount?-e?192.168.1.5???????#?查看共享服務器共享的所有目錄
Export?list?for?192.168.1.5:
/opt/wwwroot?192.168.1.0/24
[root@web1?/]#?mount?192.168.1.5:/opt/wwwroot/?/var/www/html/?????????#?掛載到本地
[root@web1?/]#?vim?/etc/fstab???????????????#設置自動掛載?
???????????????????.........................
192.168.1.5:/opt/wwwroot??/var/www/html???nfs???defaults,_netdev?0?0

web1和web2都需要掛載

搭建 LVS+HA網(wǎng)站服務群集


1)VIP在哪個調(diào)度器上,查詢該調(diào)度器承載VIP地址的物理接口,即可看到VIP地址(VIP地址在備份調(diào)度器上查不到的):

[root@LVS1?~]#?ip?a?show?dev?ens33??????????????#查詢承載VIP地址的物理網(wǎng)卡ens332:?ens33:?<BROADCAST,MULTICAST,UP,LOWER_UP>?ate?UP?groupn?1000
???link/ether?00:0c:29:77:2c:03?brd?ff:ff:ff:ff:ff:ff
???inet?200.0.0.1/24?brd?200.0.0.255?scope?global?noprefixroute?ens33
??????valid_lft?forever?preferred_lft?forever
???inet?200.0.0.100/32?scope?global?ens33???????????????????#VIP地址。
??????valid_lft?forever?preferred_lft?forever
???inet6?fe80::95f8:eeb7:2ed2:d13c/64?scope?link?noprefixroute?
??????valid_lft?forever?preferred_lft?forever

2)查詢有哪些web節(jié)點:

[root@LVS1?~]#?ipvsadm?-ln??????????????????#查詢web節(jié)點池及VIPIP?Virtual?Server?version?1.2.1?(size=4096)
Prot?LocalAddress:Port?Scheduler?Flags
???RemoteAddress:Port???????????Forward?Weight?ActiveConn?InActConn
TCP??200.0.0.100:80?rr??
???200.0.0.3:80?????????????????Route???1??????0??????????0?????????
??200.0.0.4:80?????????????????Route???1??????0??????????0

3)模擬Web2節(jié)點和主調(diào)度器宕機,并在備份調(diào)度器上再次查詢VIP以及web節(jié)點:

[root@LVS2?~]#?ip?a?show?dev?ens33???????#可以看到VIP地址已經(jīng)轉(zhuǎn)移到了備份調(diào)度器上2:?ens33:?<BROADCAST,MULTICAST,UP,LOWER_UP>?
????link/ether?00:0c:29:9a:09:98?brd?ff:ff:ff:ff:ff:ff
????inet?200.0.0.2/24?brd?200.0.0.255?scope?global?noprefixroute?ens33
???????valid_lft?forever?preferred_lft?forever
????inet?200.0.0.100/32?scope?global?ens33??????????????????????#VIP地址。
???????valid_lft?forever?preferred_lft?forever
????inet6?fe80::3050:1a9b:5956:5297/64?scope?link?noprefixroute?
???????valid_lft?forever?preferred_lft?forever
[root@LVS2?~]#?ipvsadm?-ln???????????????????#Web2節(jié)點宕機后,就查不到了。IP?Virtual?Server?version?1.2.1?(size=4096)
Prot?LocalAddress:Port?Scheduler?Flags
??->?RemoteAddress:Port???????????Forward?Weight?ActiveConn?InActConn
TCP??200.0.0.100:80?rr
??->?200.0.0.3:80?????????????????Route???1??????0??????????0?????????
????#當主調(diào)度器或Web2節(jié)點恢復正常后,將會自動添加到群集中,并且正常運行。

4)查看調(diào)度器故障切換時的日志消息:

[root@LVS2?~]#?tail?-30?/var/log/messages



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI