云服务器下centos7.3 nginx动态负载均衡

                                nginx动态负载均衡

什么是nginx动态负载均衡呢?

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

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

这类似分布式的配置中心

什么是consul?

 

Consul是一款开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册、发现实现起来非常简单,它支持如下特性。

  服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul。

服务发现:服务消费者可以通过HTTP API或DNS方式,从Consul获取服务的IP和PORT。

故障检测:支持如TCP、HTTP等方式的健康检查机制,从而当服务有故障时自动摘除。

K/V存储:使用K/V存储实现动态配置中心,其使用HTTP长轮询实现变更触发和配置更改。

多数据中心:支持多数据中心,可以按照数据中心注册和发现服务,即支持只消费本地机房服务,使用多数据中心集群还可以避免单数据中心的单点故障。

Raft算法:Consul使用Raft算法实现集群数据一致性。

通过Consul可以管理服务注册与发现,接下来需要有一个与Nginx部署在同一台机器的Agent来实现Nginx配置更改和Nginx重启功能。我们有Confd或者Consul-template两个选择,而Consul-template是Consul官方提供的,我们就选择它了。其使用HTTP长轮询实现变更触发和配置更改(使用Consul的watch命令实现)。也就是说,我们使用Consul-template实现配置模板,然后拉取Consul配置渲染模板来生成Nginx实际配置。

什么是upsync?

百度是百度不到滴,这个据说是新浪微博开发滴nginx插件啊,会用就行。

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

什么是nginx?

这个能百度有的,百度去吧哈哈。nginx常见对吧负载均衡,反向代理,高性能服务器。高端一点就是七层负载均衡,也可以做四层负载均衡。

动态负载均衡实现方案?

这里也就不过多熬诉了,网上很多种方案,这里使用Consul+upsync+Nginx

画个图就理解了。(自己手画滴请谅解)

云服务器下centos7.3 nginx动态负载均衡

下面就进入正题了啊,,,打开服务器安装软件,我这里已经安装好了,就不演示了安装这个环境之前你先把nginx依赖库都装好。(gcc gcc-c++,PCRE库,安装SSL库,安装zlib库,安装nginx)。

一,Consul环境搭建(统一安装在 usr/local 不然我也不知道会报什么错)

云服务器下centos7.3 nginx动态负载均衡

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 出现以下信息就说明安装成功

[[email protected] 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

    ******         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.浏览器访问(你服务器ip):8500

 

 

就像下面一样。

云服务器下centos7.3 nginx动态负载均衡

 

7.使用PostMan 注册Http服务

http://服务器ip:8500/v1/catalog/register

参数1

{"Datacenter": "dc1",

 "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8080", "Service": "itmayiedu","tags": ["dev"], "Port": 8080}}

 

8,创建upsync_dump_path

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

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


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

二,安装nginx-upsync-module

2.1 安装nginx-upsync-module

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

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

unzip master.zip

unzip consul_0.7.1_linux_amd64.zip

如果解压出现该错误

-bash: unzip: 未找到命令

解决办法

yum -y install unzip

三,配置nginx

3.1配置组

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

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

mkdir -p /usr/local/nginx

Upstream 动态配置

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

   upstream whfjn{

        server 127.0.0.1:11111;

        upsync 服务器ip:8500/v1/kv/upstreams/whfjn upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

###whfjn 表示你cousul注册接口的时候的组名称

        upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;

    }

 

    server {

        listen       80;

        server_name  localhost;

 

        location / {

            proxy_pass http://whfjn;

            index  index.html index.htm;

        }

    }

3.2添加nginx  Upstream服务

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

curl -X PUT http://服务ip:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

curl -X PUT http://服务ip:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

 

2.使用postmen 发送put请求

http://服务ip:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

http://服务ip:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

负载均衡信息参数

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

3.3 启动nginx

/usr/local/nginx/sbin/nginx  (无参数) 启动    (-s  stop)关闭    (-s reload)重启

  查看是否成功
  ps -ef | grep nginx

 

云服务器下centos7.3 nginx动态负载均衡

然后查看一下upsyc配置文件滴servers.conf

云服务器下centos7.3 nginx动态负载均衡

如果出现这样恭喜你动态负载均衡成功了,可以用postman软件测试连接。如果有问题请联系博主,创作不易希望大家多多支持。