nginx动态同步配置模块nginx-upsync-module

使用场景简介

nginx一般直接在配置文件里配置upstream即可实现负载均衡,但有些特定的环境下此种方式就显得有些局限性。比如后台动态调整节点的时候;调整节点后不想修改配置文件重启nginx。

可以将配置文件从nginx本地迁移到其他第三方服务上如etcd、consul上,然后通过nginx-upsync-module模块实时同步到配置文件中,实现上下线节点动态同步到upstream配置中,再结合ngx_healthcheck_module实现后端健康检测。

可以将配置文件从nginx本地迁移到其他第三方服务上如etcd、consul上,然后时候拉取配置到本地。本文采用nginx-upsync-module,主要支持consul、etcd,本文以etcd为例。

0.etcd介绍

etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统,可以用于配置共享和服务的注册和发现。在这些用于存放nginx中关于upstream的配置信息。

etcd使用的2个默认端口号:2379和2380
2379:用于客户端通信
2380:用于与集群中的Peer通信

官网地址

https://github.com/etcd-io/etcd/releases

1.安装etcd(单机版)

#VERSION=3.4.20
#tar -zxvf  etcd-v${VERSION}-linux-amd64.tar.gz
#mv etcd-v${VERSION}-linux-amd64  /usr/local/etcd/
#echo 'export PATH=${PATH}:/usr/local/etcd/' >>/etc/profile
#source /etc/profile
#etcd --version
etcd Version: 3.4.20
Git SHA: 1e26823
Go Version: go1.16.15
Go OS/Arch: linux/amd64

2.启动

nohup  /usr/local/etcd/etcd  \
--name='node1' \
--enable-v2='true' \
--data-dir='/usr/local/etcd/default.etcd' \
--listen-peer-urls='http://0.0.0.0:2380' \
--initial-advertise-peer-urls='http://0.0.0.0:2380' \
--advertise-client-urls='http://0.0.0.0:2379' \
--listen-client-urls='http://0.0.0.0:2379' > /usr/local/etcd/etcd.log  2>&1 &

3.生成脚本

cat >/usr/local/etcd/start.sh<<EOF ; chmod o+x /usr/local/etcd/start.sh
#!/bin/bash
nohup  /usr/local/etcd/etcd  \
--name='node1' \
--enable-v2='true' \
--data-dir='/usr/local/etcd/default.etcd' \
--listen-peer-urls='http://0.0.0.0:2380' \
--initial-advertise-peer-urls='http://0.0.0.0:2380' \
--advertise-client-urls='http://0.0.0.0:2379' \
--listen-client-urls='http://0.0.0.0:2379' > /usr/local/etcd/etcd.log  2>&1 &
EOF
cat >/usr/local/etcd/stop.sh<<EOF ; chmod o+x /usr/local/etcd/stop.sh
#!/bin/bash
ps aux | grep etcd | grep -v "grep" | awk '{print $2}' | xargs kill > /dev/null 2>&1
EOF

4.nginx安装

安装nginx,同时安装七层后端检测模块为例

安装七层后端检测模块
cd /root
git clone https://github.com/weibocom/nginx-upsync-module.git
git clone https://github.com/xiaokai-wang/nginx_upstream_check_module.git
rpm -qa | egrep "pcre|pcre-devel|openssl|openssl-devel|zlib|zlib-devel"
yum install  pcre pcre-devel  opessl  openssl-devel  zlib  zlib-devel -y
wget  http://nginx.org/download/nginx-1.16.0.tar.gz
tar xzvf nginx-1.16.0.tar.gz && cd nginx-1.16.0
#nginx安装补丁包
patch -p1 < /root/nginx_upstream_check_module-master/check_1.12.1+.patch
#nginx安装
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream \
--add-module=/root/nginx_upstream_check_module-master \
--add-module=/root/nginx-upsync-module
make && make install

5.配置说明

server {
        listen 80;
        …………
        location / {
        …………
        proxy_pass http://tomcat-cluster;
        }   
}
upstream tomcat-cluster {
server 127.0.0.1:11111;
upsync 127.0.0.1:2379/v2/keys/upstreams/test upsync_timeout=6m upsync_interval=500ms 	upsync_type=etcd strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/upstream.conf;
include /usr/local/nginx/conf/upstream.conf;
    …………
}

语法参数

server 127.0.0.1:11111;本机upsync工作虚拟端口

127.0.0.1:2379/v2/keys/upstreams/test, etcd服务器同步路径

  • upsync_interval=6m;每隔一段时间从consul/etcd中拉出服务器
  • upsync_timeout=500ms,从consul/etcd请求中提取服务器超时。
  • upsync_type=etcd,从拉取服务器类型:consul,etcd。
  • strong_dependency=off,每次在nginx启动或重新加载时,nginx是否从consul/etcd中拉出配置,on,拉取

off,不拉取配置

  • upsync_dump_path /usr/local/nginx/conf/upstream.conf, 同步存储配置文件路径
  • include /usr/local/nginx/conf/upstream.conf, 加载配置文件路径

最终实际配置

http {
    server {
        listen 80;
        # status interface
        location /status {
        check_status;
        }
        # http front
        location / {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://tomcat-cluster;
       }   
    }
    upstream tomcat-cluster {
        server 127.0.0.1:11111;
        upsync 127.0.0.1:2379/v2/keys/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=etcd strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/upstream.conf;
        include /usr/local/nginx/conf/upstream.conf;
        check interval=3000 rise=2 fall=5 timeout=5000 type=http;
        check_http_send "GET / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }
}

6.启动服务

/usr/local/nginx/sbin/nginx

7.后端服务添加配置

后端web服务上线后,需要向etcd中添加注册信息,添加成功以后,nginx反代自动从etcd中拉取配置,实现后端服务上线自动添加至配置中。

后端web服务新上线

1.增加服务器节点语法

curl -X PUT -d value="{"weight":1, "max_fails":2, "fail_timeout":10, "down":0, "backup":0}"
http://etcdip:etcdi​p:port/v2/keys/dir1/dir1/upstream_name/backendip:backendi​p:backend_port

curl -X PUT -d value="{"weight":1, "max_fails":2, "fail_timeout":10, "down":0, "backup":0}"  \
http://192.168.241.10:2379/v2/keys/upstreams/test/127.0.0.1:8080

后端web服务新下线

2.删除服务节点

curl -X DELETE http://etcdip:etcdi​p:port/v2/keys/upstreams/upstreamname/upstreamn​ame/backend_ip:$backend_port

curl -X DELETE http://192.168.241.10:2379/v2/keys/upstreams/test/127.0.0.1:8086

3.调整服务参数

curl -X PUT -d value="{"weight":2, "max_fails":2, "fail_timeout":10, "down":0, "backup":0}"  \
http://192.168.241.10:2379/v2/keys/upstreams/test/127.0.0.1:8082

4.检测

curl http://etcdip:etcdi​p:port/v2/keys/upstreams/$upstream_name

curl  http://192.168.241.10:2379/v2/keys/upstreams/test

5.查看nginx中的配置文件

cat /usr/local/nginx/conf/upstream.conf

千锋教育Java入门全套视频教程(java核心技术,适合java零基础,Java自学必备)

猜你喜欢

转载自blog.csdn.net/longz_org_cn/article/details/132223801