nginx负载均衡的健康检查

前言:我们使用nginx最常用到的功能恐怕就是负载均衡了,配置负载均衡主要是要使用 upstream 指令了

配置如下所示:
image
那么如果这其中有一台端口不通了,怎么办,nginx会不会自动剔除呢?我们就需要使用nginx的健康检查功能了

一 Nginx(自带)有健康检查模块:ngx_http_upstream_module-被动检查

这个模块是不需要额外安装的,nginx自带的这个模块
常见的配置如下:

默认nginx检查
- weight=number 权重值,默认为1;
- max_conns=number 上游服务器的最大并发连接数;
- fail_timeout=time 服务器不可用的判定时间;
- max_fails=numer 服务器不可用的检查次数;
- backup 备份服务器,仅当其他服务器都不可用时才会启用;
- down 标记服务器长期不可用,离线维护;

举例:server 172.19.149.196:56666 max_fails=10 fail_timeout=60s weight=1;

缺点:Nginx只有当有访问时后,才发起对后端节点探测。如果本次请求中,节点正好出现故障,Nginx依然将请求转交给故障的节点,然后再转交给健康的节点处理。所以不会影响到这次请求的正常进行。但是会影响效率,因为多了一次转发,而且自带模块无法做到预警。

二 nginx_upstream_check_module模块对后端节点做主动健康检查

这个模块是一个第三方模块,需要额外的手动安装

2.1 在已经安装好的nginx如何添加这个模块

在已安装nginx情况下安装nginx模块(不需要make install,只需要make)
image

2.1.1 下载nginx_upstream_check_module模块

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

2.1.2 为nginx打补丁

如果没有patch,需要先安装patch命令
yum install patch
cd /soft/nginx-1.24.0
patch -p1 < /soft/nginx_upstream_check_module-master/check_1.20.1+.patch 

如下所示:
image

2.1.3 编译并拷贝nginx命令

cd /soft/nginx-1.24.0


./configure --prefix=/usr/local/nginx/ --conf-path=/usr/local/nginx/config/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_sub_module --with-http_realip_module --with-http_v2_module --with-stream --add-module=/soft/nginx_upstream_check_module-master

make (不需要执行make install)
 rm -rf /usr/local/nginx/sbin/nginx
 cd /soft/nginx-1.24.0  && cp -r objs/nginx /usr/local/nginx/sbin/

检查模块是否成功安装
image

2.2 全新的nginx添加模块

这个过程跟上面的步骤完全一致,只是多了一个make install的过程

2.3 检测类型

2.3.1 http检测

upstream test{
                server 192.168.1.221:80 weight=5 max_fails=3 fail_timeout=10s;
                server 192.168.1.222:80 weight=5 max_fails=3 fail_timeout=10s;
                check interval=3000 rise=2 fall=5 timeout=1000 type=http;
     }





    location / {
                root   html;
                index  index.html index.htm;
                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_pass http://test;

          }
    location /status {
                check_status;
                access_log off;
        }


上面配置的意思是,对test这个负载均衡条目中的所有节点,每隔3秒检测一次,请求2次正常则标记 realserver状态为up,如果检测 5 次都失败,则标记 realserver的状态为down,超时时间为1秒。
image

2.3.2 tcp检测

upstream test{
                server 192.168.2.178:56666 weight=5 max_fails=3 fail_timeout=10s;
                server 192.168.2.178:19999 weight=5 max_fails=3 fail_timeout=10s;

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

server {

    listen 80;
    server_name 192.168.2.41;
    root   /usr/local/nginx/html;
    index  index.html index.htm;
    error_log /usr/local/nginx/logs/testerror.log;
    location /abc {
               # root   /usr/local/nginx/html;
               # index  index.html index.htm;
                proxy_set_header X-Real-IP        $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_pass http://test;
    }

    location /status {
                check_status;
                access_log off;
        }

}

image

2.3.3 指标介绍

index:节点

upstream:负载均衡的名字

name:负载的节点ip信息

status:节点状态,up是可用,down是不可用

rise counts:探测次数

fall counts:满足次数

check type:探测协议是什么,分为http和tcp两种

check post:指定后端服务器中的检查端口,它可以与原始服务器端口不同,默认端口为 0,表示与原始后端服务器相同

posted @ 2024-04-12 09:33  huningfei  阅读(579)  评论(0编辑  收藏  举报
levels of contents