Nginx负载均衡+监控状态检测

想用Nginx或者Tengine替代LVS,即能做七层的负载均衡,又能做监控状态检测,一旦发现后面的realserver挂了就自动剔除,恢复后自动加入服务池里,可以用Tengine的ngx_http_upstream_check_module模块。该模块在Tengine-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_upstream_check_module。

Nginx.conf 配置

http {     upstream fire_server{     ip_hash;     server 192.168.1.1:80;     server 192.168.1.2:80;      check interval=3000 rise=2 fall=5 timeout=1000 type=http ;     check_http_send "GET /status.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";     check_http_expect_alive http_2xx http_3xx ;     }      server {         listen       80;         server_name  localhost default;          location / {             proxy_pass http://fire_server;             access_log logs/fire_server_access.log main;             error_log logs/error.log debug;         }          error_page   500 502 503 504  /50x.html;         location = /50x.html {             root   html;         }     }    } 

  • 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 {             root html;             access_log logs/access.log main;         } 

后端realserver配置,只需要保证 curl能访问到即可。

测试


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

2015/04/04 22:00:42 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80 2015/04/04 22:00:43 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80 2015/04/04 22:00:44 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80 ... enable check peer: 192.168.1.1:80