Nginx節點存活狀態檢查


目錄

 

 

1 nginx_upstream_check_module

可以利用第三方Nginx插件監控代理后端節點的服務器。 
淘寶技術團隊開發了一個Tengine(Nginx的分支)模塊nginx_upstream_check_module,用於提供主動式后端服務器健康檢查。通過它可以檢測后端realserver的健康狀態,如果后端的realserver不可用,則所有的請求就不會轉發到該節點上。 
Tengine原生支持這個模塊,而Nginx則需要通過打補丁的方式將該模塊添加到Nginx中。

補丁下載地址:

https://github.com/yaoweibin/nginx_upstream_check_module

需要在上一篇文章基礎之上實現

Nginx服務實現動靜分離

環境准備

Hostname IP 說明
lb01 192.168.90.5 Nginx主負載均衡器
lb02 192.168.90.6 Nginx輔負載均衡器
web01 192.168.90.8 web01服務器
web02 192.168.90.7 web02服務器

VIP:192.168.90.3

2 Nginx中加載並配置此模塊

  1.  
    # 在負載lb01中測試
  2.  
    [root @lb01 ~]# cd /home/oldboy/tools/
  3.  
    [root @lb01 tools]# wget -q https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
  4.  
    [root @lb01 tools]# ls
  5.  
    master nginx -1.6.3 nginx-1.6.3.tar.gz
  6.  
    [root @lb01 tools]# unzip master
  7.  
    [root @lb01 tools]# ls
  8.  
    master nginx -1.6.3 nginx-1.6.3.tar.gz nginx_upstream_check_module-master
  9.  
     
  10.  
    # 由於是對源程序打補丁,所以還需要nginx軟件包
  11.  
    [root @lb01 tools]# cd nginx-1.6.3
  12.  
    [root @lb01 nginx-1.6.3]# patch -p1 ../nginx_upstream_check_module-master/check_1.5.12+.patch
  13.  
    patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
  14.  
    patching file src/http/modules/ngx_http_upstream_least_conn_module.c
  15.  
    patching file src/http/ngx_http_upstream_round_robin.c
  16.  
    patching file src/http/ngx_http_upstream_round_robin.h
  17.  
     
  18.  
    # 編譯參數要和以前一致
  19.  
    [root @lb01 nginx-1.6.3]# ./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/home/oldboy/tools/nginx_upstream_check_module-master/ --prefix=/application/nginx-1.6.3/
  20.  
     
  21.  
    # 如果是新裝的nginx則繼續執行下面make install一步,如果給已經安裝的nginx系統打監控補丁就不用執行make install了,這里直接make就行
  22.  
    # make 的作用就是重新生成Nginx二進制啟動命令而已。
  23.  
    make
  24.  
     
  25.  
    # 操作前最好先備份
  26.  
    [root @lb01 nginx-1.6.3]# mv /application/nginx/sbin/nginx{,.ori}
  27.  
     
  28.  
    # 將打過補丁的nginx二進制程序復制到/application/nginx/sbin/目錄下
  29.  
    [root @lb01 nginx-1.6.3]# cp ./obj/nginx /application/nginx/sbin
  30.  
    # 檢測nginx語法是否正常
  31.  
    /application/nginx/sbin/nginx -t
  32.  
    /application/nginx/sbin/nginx -v # 查看版本
  33.  
     
  34.  
    # 修改配置文件如下,注意location中的default_pools要刪掉,否則會跳轉到10.0.0.7中的主頁面
  35.  
    [root @lb01 ~]# vim /application/nginx/conf/nginx.conf
  36.  
    worker_processes 1;
  37.  
    events {
  38.  
    worker_connections 1024;
  39.  
    }
  40.  
    http {
  41.  
    include mime.types;
  42.  
    default_type application/octet-stream;
  43.  
    sendfile on;
  44.  
    keepalive_timeout 65;
  45.  
     
  46.  
    upstream static_pools{
  47.  
    server 192.168.90.7:80 weight=1;
  48.  
    check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
  49.  
    }
  50.  
    upstream upload_pools{
  51.  
    server 192.168.90.7:8080 weight=1;
  52.  
    check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
  53.  
    }
  54.  
    upstream default_pools{
  55.  
    server 192.168.90.8:80 weight=1;
  56.  
    check interval= 3000 rise=2 fall=5 timeout=1000 type=http;
  57.  
    }
  58.  
     
  59.  
    server {
  60.  
    listen 80;
  61.  
    server_name www.rsq.com;
  62.  
    location /static/ {
  63.  
    proxy_pass http://static_pools;
  64.  
    include proxy.conf;
  65.  
    }
  66.  
    location /upload/ {
  67.  
    proxy_pass http://upload_pools;
  68.  
    include proxy.conf;
  69.  
    }
  70.  
    location /status {
  71.  
    check_status;
  72.  
    access_log off;
  73.  
    }
  74.  
    }
  75.  
    }
  76.  
    [root @lb01 nginx-1.6.3]# /application/nginx/sbin/nginx -t
  77.  
    nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
  78.  
    nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

check interval=3000 rise=2 fall=5 timeout=1000 type=http; 
上面配置的意思是,對三個負載均衡pools條目中的所有節點,每隔3秒檢測一次,請求2次正常則標記realserver狀態為up,如果檢測5次都失敗,則標記realserver的狀態為down,超市時間為1秒,檢查的協議是http。 
詳細用法可參見官網 
http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

3 web頁面測試

這里寫圖片描述

把web02中的nginx服務停掉,過一會刷新看結果

這里寫圖片描述

基於nginx_upstream_check_module此模塊可以實現對Nginx Web節點(web01和web02)進行健康檢查

 

轉載至https://blog.csdn.net/mr_rsq/article/details/80399642


免責聲明!

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



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