首頁 資訊 Nginx被動健康檢查和主動健康檢查

Nginx被動健康檢查和主動健康檢查

來源:泰然健康網(wǎng) 時間:2024年11月25日 21:57

1.被動健康檢查

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),中達到1次連接失敗,那么接將把節(jié)點標記為不可用,并等待下一個周期(同樣時常為fail_timeout)再一次去請求,判斷是否連接是否成功。
   # fail_timeout為10s,max_fails為1次。
  } server { listen 80; server_name xxxxxxx.com; location / { proxy_pass http://cluster; } }

Nginx只有當有訪問時后,才發(fā)起對后端節(jié)點探測。如果本次請求中,節(jié)點正好出現(xiàn)故障,Nginx依然將請求轉(zhuǎn)交給故障的節(jié)點,然后再轉(zhuǎn)交給健康的節(jié)點處理。所以不會影響到這次請求的正常進行。但是會影響效率,因為多了一次轉(zhuǎn)發(fā),而且自帶模塊無法做到預警。

2.主動健康檢查(需使用第三方模塊)

主動地健康檢查,nignx定時主動地去ping后端的服務列表,當發(fā)現(xiàn)某服務出現(xiàn)異常時,把該服務從健康列表中移除,當發(fā)現(xiàn)某服務恢復時,又能夠?qū)⒃摲占踊亟】盗斜碇小L詫氂幸粋€開源的實現(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模塊

#進入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 # 進入nginx的源碼目錄 # -p0,是“當前路徑” -p1,是“上一級路徑” 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 # 檢查下是否有問題 注意 check版本和Nginx版本要求有限制 1.12以上版本的nginx,補丁為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

訪問http://nginx/nstatus
人為把其中的一個節(jié)點關(guān)掉刷新http://nginx/nstatus

udp反向代理時健康檢查的問題,另一位大神在上面nginx_upstream_check_module的基礎(chǔ)上作了修改,實現(xiàn)了在第4層的代理tcp和udp時的健康檢查。

https://github.com/zhouchangxun/ngx_healthcheck_module

相關(guān)知識

孕前檢查,健康檢查……
k8s健康檢查 spring k8s健康檢查探針多個地址
健康檢查
健康檢查如何體檢
健康檢查表
健康檢查項目
定期健康檢查
身體健康檢查
健康體檢查什么
產(chǎn)前檢查能查出孩子健康嗎

網(wǎng)址: Nginx被動健康檢查和主動健康檢查 http://www.u1s5d6.cn/newsview92023.html

推薦資訊