提供负载均衡器内节点的健康检查的。这个就是淘宝技术团队开发的 nginx 模块 nginx_upstream_check_module,
通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则所以的请求就不会转发到该节点上。
在淘宝自己的 tengine 上是自带了该模块的,大家可以访问淘宝tengine的官网来获取该版本的nginx,官方地址: 
http://tengine.taobao.org/ 。

下载 nginx_upstream_check_module模块

[root@localhost ~]# cd /usr/local/src
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
unzip master
[root@localhost /usr/local/src]# ll -d nginx_upstream_check_module-master
drwxr-xr-x. 6 root root 4096 Dec  1 02:28 nginx_upstream_check_module-master
2、为nginx打补丁

[root@localhost /usr/local/src]# cd nginx-1.6.0 # 进入nginx的源码目录
[root@localhost nginx-1.6.0]# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
[root@localhost nginx-1.6.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.0 
--with-http_ssl_module --with-openssl=/usr/local/src/openssl-0.9.8q --with-pcre=/usr/local/src/pcre-8.32 
--add-module=/usr/local/src/nginx_concat_module/ --add-module=../nginx_upstream_check_module-master/
make (注意:此处只make,编译参数需要和之前的一样)
[root@localhost nginx-1.6.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.6.0.bak
[root@localhost nginx-1.6.0]# cp ./objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.6.0]# /usr/local/nginx/sbin/nginx -t  # 检查下是否有问题
[root@localhost nginx-1.6.0]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
3、在nginx.conf配置文件里面的upstream加入健康检查,如下:

upstream name {
       server 192.168.0.21:80;
       server 192.168.0.22:80;
       check interval=3000 rise=2 fall=5 timeout=1000 type=http;

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

这里列出 nginx_upstream_check_module 模块所支持的指令意思:


Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false]
 [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]Default: 如果没有配置参数,默认值是:interval=30000 fall=5 
 rise=2 timeout=1000 default_down=true type=tcpContext: upstream
该指令可以打开后端服务器的健康检查功能。

指令后面的参数意义是:

  - 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: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查
  80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
Syntax: check_keepalive_requests request_numDefault: 1Context: upstream
该指令可以配置一个连接发送的请求数,其默认值为1,表示Tengine完成1次请求后即关闭连接。

Syntax: check_http_send http_packetDefault: "GET / HTTP/1.0\r\n\r\n"Context: upstream
该指令可以配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用"HEAD"方法。

当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 
同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或
网络异常。

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]Default: http_2xx | http_3xxContext: 
upstream
该指令指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。

Syntax: check_shm_size sizeDefault: 1MContext: http
所有的后端服务器健康检查状态都存于共享内存中,该指令可以设置共享内存的大小。默认是1M,如果你有1千台以上的服务器并在配置的时候
出现了错误,就可能需要扩大该内存的大小。

Syntax: check_status [html|csv|json]Default: check_status htmlContext: location
显示服务器的健康状态页面。该指令需要在http块中配置。

在Tengine-1.4.0以后,你可以配置显示页面的格式。支持的格式有: html、csv、 json。默认类型是html。

你也可以通过请求的参数来指定格式,假设‘/status’是你状态页面的URL, format参数改变页面的格式,比如:

/status?format=html
/status?format=csv
/status?format=json
同时你也可以通过status参数来获取相同服务器状态的列表,比如:

/status?format=html&status=down
/status?format=csv&status=up
下面是一个状态也配置的范例:

http {
      server {
       location /nstatus {
         check_status;
         access_log off;
         #allow IP;         #deny all;       }
      }
}
配置完毕后,重启nginx。此时通过访问定义好的路径,就可以看到当前 realserver 实时的健康状态啦。效果如下图: 
realserver 都正常的状态:

wKiom1SZZXKQcPJTAAFnMqUEfBo238.jpg

一台 realserver 故障的状态:

wKioL1SZZivDAzdWAAGTyIK9cS8558.jpgOK,以上nginx_upstream_check_module模块的相关信息,更多的信息大家可以去该模块的淘宝
tengine页面和github上该项目页面去查看,下面是访问地址:

http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

https://github.com/yaoweibin/nginx_upstream_check_module

在生产环境的实施应用中,需要注意的有 2 点:

1、主要定义好type。由于默认的type是tcp类型,因此假设你服务启动,不管是否初始化完毕,它的端口都会起来,所以此时前段负载均衡器
为认为该服务已经可用,其实是不可用状态。

2、注意check_http_send值的设定。由于它的默认值是"GET / HTTP/1.0\r\n\r\n"。假设你的应用是通过http://ip/name访问的,那么
这里你的 check_http_send值就需要更改为 "GET /name HTTP/1.0\r\n\r\n"才可以。针对采用长连接进行检查的, 这里增加keep-alive
请求 头,即"HEAD /name HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。如果你后端的tomcat是基于域名的多虚拟机,此时你需要通
过 check_http_send定义host,不然每次访问都是失败,范例:check_http_send "GET /mobileapi HTTP/1.0\r\n HOST www.redhat.sx
\r\n\r\n";

三、 ngx_http_healthcheck_module模块

除了上面两个模块,nginx官方在早期的时候还提供了一个ngx_http_healthcheck_module  模块用来进行 nginx后端节点的健康检查。
nginx_upstream_check_module模块就是参照 该模块的设计理念进行开发的,因此在使用和效果上都大同小异。但是需要注意的是, 
ngx_http_healthcheck_module  模块仅仅支持nginx的1.0.0版本,1.1.0版本以后都不支持了!因此,对于目前常见的生产环境上都不
会去用了,这里仅仅留个纪念,给大家介绍下这个模块!

具体的使用方法,这里可以贴出几篇靠谱的博文地址以及官方地址:

http://wiki.nginx.org/HttpHealthcheckModule

https://github.com/cep21/healthcheck_nginx_upstreams/blob/master/README

OK!

以上就是本文的内容,希望能够对51博友有所帮助!


来源: http://www.tuicool.com/articles/vuiQry

results matching ""

    No results matching ""