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服务实现动静分离

环境准备

HostnameIP说明
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

posted @ 2020-12-10 15:43  zbs666  阅读(984)  评论(0编辑  收藏  举报