Nginx upstream_check_module模块实现后端节点健康检查功能

释放双眼,带上耳机,听听看~!
淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后服务的健康状态,如果后端服务器不可用,则所以的请求不转发到这台服务器。

Nginx upstream_check_module模块实现后端节点健康检查功能

Nginx

Date:2019年01月09日00:40:41


nginx自带是没有针对负载均衡后端节点的健康检查的,但是可以通过默认自带的ngx_http_proxy_module模块和ngx_http_upstream_module模块中的相关指令来完成当后端节点出现故障时,自动切换到健康节点来提供访问,但是还会有请求转发到后端的这台后端节点上面去

ngx_http_upstream_module是淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方服务的健康状态,如果后端服务器不可用,则所有的请求不转发到这台服务器

github项目地址:https://github.com/yaoweibin/nginx_upstream_check_module/

我们常见的nginx状态模块,状态比较少

关于监控相关文章可以阅读 监控体系

Active connections: 当前活跃的连接数
12----> 一共处理了多少个链接(请求)
12----> 成功创建多少次握手
15--> 总共创建了多少个请求
Reading:当前读取客户端heardr的数量
Writing:当前返回给客户端heardr的数量  #如果这个指标飙升,说明是后面的节点挂掉了,例如数据库等。
Waiting:大体意思是已经处理完,等待下次请求的数量
提示:我们只需要关注活动链接即可

参考地址:https://www.i4t.com/1376.html

Nginx upstream_check_module模块实现后端节点健康检查功能

upstream_check_module模块展示如下

Nginx upstream_check_module模块实现后端节点健康检查功能

宕机状态如下

Nginx upstream_check_module模块实现后端节点健康检查功能

Inde         项目排序
Upstream     upstream名称
Name         后端地址
Status       状态地址 (正常为up|异常为down)
Fall counts  down机时间 (以秒为单位)
Check type   监控类型 (默认tcp)
具体参数可以查看官方文档https://github.com/yaoweibin/nginx_upstream_check_module

Nginx 编译模块安装

1.安装依赖

yum install -y gcc glibc gcc-c++ prce-devel openssl-devel pcre-devel lua-devel libxml2 libxml2-dev libxslt-devel  perl-ExtUtils-Embed   GeoIP GeoIP-devel GeoIP-data

2.下载软件包

useradd -s /sbin/nologin nginx -M
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xf nginx-1.14.2.tar.gz

3.下载nginx模块

wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
unzip master

Nginx upstream_check_module模块实现后端节点健康检查功能

4.nginx打补丁

yum install -y patch
cd nginx-1.14.2
patch -p1 < ../nginx_upstream_check_module-master/check_1.14.0+.patch
#因为我们nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录

Nginx upstream_check_module模块实现后端节点健康检查功能

5.编译Nginx

./configure --prefix=/usr/local/nginx-1.14 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx_upstream_check_module-master

make && make install
ln -s /usr/local/nginx-1.14 /usr/local/nginx

启动测试
/usr/local/nginx/sbin/nginx

[root@yzsjhl82-118 ~]# lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   6731  root    6u  IPv4  44182      0t0  TCP *:http (LISTEN)
nginx   6732 nginx    6u  IPv4  44182      0t0  TCP *:http (LISTEN)

6.配置模块

首先需要有一个代理后端的upstream,在配置一个健康检查

监控需要配置在server标签下

[root@yzsjhl82-118 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream i4t.com {
       server 10.4.81.41:900;
       server 10.4.81.42:900;
       check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
    }


    server {
        listen       80;
        server_name  localhost;

        location / {
         proxy_pass http://i4t.com;
        }
        location /status1 {
           stub_status on;      #配置nginx内置健康检查
           access_log  off;
        }
        location /status2 {     #配置upstream_check_module模块健康检查
           check_status;
           access_log off;
           #allow SOME.IP.ADD.RESS; #可以设置允许网段访问
           #deny all;
       }
    }
}

修改完配置文件,reload即可
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

配置文件参数解释

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

interval检测间隔时间,单位为毫秒
rsie请求2次正常的话,标记此后端的状态为up
type  类型为tcp
fall表示请求5次都失败的情况下,标记此后端的状态为down
timeout为超时时间,单位为毫秒

给TA打赏
共{{data.count}}人
人已打赏
NGINX

nginx proxy_pass后的url加不加/的区别

2017-9-6 16:45:43

NGINX

ngx_http_substitutions_filter_module 模块替换正文内容和URL

2019-1-10 1:11:19

4 条回复 A文章作者 M管理员
  1. 笔者可以结合 proxy_next_upstream模块,在后端节点超时的时候客户端不会超时。
    并且可以使用nginx + consul + nginx-upsync-module 的方式动态调整upstream,降低上线的影响

  2. kubernetes

    nginx+etcd/consul+nginx_upsync-module 是后端的动态流量调整,不需要reload nginx ,健康检查还是需要的。

  3. […] 启动服务判断,如果有一台服务启动异常,将会暂停发布。通过nginx upstream_check_module会自动停止异常tomcat请求 […]

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索