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


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

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

Nginx.conf 配置

  1.  
    http {
  2.  
    upstream fire_server{
  3.  
    ip_hash;
  4.  
    server 192.168.1.1:80;
  5.  
    server 192.168.1.2:80;
  6.  
     
  7.  
    check interval=3000 rise=2 fall=5 timeout=1000 type=http ;
  8.  
    check_http_send "GET /status.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
  9.  
    check_http_expect_alive http_2xx http_3xx ;
  10.  
    }
  11.  
     
  12.  
    server {
  13.  
    listen 80;
  14.  
    server_name localhost default;
  15.  
     
  16.  
    location / {
  17.  
    proxy_pass http://fire_server;
  18.  
    access_log logs/fire_server_access.log main;
  19.  
    error_log logs/error.log debug;
  20.  
    }
  21.  
     
  22.  
    error_page 500 502 503 504 /50x.html;
  23.  
    location = /50x.html {
  24.  
    root html;
  25.  
    }
  26.  
    }
  27.  
    }
  • check interval 指令可以打開后端服務器的健康檢查功能。
  1.  
    指令后面的參數意義是:
  2.  
     
  3.  
    interval:向后端發送的健康檢查包的間隔。
  4.  
    fall(fall_count): 如果連續失敗次數達到fall_count,服務器就被認為是down。
  5.  
    rise(rise_count): 如果連續成功次數達到rise_count,服務器就被認為是up。
  6.  
    timeout: 后端健康請求的超時時間。
  7.  
    default_down: 設定初始時服務器的狀態,如果是true,就說明默認是down的,如果是false,就是up的。默認值是true,也就是一開始服務器認為是不可用,要等健康檢查包達到一定成功次數以后才會被認為是健康的。
  8.  
    type:健康檢查包的類型,現在支持以下多種類型
  9.  
    tcp:簡單的tcp連接,如果連接成功,就說明后端正常。
  10.  
    ssl_hello:發送一個初始的SSL hello包並接受服務器的SSL hello包。
  11.  
    http:發送HTTP請求,通過后端的回復包的狀態來判斷后端是否存活。
  12.  
    mysql: 向mysql服務器連接,通過接收服務器的greeting包來判斷后端是否存活。
  13.  
    ajp:向后端發送AJP協議的Cping包,通過接收Cpong包來判斷后端是否存活。
  14.  
    port: 指定后端服務器的檢查端口。
  • check_http_send 指令
該指令可以讓負載均衡器模擬向后端realserver發送,監控檢測的http包,模擬LVS的檢測。
  • check_http_expect_alive 指令
  1.  
    check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
  2.  
    返回指定HTTP code,符合預期就算檢測成功
  3.  
     

RealServer配置

  1.  
    location = /status. html {
  2.  
    root html;
  3.  
    access_log logs/access.log main;
  4.  
    }

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

測試

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

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

轉載於:https://www.cnblogs.com/DjangoBlog/p/7133174.html


免責聲明!

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



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