nginx后端节点的健康检测
2018-01-30 11:26
609 查看
背景
随着使用年限的增加,服务器性能逐渐下降,现公司对业务线变通,预计使用nginx做负载均衡(版本:1.6.2),同时想让nginx 支持后端服务器的健康检测,即后端服务器如果挂了,自动将其从集群中排除。如果服务器恢复了,自动加入集群。正式部署之前,在测试环境实验一二~~~
前期准备
明确环境
(1)DR:10.10.10.100 nginx版本:1.6.2;
RS1:10.10.10.131 nginx/1.13.8;
RS2:10.10.10.133 nginx/1.13.8;
(2)
所有压缩包及其解压出来的文件位于/usr/local/src目录下;
所有安装目录为/usr/local,即nginx位于/usr/local/nginx;
下载nginx1.6.2
下载健康检测模块查阅健康检测文档http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html 可知健康检测模块需要另外下载,其中
doc目录中的note文件说明了打补丁规则
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip unzip nginx_upstream_check_module/archive/master.zip #用于解压缩“.zip”压缩包
下载nginx1.6.2
rm -rf /usr/localnginx /usr/local/src/nginx1.13.4 #删除原来的nginx目录 wget http://nginx.org/download/nginx‐1.6.2.tar.gz tar -xf nginx‐1.6.2.tar.gz [root@bogon nginx-1.6.2]# patch -p1 -u < /usr/local/src/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 #可以查看修改情况 ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx_upstream_check_module-master/ make && make install nginx -V #检测 nginx version: nginx/1.6.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
配置
#以下配置文件有些不必要部分已经省略,注意行数 #DR 35 upstream test { #位于http区块 36 # ip_hash; #实际应用中需要保证session一致; 37 server 10.10.10.131; #基于IP将http请求转交给RS,负载均衡 38 server 10.10.10.133; 39 check interval=3000 rise=2 fall=4 timeout=4000; #每3s检测一次,检查2次ok则恢复,检查4次down后端节点则切换,超时时间为3s 40 } 44 server { 45 listen 80; #DR不需要server name 54 55 location / { 58 proxy_pass http://test; #test为upstream模块名 59 } 60 61 location /status { 62 check_status; #可以监控RS状态 63 access_log off; 64 allow all; 65 } 66 #RS1(RS2类似,此处省略) 35 server { #当我们通过ip访问web服务器时,不管有多少台虚拟主机,访问到的网页都是显示第一台提供的网页; 36 listen 80; 37 server_name www.test1.com; 44 access_log logs/test1.access.log; 45 error_log logs/test2.error.log; 46 47 location / { 48 root /data/test1; 49 index index.html; 50 } nginx #开启nginx服务 nginx -t #重读配置文件 nginx -s reload #注意iptables和selinux需要放行 #问题 [root@bogon conf]# nginx -t nginx: [emerg] invalid parameter "interval" in /usr/local/nginx/conf/nginx.conf:40 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed 错解:check interval = 3000 解决:注意空格,=两边不需要空格
测试
测试RS1和RS2
(1)windows下修改hosts:C:\Windows\System32\drivers\etc;(2)在浏览器输入www.test1.com www.test2.com测试; #也可以不修改host直接输入ip测试
测试DR
linux下elinks 10.10.10.100第一次
第二次
down掉RS2
第一次第二次
查看RS状态
思考
1、负载均衡使用什么算法?为方便测试,此处使用nginx默认的分配方式轮询。这种算法是按顺序轮流发送到RS上,并未考虑服务器的性能,且会出现session丢失的情况。
实际实际应用中,应该根据服务器性能和业务需求选择合适的分配方式,比如增加:weight=xxx;
2、如何处理session不一致?
初步了解,可使用如下方法:
(1)ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题;
(2)memcached/redis:分布式高速缓存;
3、优化方案
有兴趣的朋友可以搭建一个负载均衡架构玩玩:LVS+keepalived(前端)+nginx(后端)做的负载均衡,LVS使用wrr(加权轮训)算法和TUN(隧道)模式,配置智能DNS做一套approute
(1)思考过程中,发觉挂掉和恢复切换期间,需要给出某些提示比较好,可在配置文件中增加“rerurn 状态码“参数;
(2)考虑到此种情形下,若DR死亡,所有RS也将无法工作,为了增加DR的容灾能力,需要配置keepalive;
(3)upstream模块使用ip_hash或者weight分配方式;
参考文档
http://nginx.org/download/ #nginx所有版本下载源https://github.com/yaoweibin/nginx_upstream_check_module/tree/master/doc #github健康检测文档
http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html #nginx官网健康检测文档
http://www.360doc.com/content/14/1225/14/7635_435663893.shtml #关于session问题
相关文章推荐
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- Nginx1.12.1后端节点健康检测+会话保持方案
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx后端节点的健康检测
- nginx下后端realserver健康检测模块ngx_http_upstream_check_module
- Nginx后端节点健康检查配置使用详情
- Nginx对后端节点健康检查
- Nginx后端节点健康检查
- Nginx实战之--后端节点健康检查
- nginx后端节点服务器健康检查二
- Nginx实战系列之功能篇----后端节点健康检查