目录
1 nginx_upstream_check_module
可以利用第三方Nginx插件监控代理后端节点的服务器。
淘宝技术团队开发了一个Tengine(Nginx的分支)模块nginx_upstream_check_module,用于提供主动式后端服务器健康检查。通过它可以检测后端realserver的健康状态,如果后端的realserver不可用,则所有的请求就不会转发到该节点上。
Tengine原生支持这个模块,而Nginx则需要通过打补丁的方式将该模块添加到Nginx中。
补丁下载地址:
需要在上一篇文章基础之上实现
环境准备
Hostname | IP | 说明 |
---|---|---|
lb01 | 192.168.90.5 | Nginx主负载均衡器 |
lb02 | 192.168.90.6 | Nginx辅负载均衡器 |
web01 | 192.168.90.8 | web01服务器 |
web02 | 192.168.90.7 | web02服务器 |
VIP:192.168.90.3
2 Nginx中加载并配置此模块
# 在负载lb01中测试
[root@lb01 ~]# cd /home/oldboy/tools/
[root@lb01 tools]# wget -q https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
[root@lb01 tools]# ls
master nginx-1.6.3 nginx-1.6.3.tar.gz
[root@lb01 tools]# unzip master
[root@lb01 tools]# ls
master nginx-1.6.3 nginx-1.6.3.tar.gz nginx_upstream_check_module-master
# 由于是对源程序打补丁,所以还需要nginx软件包
[root@lb01 tools]# cd nginx-1.6.3
[root@lb01 nginx-1.6.3]# patch -p1 ../nginx_upstream_check_module-master/check_1.5.12+.patch
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
# 编译参数要和以前一致
[root@lb01 nginx-1.6.3]# ./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/home/oldboy/tools/nginx_upstream_check_module-master/ --prefix=/application/nginx-1.6.3/
# 如果是新装的nginx则继续执行下面make install一步,如果给已经安装的nginx系统打监控补丁就不用执行make install了,这里直接make就行
# make 的作用就是重新生成Nginx二进制启动命令而已。
make
# 操作前最好先备份
[root@lb01 nginx-1.6.3]# mv /application/nginx/sbin/nginx{,.ori}
# 将打过补丁的nginx二进制程序复制到/application/nginx/sbin/目录下
[root@lb01 nginx-1.6.3]# cp ./obj/nginx /application/nginx/sbin
# 检测nginx语法是否正常
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -v # 查看版本
# 修改配置文件如下,注意location中的default_pools要删掉,否则会跳转到10.0.0.7中的主页面
[root@lb01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools{
server 192.168.90.7:80 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}
upstream upload_pools{
server 192.168.90.7:8080 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}
upstream default_pools{
server 192.168.90.8:80 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}
server {
listen 80;
server_name www.rsq.com;
location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}
location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
location /status {
check_status;
access_log off;
}
}
}
[root@lb01 nginx-1.6.3]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
上面配置的意思是,对三个负载均衡pools条目中的所有节点,每隔3秒检测一次,请求2次正常则标记realserver状态为up,如果检测5次都失败,则标记realserver的状态为down,超市时间为1秒,检查的协议是http。
详细用法可参见官网
http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
3 web页面测试
把web02中的nginx服务停掉,过一会刷新看结果
基于nginx_upstream_check_module此模块可以实现对Nginx Web节点(web01和web02)进行健康检查