Nginx負載均衡+監控狀態檢測


Nginx負載均衡+監控狀態檢測

想用Nginx或者Tengine替代LVS,即能做七層的負載均衡,又能做監控狀態檢測,一旦發現后面的realserver掛了就自動剔除,恢復后自動加入服務池里,可以用Tengine的ngx_http_upstream_check_module模塊。該模塊在Tengine-1.4.0版本以前沒有默認開啟,它可以在配置編譯選項的時候開啟:./configure --with-http_upstream_check_module。

Nginx.conf 配置

http {
    upstream fire_server{
    ip_hash;
    server 192.168.1.1:80;
    server 192.168.1.2:80;

    check interval=3000 rise=2 fall=5 timeout=1000 type=http ;
    check_http_send "GET /status.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx ;
    }

    server {
        listen       80;
        server_name  localhost default;

        location / {
            proxy_pass http://fire_server;
            access_log logs/fire_server_access.log main;
            error_log logs/error.log debug;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }   
}
  • 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 {
            root html;
            access_log logs/access.log main;
        }

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

測試

  • 移除realserver的status.html即可模擬服務不可用,負載均衡器會在N次檢測后發現realserver不服務,error_log里會打印。移回status.html即立馬恢復服務。
2015/04/04 22:00:42 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
2015/04/04 22:00:43 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
2015/04/04 22:00:44 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
...
enable check peer: 192.168.1.1:80

參考: http://tengine.taobao.org/document_cn/http_upstream_check_cn.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM