想用Nginx或者Tengine替代LVS,即能做七层的负载均衡,又能做监控状态检测,一旦发现后面的realserver挂了就自动剔除,恢复后自动加入服务池里,可以用Tengine的ngx_http_upstream_check_module模块。本文主要介绍在工作中,搭建遇到问题及处理方法,便以后查询。

首先,我们大多数站点都是nginx+tomcat这个比较常见模式,其实nginx本身也有自己的健康检测模块,本人觉得不好用,故使用ngx_http_upstream_check_module。

nginx版本:1.8.0

tomcat: 1.6

因之前已经安装了nginx,所以要增加此模块,需要给nginx打补丁:

1、下载模块

  #cd /usr/local/src

  #wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz

  #tar zxvf v0.3.0.tar.gz

  #mv nginx_upstream_check_module-0.3.0 nginx_upstream_check_module

2、为nginx打补丁

  #cd /opt/software/nginx-1.8.0

  #patch -p1 < /opt/software/nginx_upstream_check_module/check_1.7.2+.patch

  #./configure --user=www --group=www --add-module=/opt/software/ngx_devel_kit --add-module=/opt/software/lua-nginx-module --prefix=/opt/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_secure_link_module --with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-pcre=/opt/software/pcre-8.36 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1p --with-google_perftools_module --add-module=/usr/local/src/ModSecurity/nginx/modsecurity/ --add-module=/opt/software/nginx_upstream_check_module/

  #make (备注:此编译要和之前一样)

  #mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.old

  #cp ./objs/nginx /opt/nginx/sbin/

  #/opt/nginx/sbin/nginx -t(检查是否有问题)

  #kill -USR2 `cat /var/run/nginx.pid`

3、在nginx.conf配置文件里upstream中加入健康检测

  

upstream www {


         server 10.1.1.22:38080;

         server 10.1.1.22:38081;

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

         check_http_send "GET / HTTP/1.1\r\nHost: www.baidu.cn\r\n\r\n";



check interval 指令可以打开后端服务器的健康检查功能。

指令后面的参数意义是:


interval:向后端发送的健康检查包的间隔。

fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。

rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。

timeout: 后端健康请求的超时时间。

default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。

type:健康检查包的类型,现在支持以下多种类型

    tcp:简单的tcp连接,如果连接成功,就说明后端正常。

    ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。

    http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。

    mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。

    ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。

port: 指定后端服务器的检查端口。

check_http_send 指令

该指令可以让负载均衡器模拟向后端realserver发送,监控检测的http包,模拟LVS的检测。

check_http_expect_alive 指令

check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]

返回指定HTTP code,符合预期就算检测成功


realserver配置


        location = /status.html {

            proxy_pass http://www;

            access_log logs/access.log main;

        }

后端realserver配置,只需要保证 curl http://realserver/status.html 能访问到即可。


测试


移除realserver的status.html即可模拟服务不可用,负载均衡器会在N次检测后发现realserver不服务,error_log里会打印。移回status.html即立马恢复服务。