VRRP協議
目的就是爲了解決靜態路由單點故障問題的css
keepalived高可用功能實現的基本原理爲: 兩臺主機同時安裝好keepalived軟件並啓動服務,開始正常工做時 角色爲Master的主機得到全部資源並對用戶提供服務 角色爲Backup的主機做爲Master主機的熱備; 當角色爲Master的主機失效或出現故障時 角色爲Backup的主機將自動接管Master主機的全部工做,包括接管VIP資源及相應資源服務 而當角色爲Master的主機故障修復後,又會自動接管回他原來處理的工做 角色爲Backup的主機則同時釋放Master主機失效時他接管的工做 此時,兩臺主機將恢復到啓動時各自的原始角色及工做狀態
VRRP,全稱Virtual Router Redundancy Protocol,中文名爲虛擬路由冗餘協議 VRRP的出現就是爲了解決靜態路由的單點故障問題 VRRP是經過一種競選機制來將路由的任務交給某臺VRRP路由器的. VRRP經過競選機制來實現虛擬路由器的功能,全部的協議報文都是經過IP多播(Multicast)包(默認的多播地址224.0.0.18)形式發送的 虛擬路由器由VRID(範圍0-255)和一組IP地址組成,對外表現爲一個周知的MAC地址,:00-00-5E-00-01-{VRID}. 因此,在一個虛擬路由器中,無論誰是Master,對外都是相同的MAC和IP(稱之爲VIP). 客戶端主機並不須要因Master的改變修改本身的路由配置.對它們來講,這種切換是透明的. 在一組虛擬路由器中,只有做爲Master的VRRP路由器會一直髮送VRRP廣播包,此時Backup不會搶佔Master 當Master不可用時,Backup就收不到來自Master的廣播包了,此時多臺Backup中優先級最高的路由器會搶佔爲Master. 這種搶佔是很是快速的(可能只有1秒甚至更少),以保證服務的連續性,處於安全性考慮,VRRP數據包使用了加密協議進行了加密.
解答: keepalived高可用對之間是經過VRRP通訊的,所以,我從VRRP開始給您講起. 1)VRRP,全稱Virtual Router Reduancy Protocol,中文名爲虛擬路由器冗餘協議,VRRP的出現是爲了解決靜態路由的單點故障, 2)VRRP是經過一種競選協議來將路由任務交給某臺VRRP路由器的, 3)VRRP用IP多播的方式,(默認多播地址(224.0.0.18))實現高可用對之間通訊. 4)工做時主節點發包,備節點接包,當備節點接收不到主節點發的包的時候,就啓動接管程序接管主節點的資源.備節點能夠有多個,經過優先級競選,但通常keepalived系統運維工做中都是一對. 5)VRRP使用了加密協議加密數據,但keepalived官方目前仍是推薦用明文的方式配置認證類型和密碼. 介紹完了VRRP,接下來我在介紹一下keepalived服務的工做原理; keepalived高可用對之間是經過VRRP進行通訊的,VRRP是經過競選機制來肯定主備的,主的優先級高於備,所以,工做時會優先得到全部的資源,備節點處於等待狀態,當主掛了的時候,備節點就會接管主節點的資源,而後頂替主節點對外提供服務. 在keepalived服務對之間,只有做爲主的服務器會一直髮送VRRP廣播包,告訴備他還活着,此時備不會搶佔主,當主不可用時,即備監聽不到主發送的廣播包時,就會啓動相關服務接管資源,保證業務的連續性,接管速度最快能夠小於一秒
VRRP經過競選機制來實現虛擬路由器的功能
全部的協議報文都是經過IP多播(Multicast)包
默認的多播地址224.0.0.18html
1.虛擬公網IP必須是真實可用的
2.虛擬公網IP不能重複
3.組播地址必須是能夠通信的nginx
yum install keepalived -yweb
global_defs { router_id lb01 #設置路由ID,每一個主機不同 } vrrp_instance VI_1 { #設置VRRP組名,同一組組名相同 state MASTER #設置角色狀態,分爲MASTER BACKUP interface eth0 #VIP綁定的網卡 virtual_router_id 50 #虛擬路由id,同一組同樣 priority 150 #權重,權重越高,優先級越高 advert_int 1 #發送組播間隔 authentication { #設置驗證,密碼爲明文 auth_type PASS auth_pass 1111 } virtual_ipaddress { #設定的虛擬IP,這個虛擬IP必須是存在且合法且沒有被使用的。 10.0.0.3 } }
[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 } }
[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 } }
systemctl start keepalived
關掉任意一臺,觀察VIP是否會漂移
恢復MASTER觀察BACKUP的VIP是否會消失面試
yum install tcpdump -y
tcpdump -nn -i any host 224.0.0.18
systemctl start firewalld.service
觀察是否兩邊都有VIP安全
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 eth1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT systemctl reload firewalld
觀察是否兩邊都有VIPbash
[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 } }
[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 } }
systemctl restart keepalived
注意!兩臺lb服務器的Nginx配置如出一轍服務器
mkdir /backup cd /etc/nginx/conf.d mv * /backup
[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; } }
nginx -t systemctl restart nginx
[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 } }
[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服務器配置:
注意!兩臺web服務器配置如出一轍網絡
[root@web01 ~]# cat /etc/nginx/conf.d/www.conf server { listen 80; server_name www.mysun.com; location / { root /code; index www.html; } }
echo "$(hostname)" >/code/index.html
1.nginx掛了,可是keep還活着
2.兩邊都有VIP負載均衡
解決nginx掛了問題:
1.編寫一個腳本
2.keepalived定時去調用這個腳本
1.命令如何實現
systemctl start nginx
2.檢查nginx進程
[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
腳本內容:
[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調用腳本:
[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 } }
兩邊都有VIP
現象:
2邊都有VIP
2邊Nginx都活着
對面的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
腳本內容:
[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 } }