Nginx + Consul + Upsync实现动态负载均衡

各组件作用:

ConsulWeb:Consul的客户端可视化界面,管理负载均衡配置的信息

ConsulServer:Consul服务端,用于存放负载均衡配置

Nginx:以间隔时间动态读取ConsulServer配置

Upsync:新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并动态更新Nginx的路由信息。此模块不依赖于任何第三方模块。Consul作为Nginx的DB,利用Consul的KV服务,每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。

1.搭建ConsulServer存放负载均衡注册配置信息

2.nginx间隔时间动态取最新的ConsulServer配置信息

1.下载文件

1.1Nginx

wget http://nginx.org/download/nginx-1.9.10.tar.gz

作用:实现反向代理、负载负载库

1.2consul

wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip

作用:对动态负载均衡均配置实现注册

1.3nginx-upsync-module

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

作用:nginx动态获取最新upstream信息

2.解压安装

unzip master.zip

unzip consul_0.7.1_linux_amd64.zip

如果解压出现该错误

-bash: unzip: 未找到命令

解决办法

yum -y install unzip

安装Nginx

解压Nginx

tar -zxvf nginx-1.9.10.tar.gz

配置Nginx

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /usr/local/nginx

编译Nginx

cd nginx-1.9.10

 

./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre --add-module=../nginx-upsync-module-master

编译的是报错

./configure: error: SSL modules require the OpenSSL library.

解决办法

yum -y install openssl openssl-devel

 

make && make install

Upstream 动态配置

 ##动态去consul 获取注册的真实反向代理地址
   upstream itmayiedu{
        server 127.0.0.1:11111;
        ### 连接consul server,获取动态upstreams,配置负载均衡信息,间隔0.5s获取配置信息
        upsync 192.168.128.149:8500/v1/kv/upstreams/itmayiedu upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        ### 动态获取consul server相关负载均衡配置信息持久化在硬盘
        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            ### 配置反向代理
            proxy_pass http://itmayiedu;
            index  index.html index.htm;
        }
    }

upsync指令指定从consul哪个路径拉取上游服务器配置;upsync_timeout配置从consul拉取上游服务器配置的超时时间;upsync_interval配置从consul拉取上游服务器配置的间隔时间;upsync_type指定使用consul配置服务器;strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时nginx启动同样失败。upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

注意:替换 consul 注册中心地址

创建upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

启动consul

临时关闭防火墙:systemctl stop firewalld

我的linux Ip地址192.168.128.149

./consul agent -dev -ui -node=consul-dev -client=192.168.128.149

添加nginx  Upstream服务

1.使用linux命令方式发送put请求

curl -X PUT http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8080

curl -X PUT http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8081

或者使用postmen 发送put请求

http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8080

http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8081

启动Nginx

/usr/local/nginx/sbin/nginx

访问http://192.168.128.149/,就有负载均衡了(本地需要启动8080和8081两个项目)

本地在启动一个8084项目:

使用postmen 发送put请求

http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8084

此时会自动读取新增的配置信息

 给8080权重为3的配置:

负载均衡信息参数

{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}

查看服务列表: 

 

猜你喜欢

转载自blog.csdn.net/qq_38270106/article/details/83239756