Nginx被動(dòng)健康檢查和主動(dòng)健康檢查
1.被動(dòng)健康檢查
Nginx自帶有健康檢查模塊:ngx_http_upstream_module,可以做到基本的健康檢查,配置如下:
upstream cluster{ server 172.16.0.23:80 max_fails=1 fail_timeout=10s; server 172.16.0.24:80 max_fails=1 fail_timeout=10s;
# max_fails=1和fail_timeout=10s 表示在單位周期為10s鐘內(nèi),中達(dá)到1次連接失敗,那么接將把節(jié)點(diǎn)標(biāo)記為不可用,并等待下一個(gè)周期(同樣時(shí)常為fail_timeout)再一次去請(qǐng)求,判斷是否連接是否成功。
# fail_timeout為10s,max_fails為1次。
} server { listen 80; server_name xxxxxxx.com; location / { proxy_pass http://cluster; } }
Nginx只有當(dāng)有訪問(wèn)時(shí)后,才發(fā)起對(duì)后端節(jié)點(diǎn)探測(cè)。如果本次請(qǐng)求中,節(jié)點(diǎn)正好出現(xiàn)故障,Nginx依然將請(qǐng)求轉(zhuǎn)交給故障的節(jié)點(diǎn),然后再轉(zhuǎn)交給健康的節(jié)點(diǎn)處理。所以不會(huì)影響到這次請(qǐng)求的正常進(jìn)行。但是會(huì)影響效率,因?yàn)槎嗔艘淮无D(zhuǎn)發(fā),而且自帶模塊無(wú)法做到預(yù)警。
2.主動(dòng)健康檢查(需使用第三方模塊)
主動(dòng)地健康檢查,nignx定時(shí)主動(dòng)地去ping后端的服務(wù)列表,當(dāng)發(fā)現(xiàn)某服務(wù)出現(xiàn)異常時(shí),把該服務(wù)從健康列表中移除,當(dāng)發(fā)現(xiàn)某服務(wù)恢復(fù)時(shí),又能夠?qū)⒃摲?wù)加回健康列表中。淘寶有一個(gè)開(kāi)源的實(shí)現(xiàn)nginx_upstream_check_module模塊
官網(wǎng):http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
http { upstream cluster1 { # simple round-robin server 192.168.0.1:80; server 192.168.0.2:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0rnrn"; check_http_expect_alive http_2xx http_3xx; } upstream cluster2 { # simple round-robin server 192.168.0.3:80; server 192.168.0.4:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_keepalive_requests 100; check_http_send "HEAD / HTTP/1.1rnConnection: keep-alivernrn"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; location /1 { proxy_pass http://cluster1; } location /2 { proxy_pass http://cluster2; } location /status { check_status; access_log off; allow SOME.IP.ADD.RESS; deny all; } } }
3.集成第三方模塊部署
3.1、下載nginx_upstream_check_module模塊
#進(jìn)入nginx安裝目錄 cd /usr/local/nginx #下載nginx_upstream_check_module模塊 wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master #wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip #解壓 unzip master cd nginx-1.12 # 進(jìn)入nginx的源碼目錄 # -p0,是“當(dāng)前路徑” -p1,是“上一級(jí)路徑” patch -p1 < ../nginx_upstream_check_module-master/check_1.11.5+.patch #nginx -V 可以查看原有配置 輸出 ./configure --prefix=/usr/local/nginx #增加upstream_check模塊 ./configure --prefix=/usr/local/nginx --add-module=../nginx_upstream_check_module-master make /usr/local/nginx/sbin/nginx -t # 檢查下是否有問(wèn)題 注意 check版本和Nginx版本要求有限制 1.12以上版本的nginx,補(bǔ)丁為check_1.11.5+.patch 具體參考github https://github.com/yaoweibin/nginx_upstream_check_module
3.2.修改配置文件,讓nginx_upstream_check_module模塊生效
http { upstream cluster1 { # simple round-robin server 192.168.0.1:80; server 192.168.0.2:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0rnrn"; check_http_expect_alive http_2xx http_3xx; } upstream cluster2 { # simple round-robin server 192.168.0.3:80; server 192.168.0.4:80; check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_keepalive_requests 100; check_http_send "HEAD / HTTP/1.1rnConnection: keep-alivernrn"; check_http_expect_alive http_2xx http_3xx; } server { listen 80; location /1 { proxy_pass http://cluster1; } location /2 { proxy_pass http://cluster2; } location /status { check_status; access_log off; allow SOME.IP.ADD.RESS; deny all; } } }
3.3重載nginx
訪問(wèn)http://nginx/nstatus
人為把其中的一個(gè)節(jié)點(diǎn)關(guān)掉刷新http://nginx/nstatus
udp反向代理時(shí)健康檢查的問(wèn)題,另一位大神在上面nginx_upstream_check_module的基礎(chǔ)上作了修改,實(shí)現(xiàn)了在第4層的代理tcp和udp時(shí)的健康檢查。
https://github.com/zhouchangxun/ngx_healthcheck_module
相關(guān)知識(shí)
孕前檢查,健康檢查……
k8s健康檢查 spring k8s健康檢查探針多個(gè)地址
健康檢查
健康檢查如何體檢
健康檢查表
健康檢查項(xiàng)目
定期健康檢查
身體健康檢查
健康體檢查什么
產(chǎn)前檢查能查出孩子健康嗎
網(wǎng)址: Nginx被動(dòng)健康檢查和主動(dòng)健康檢查 http://www.u1s5d6.cn/newsview92023.html
推薦資訊
- 1發(fā)朋友圈對(duì)老公徹底失望的心情 12775
- 2BMI體重指數(shù)計(jì)算公式是什么 11235
- 3補(bǔ)腎吃什么 補(bǔ)腎最佳食物推薦 11199
- 4性生活姿勢(shì)有哪些 盤點(diǎn)夫妻性 10428
- 5BMI正常值范圍一般是多少? 10137
- 6在線基礎(chǔ)代謝率(BMR)計(jì)算 9652
- 7一邊做飯一邊躁狂怎么辦 9138
- 8從出汗看健康 出汗透露你的健 9063
- 9早上怎么喝水最健康? 8613
- 10五大原因危害女性健康 如何保 7828