nginx+upsync+consul实现动态的负载均衡

1、什么是动态负载均衡

传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,

因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化、动态化,无需人工重新加载nginx.conf。

这类似分布式的配置中心

2、Consul环境搭建

1.下载consul_0.7.5_linux_amd64.zip

wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

2.解压consul_0.7.5_linux_amd64.zip

unzip consul_0.7.5_linux_amd64.zip

如果解压出现该错误

-bash: unzip: 未找到命令

解决办法

yum -y install unzip

 

3. 执行以下 ./consul 出现以下信息就说明安装成功

[root@localhost soft] ./consul

usage: consul [--version] [--help] <command> [<args>]

Available commands are:

    agent          Runs a Consul agent

    configtest     Validate config file

    event          Fire a new event

    exec           Executes a command on Consul nodes

    force-leave    Forces a member of the cluster to enter the "left" state

    info           Provides debugging information for operators

    join           Tell Consul agent to join cluster

    keygen         Generates a new encryption key

    keyring        Manages gossip layer encryption keys

    kv             Interact with the key-value store

    leave          Gracefully leaves the Consul cluster and shuts down

    lock           Execute a command holding a lock

    maint          Controls node or service maintenance mode

    members        Lists the members of a Consul cluster

    monitor        Stream logs from a Consul agent

    operator       Provides cluster-level tools for Consul operators

    reload         Triggers the agent to reload configuration files

    rtt            Estimates network round trip time between nodes

    snapshot       Saves, restores and inspects snapshots of Consul server state

    version        Prints the Consul version

    watch          Watch for changes in Consul

 

4.启动consul

我的linux Ip地址192.168.212.131

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

 

5.临时关闭防火墙systemctl stop firewalld

 

6.浏览器访问192.168.212.131:8500

7.使用PostMan 注册Http服务

http://192.168.212.131:8500/v1/catalog/register

参数1

{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8081", "Service": "itmayeidu","tags": ["dev"], "Port": 8080}}

参数2

{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8081", "Service": "itmayeidu","tags": ["dev"], "Port": 8081}}


Datacenter指定数据中心,Address指定服务IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、预发环境等),Service.Port指定服务端口。

 

7.发现Http服务

http://172.20.101.111:8500/v1/catalog/service/item_jd_tomcat

 

3、解压Nginx

 

tar -zxvf nginx-1.9.10.tar.gz

 

4、配置Nginx

 

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

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

mkdir -p /usr/local/nginx

 

5、编译Nginx

 

cd nginx-1.9.0

 

./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

make && make install

 

编译的是报错

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

解决办法

yum -y install openssl openssl-devel

6、Upstream 动态配置

  ##动态去consul 获取注册的真实反向代理地址

   upstream itmayiedu{

        server 127.0.0.1:11111;

        upsync 192.168.212.134:8500/v1/kv/upstreams/itmayiedu upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

        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.212.131

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

 

添加nginx  Upstream服务

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

curl -X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

curl -X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

 

2.使用postmen 发送put请求

http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081 http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

 

负载均衡信息参数

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

posted @ 2019-04-08 09:52  柚子味儿的西瓜  阅读(22)  评论(0)    收藏  举报