基于consul实现nginx的动态upstream

  • 基于consul实现nginx的动态upstream已关闭评论
  • 115,024 views
  • A+
所属分类:nginx

1. 相关组件

名称 公司 描述 下载地址
consul (HashiCorp)[https://www.consul.io/intro/getting-started/install.html] 用于实现分布式系统的服务发现与配置 https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip
nginx-upsync-module (微博)[https://github.com/weibocom/nginx-upsync-module]
拉取 consul 的后端 server 的列表,并更新 Nginx 的路由信息
https://github.com/weibocom/nginx-upsync-module/archive/nginx-upsync-1.8.x.zip
nginx Apache 请求转发 http://nginx.org/download/nginx-1.8.0.tar.gz

2. 简单Demo

2.1 安装并配置Consul

# 解压包
unzip consul_1.0.6_linux_amd64.zip
# 移动consul到格式位置
mv consul /root/services/account_consul/
# 启动consul
/root/services/account_consul/consul agent -server -bootstrap-expect=1 -data-dir=/root/data/consul/ -node=accounting01 -bind=172.31.132.207 -config-dir=/root/data/consul_config/ -client 0.0.0.0 -ui
# 添加upstreams配置
curl -X PUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
curl -X PUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8001

基于consul实现nginx的动态upstream

上图我们可以看出,我们已经通过http请求存储了upstreams配置信息, 接下来我们需要让Nginx从consul自动更新upstreams配置;

2.2 重新编译安装Nginx,添加upsync模块

2.2.1 install

# 解压
tar -zxvf nginx-1.8.0.tar.gz
unzip nginx-upsync-1.8.x.zip
# 编译安装
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --add-module=/root/packages/nginx-upsync-module-nginx-upsync-1.8.x
make && make install
# 检查Nginx是否按照成功
/usr/local/nginx/sbin/nginx

2.2.2 config

vim /usr/local/nginx/conf/nginx.conf
# 修改用户
(PS:由于我是root环境启动的服务, 若Nginx的用户不是root的话, 某些文件无法访问, 例如: 静态文件)
user root;
# 添加额外配置文件路径
(PS: 非必须, 但是我习惯这么做)
http {
...
include /usr/local/nginx/conf/sites-enabled/*;
server {
....
}
}
# 添加Nginx的upstream配置
(accouting_server) ➜ conf cd sites-enabled
(accouting_server) ➜ sites-enabled pwd
/usr/local/nginx/conf/sites-enabled
(accouting_server) ➜ sites-enabled ll
total 8.0K
-rw-r--r-- 1 root root 915 Feb 24 18:13 accounting
-rw-r--r-- 1 root root 213 Feb 24 18:13 consul
(accouting_server) ➜ sites-enabled cat accounting
upstream accounting {
server 127.0.0.1:11111;
upsync 172.31.132.207:8500/v1/kv/upstreams/accounting/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/accounting.conf;
}

server {
listen 80;
#server_name accounting.willdx.me;
server_name 47.104.93.126
charset utf-8;
client_max_body_size 20M;
access_log /var/log/nginx/accounting/access.log;
error_log /var/log/nginx/accounting/error.log;
location /media {
alias /root/mycode/accouting_server/accouting_project/media;
}
location /static {
alias /root/mycode/accouting_server/accouting_project/static;
}

location / {
#proxy_set_header Host $host;
#proxy_pass http://localhost:8888/;
#proxy_redirect http://0.0.0.0:8888/ /;
proxy_pass http://accounting;
}
}

2.2.3 测试

# 启动测试服务
python -m http.server 8000
python -m http.server 8001

测试1: 负载均衡

# 测试
for i in `seq 10000`; do curl http://47.104.93.126/; sleep 1; done;
# 发现请求是负载均衡的

测试2: 测试动态的更改数据之后Nginx请求

# 删除一个配置
curl -X DELETE http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
true#
# 再次测试2.2.3步骤
发现请求不走127.0.0.1:8000了
# 开启8000
curl -X PUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
# 再次测试2.2.3步骤
发现请求又走127.0.0.1:8000了

我们可以consul来进行集中的配置管理, 服务变更的时候, 我们只需要更改配置即可;

  • 我的微信
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar