介绍
Nginx Upstream配置动态更新已经有很多开源方案,大多数都是基于生成配置文件后进行reload(本博客中也有介绍),但是reload这个操作在业务流量不大的时候是可以进行的,但是在一些高流量的站点,随便进行reload会对整个集群的性能造成抖动,这个抖动问题,要追究起来可大可小,不过总有人希望有个完美的方案来避免这个抖动,weibo的nginx-upsync-module模块就是为解决这个问题而生的。
能搜索到这篇文章的,详细你也是有同样的需求的,我也就不废话了,开战。
角色
主机 | consul | nginx upstream |
---|---|---|
172.31.68.243 | server | up1 |
172.31.68.244 | client | up1 |
原理
官方地址:nginx-upsync-module
Consul部署
下载(243/244)
cd /opt/programs
wget 'https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip?
_ga=1.178443949.807959168.1488883479'
unzip consul_0.7.5_linux_amd64.zip
ps:第一次玩consul的还是3年前,一晃时间真快....
Server启动(244)
cd /opt/programs
./consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=DC1 -bind=172.31.68.244 -client=172.31.68.244
Client启动(243)
cd /opt/programs
./consul agent -data-dir /tmp/consul -node=DC2 -bind=172.31.68.243 -join=172.31.68.244 -client=172.31.68.243
Nginx部署
下载(243)
#upsync模块下载
cd /opt/soft/ngx_module/
git clone https://github.com/weibocom/nginx-upsync-module.git
#nginx下载
cd /opt/programs
wget 'https://nginx.org/download/nginx-1.10.3.tar.gz'
tar zxvf nginx-1.10.3.tar.gz
编译安装(243)
cd /opt/programs
./configure --prefix=/opt/programs/nginx_consul --add-dynamic-module=/opt/soft/ngx_module/nginx-upsync-module/
make
make install
ps:安装完成的nginx复制至244,因为244也要作为upstram的后端进行测试
Nginx配置
nginx配置目录
ps:主要配置文件在conf.d和upstream.d目录下面
243-nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
load_module modules/ngx_http_upsync_module.so;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include upstream.d/*.conf;
include conf.d/*.conf;
243-conf.d/default.conf
server {
listen 80;
server_name localhost;
access_log logs/host.access.log main;
location / {
proxy_pass http://up1;
}
location = /upstream_show {
upstream_show;
}
}
243-conf.d/up1.conf
server {
listen 8000;
server_name localhost;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
244-nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
load_module modules/ngx_http_upsync_module.so;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf;
}
244-conf.d/up1.conf
server {
listen 8000;
server_name localhost;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
启动Nginx(243/244)
#243 && 244
cd /opt/programs/nginx_consul/sbin
./nginx
测试
Consul添加243节点
curl -X PUT http://172.31.68.244:8500/v1/kv/upstreams/up1/172.31.68.243:8000
Consul查看243节点信息
curl -s http://172.31.68.244:8500/v1/kv/upstreams/up1?recurse
测试节点
curl http://172.31.68.243/
curl http://172.31.68.243/upstream_show
Consul添加244新节点
curl -X PUT http://172.31.68.244:8500/v1/kv/upstreams/up1/172.31.68.244:8000
测试新节点
下线243节点
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":1}' http://172.31.68.244:8500/v1/kv/upstreams/up1/172.31.68.243:8000
测试节点
上线243节点
curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}' http://172.31.68.244:8500/v1/kv/upstreams/up1/172.31.68.243:8000
再次测试