您好,登錄后才能下訂單哦!
這篇文章主要講解了Keepalived實(shí)現(xiàn)Nginx負(fù)載均衡高可用的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
第一章:keepalived介紹
VRRP協(xié)議
目的就是為了解決靜態(tài)路由單點(diǎn)故障問題的
第二章: keepalived工作原理
2.1 作為系統(tǒng)網(wǎng)絡(luò)服務(wù)的高可用功能(failover)
keepalived高可用功能實(shí)現(xiàn)的基本原理為:
兩臺(tái)主機(jī)同時(shí)安裝好keepalived軟件并啟動(dòng)服務(wù),開始正常工作時(shí)
角色為Master的主機(jī)獲得所有資源并對(duì)用戶提供服務(wù)
角色為Backup的主機(jī)作為Master主機(jī)的熱備;
當(dāng)角色為Master的主機(jī)失效或出現(xiàn)故障時(shí)
角色為Backup的主機(jī)將自動(dòng)接管Master主機(jī)的所有工作,包括接管VIP資源及相應(yīng)資源服務(wù)
而當(dāng)角色為Master的主機(jī)故障修復(fù)后,又會(huì)自動(dòng)接管回他原來處理的工作
角色為Backup的主機(jī)則同時(shí)釋放Master主機(jī)失效時(shí)他接管的工作
此時(shí),兩臺(tái)主機(jī)將恢復(fù)到啟動(dòng)時(shí)各自的原始角色及工作狀態(tài)
2.2 什么是VRRP
VRRP,全稱Virtual Router Redundancy Protocol,中文名為虛擬路由冗余協(xié)議
VRRP的出現(xiàn)就是為了解決靜態(tài)路由的單點(diǎn)故障問題
VRRP是通過一種競(jìng)選機(jī)制來將路由的任務(wù)交給某臺(tái)VRRP路由器的.
VRRP通過競(jìng)選機(jī)制來實(shí)現(xiàn)虛擬路由器的功能,所有的協(xié)議報(bào)文都是通過IP多播(Multicast)包(默認(rèn)的多播地址224.0.0.18)形式發(fā)送的
虛擬路由器由VRID(范圍0-255)和一組IP地址組成,對(duì)外表現(xiàn)為一個(gè)周知的MAC地址,:00-00-5E-00-01-{VRID}.
所以,在一個(gè)虛擬路由器中,不管誰(shuí)是Master,對(duì)外都是相同的MAC和IP(稱之為VIP).
客戶端主機(jī)并不需要因Master的改變修改自己的路由配置.對(duì)它們來說,這種切換是透明的.
在一組虛擬路由器中,只有作為Master的VRRP路由器會(huì)一直發(fā)送VRRP廣播包,此時(shí)Backup不會(huì)搶占Master
當(dāng)Master不可用時(shí),Backup就收不到來自Master的廣播包了,此時(shí)多臺(tái)Backup中優(yōu)先級(jí)最高的路由器會(huì)搶占為Master.
這種搶占是非??焖俚?可能只有1秒甚至更少),以保證服務(wù)的連續(xù)性,處于安全性考慮,VRRP數(shù)據(jù)包使用了加密協(xié)議進(jìn)行了加密.
2.3 面試的時(shí)候怎么說
解答:
keepalived高可用對(duì)之間是通過VRRP通信的,因此,我從VRRP開始給您講起.
1)VRRP,全稱Virtual Router Reduancy Protocol,中文名為虛擬路由器冗余協(xié)議,VRRP的出現(xiàn)是為了解決靜態(tài)路由的單點(diǎn)故障,
2)VRRP是通過一種競(jìng)選協(xié)議來將路由任務(wù)交給某臺(tái)VRRP路由器的,
3)VRRP用IP多播的方式,(默認(rèn)多播地址(224.0.0.18))實(shí)現(xiàn)高可用對(duì)之間通信.
4)工作時(shí)主節(jié)點(diǎn)發(fā)包,備節(jié)點(diǎn)接包,當(dāng)備節(jié)點(diǎn)接收不到主節(jié)點(diǎn)發(fā)的包的時(shí)候,就啟動(dòng)接管程序接管主節(jié)點(diǎn)的資源.備節(jié)點(diǎn)可以有多個(gè),通過優(yōu)先級(jí)競(jìng)選,但一般keepalived系統(tǒng)運(yùn)維工作中都是一對(duì).
5)VRRP使用了加密協(xié)議加密數(shù)據(jù),但keepalived官方目前還是推薦用明文的方式配置認(rèn)證類型和密碼.
介紹完了VRRP,接下來我在介紹一下keepalived服務(wù)的工作原理;
keepalived高可用對(duì)之間是通過VRRP進(jìn)行通信的,VRRP是通過競(jìng)選機(jī)制來確定主備的,主的優(yōu)先級(jí)高于備,因此,工作時(shí)會(huì)優(yōu)先獲得所有的資源,備節(jié)點(diǎn)處于等待狀態(tài),當(dāng)主掛了的時(shí)候,備節(jié)點(diǎn)就會(huì)接管主節(jié)點(diǎn)的資源,然后頂替主節(jié)點(diǎn)對(duì)外提供服務(wù).
在keepalived服務(wù)對(duì)之間,只有作為主的服務(wù)器會(huì)一直發(fā)送VRRP廣播包,告訴備他還活著,此時(shí)備不會(huì)搶占主,當(dāng)主不可用時(shí),即備監(jiān)聽不到主發(fā)送的廣播包時(shí),就會(huì)啟動(dòng)相關(guān)服務(wù)接管資源,保證業(yè)務(wù)的連續(xù)性,接管速度最快可以小于一秒
第三章: VRRP協(xié)議
VRRP通過競(jìng)選機(jī)制來實(shí)現(xiàn)虛擬路由器的功能
所有的協(xié)議報(bào)文都是通過IP多播(Multicast)包
默認(rèn)的多播地址224.0.0.18
VIP前提條件:
1.虛擬公網(wǎng)IP必須是真實(shí)可用的
2.虛擬公網(wǎng)IP不能重復(fù)
3.組播地址必須是可以通訊的
第四章:keepalived安裝配置
1.安裝keepalived
yum install keepalived -y
2.配置文件解釋
global_defs { router_id lb01 #設(shè)置路由ID,每個(gè)主機(jī)不一樣 } vrrp_instance VI_1 { #設(shè)置VRRP組名,同一組組名相同 state MASTER #設(shè)置角色狀態(tài),分為MASTER BACKUP interface eth0 #VIP綁定的網(wǎng)卡 virtual_router_id 50 #虛擬路由id,同一組一樣 priority 150 #權(quán)重,權(quán)重越高,優(yōu)先級(jí)越高 advert_int 1 #發(fā)送組播間隔 authentication { #設(shè)置驗(yàn)證,密碼為明文 auth_type PASS auth_pass 1111 } virtual_ipaddress { #設(shè)定的虛擬IP,這個(gè)虛擬IP必須是存在且合法且沒有被使用的。 10.0.0.3 } }
3.lb01配置
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } }
4.lb02配置
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } }
5.啟動(dòng)
systemctl start keepalived
6.測(cè)試
關(guān)掉任意一臺(tái),觀察VIP是否會(huì)漂移
恢復(fù)MASTER觀察BACKUP的VIP是否會(huì)消失
第五章:腦裂現(xiàn)象
1.安裝抓包工具
yum install tcpdump -y
2.lb02抓包查看
tcpdump -nn -i any host 224.0.0.18
3.lb02新開一個(gè)終端,然后開啟防火墻
systemctl start firewalld.service
4.lb02觀察抓包現(xiàn)象
觀察是否兩邊都有VIP
5.添加放行規(guī)則
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth2 --destination 224.0.0.18 --protocol vrrp -j ACCEPT systemctl reload firewalld
6.lb02觀察抓包現(xiàn)象
觀察是否兩邊都有VIP
第六章:keepalived雙主實(shí)驗(yàn)
1.lb01配置文件
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4 } }
2.lb02配置文件
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4 } }
3.重啟keepalived并觀察現(xiàn)象
systemctl restart keepalived
第七章:keepalived結(jié)合nginx反向代理負(fù)載均衡
lb服務(wù)器的Nginx配置:
注意!兩臺(tái)lb服務(wù)器的Nginx配置一模一樣
1.備份原有配置
mkdir /backup cd /etc/nginx/conf.d mv * /backup
2.編寫Nginx配置文件
[root@lb01 /etc/nginx/conf.d]# cat proxy.conf upstream web_pools { server 172.16.1.7; server 172.16.1.8; } server { listen 80; server_name (www|bbs).mysun.com ; location / { proxy_pass http://web_pools; include proxy_params; } }
3.測(cè)試并重啟nginx
nginx -t systemctl restart nginx
lb服務(wù)器的keepalived配置:
1.lb01的keepalived配置
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } }
2.lb02的keepalived配置
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } }
web服務(wù)器配置:
注意!兩臺(tái)web服務(wù)器配置一模一樣
1.nginx配置
[root@web01 ~]# cat /etc/nginx/conf.d/www.conf server { listen 80; server_name www.mysun.com; location / { root /code; index www.html; } }
2.寫入測(cè)試文件
echo "$(hostname)" >/code/index.html
第八章: 防裂腦腳本
1.問題現(xiàn)象:
1.nginx掛了,但是keep還活著
2.兩邊都有VIP
2.思路:
解決nginx掛了問題:
1.編寫一個(gè)腳本
2.keepalived定時(shí)去調(diào)用這個(gè)腳本
3.實(shí)現(xiàn):
1.命令如何實(shí)現(xiàn)
systemctl start nginx
2.檢查nginx進(jìn)程
[root@lb01 ~]# ps -ef|grep nginx|grep -v "grep" root 1210 1 0 11:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 1211 1210 0 11:21 ? 00:00:00 nginx: worker process [root@lb01 ~]# ps -ef|grep nginx|grep -v "grep"|wc -l 2 [root@lb01 ~]# ps -ef|grep nginx|grep -v "grep"|wc -l 0
腳本內(nèi)容:
[root@lb01 ~]# cat check_web.sh !/bin/bash nginx_status=$(ps -C nginx --no-header|wc -l) if [[ ${nginx_status} == 0 ]] then systemctl start nginx &> /dev/null sleep 1 nginx_status=$(ps -C nginx --no-header|wc -l) if [[ ${nginx_status} == 0 ]] then systemctl stop keepalived fi fi
keepalived調(diào)用腳本:
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } vrrp_script check_web { script "/server/scripts/check_web.sh" interval 5 weight 50 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } track_script { check_web } }
4.第二個(gè)問題:腦裂問題
兩邊都有VIP
現(xiàn)象:
2邊都有VIP
2邊Nginx都活著
對(duì)面的MASTER的Nginx還活著
curl -I -s -w "%{http_code}\n" -o /dev/null 10.0.0.5
但是我又有了VIP
ip a |grep "10.0.0.3"|wc -l
我就把自己干掉
systemctl stop nginx systemctl stop keepalived
腳本內(nèi)容:
[root@lb02 /server/scripts]# cat check_vip.sh #!/bin/bash master_status=$(curl -I -s -w "%{http_code}\n" -o /dev/null 10.0.0.5) my_vip=$(ip a |grep "10.0.0.3"|wc -l) if [ ${master_status} == 200 -a ${my_vip} == 1 ] then systemctl stop nginx systemctl stop keepalived fi
keepalived配置:
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_script check_web { script "/server/scripts/check_web.sh" interval 5 weight 50 } vrrp_script check_vip { script "/server/scripts/check_vip.sh" interval 5 weight 50 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } track_script { check_web check_vip } }
看完上述內(nèi)容,是不是對(duì)Keepalived實(shí)現(xiàn)Nginx負(fù)載均衡高可用的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。