nginx下後端realserver健康檢測模塊ngx_http_upstream_check_module

想用Nginx或者Tengine替代LVS,即能做七層的負載均衡,又能做監控狀態檢測,一旦發現後面的realserver掛了就自動剔除,恢復後自動加入服務池裏,可以用Tengine的ngx_http_upstream_check_module模塊。本文主要介紹在工作中,搭建遇到問題及處理方法,便以後查詢。

首先,我們大多數站點都是nginx+tomcat這個比較常見模式,其實nginx本身也有自己的健康檢測模塊,本人覺得不好用,故使用ngx_http_upstream_check_module。

nginx版本:1.8.0

tomcat: 1.6

因之前已經安裝了nginx,所以要增加此模塊,需要給nginx打補丁:

1、下載模塊

  #cd /usr/local/src

  #wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz

  #tar zxvf v0.3.0.tar.gz

  #mv nginx_upstream_check_module-0.3.0 nginx_upstream_check_module

2、爲nginx打補丁

  #cd /opt/software/nginx-1.8.0

  #patch -p1 < /opt/software/nginx_upstream_check_module/check_1.7.2+.patch

  #./configure --user=www --group=www --add-module=/opt/software/ngx_devel_kit --add-module=/opt/software/lua-nginx-module --prefix=/opt/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_secure_link_module --with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-pcre=/opt/software/pcre-8.36 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1p --with-google_perftools_module --add-module=/usr/local/src/ModSecurity/nginx/modsecurity/ --add-module=/opt/software/nginx_upstream_check_module/

  #make (備註:此編譯要和之前一樣)

  #mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.old

  #cp ./objs/nginx /opt/nginx/sbin/

  #/opt/nginx/sbin/nginx -t(檢查是否有問題)

  #kill -USR2 `cat /var/run/nginx.pid`

3、在nginx.conf配置文件裏upstream中加入健康檢測

  

upstream www {


         server 10.1.1.22:38080;

         server 10.1.1.22:38081;

         check interval=3000 rise=2 fall=5 timeout=1000 type=http;

         check_http_send "GET / HTTP/1.1\r\nHost: www.baidu.cn\r\n\r\n";



check interval 指令可以打開後端服務器的健康檢查功能。

指令後面的參數意義是:


interval:向後端發送的健康檢查包的間隔。

fall(fall_count): 如果連續失敗次數達到fall_count,服務器就被認爲是down。

rise(rise_count): 如果連續成功次數達到rise_count,服務器就被認爲是up。

timeout: 後端健康請求的超時時間。

default_down: 設定初始時服務器的狀態,如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認爲是不可用,要等健康檢查包達到一定成功次數以後纔會被認爲是健康的。

type:健康檢查包的類型,現在支持以下多種類型

    tcp:簡單的tcp連接,如果連接成功,就說明後端正常。

    ssl_hello:發送一個初始的SSL hello包並接受服務器的SSL hello包。

    http:發送HTTP請求,通過後端的回覆包的狀態來判斷後端是否存活。

    mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷後端是否存活。

    ajp:向後端發送AJP協議的Cping包,通過接收Cpong包來判斷後端是否存活。

port: 指定後端服務器的檢查端口。

check_http_send 指令

該指令可以讓負載均衡器模擬向後端realserver發送,監控檢測的http包,模擬LVS的檢測。

check_http_expect_alive 指令

check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]

返回指定HTTP code,符合預期就算檢測成功


realserver配置


        location = /status.html {

            proxy_pass http://www;

            access_log logs/access.log main;

        }

後端realserver配置,只需要保證 curl http://realserver/status.html 能訪問到即可。


測試


移除realserver的status.html即可模擬服務不可用,負載均衡器會在N次檢測後發現realserver不服務,error_log裏會打印。移回status.html即立馬恢復服務。


 

    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章