1 什么是动态负载均衡
传统的负载均衡,如果 Upstream
参数发生变化,每次都需要重新加载nginx.conf
文件,因此扩展性不是很高,所以我们可以采用动态负载均衡,实现 Upstream
可配置化
、动态化
,无需人工重新加载
nginx.conf
。
这类似分布式的配置中心。
2 动态负载均衡实现方案
每次发现配置更改需要 reload nginx,重启 Nginx。
Consul
+Consul-template
Consul
+OpenResty
实现无需 reload 动态负载均衡Consul
+upsync
+Nginx
实现无需 reload 动态负载均衡
3 常用服务器注册与发现框架
常见服务发现框架 Consul
、Eureka
、 ZooKeeper
以及 Etcd
。
ZooKeeper
是这种类型的项目中历史最悠久的之一,它起源于 Hadoop
。它非常成熟、可靠,被许多大公司(YouTube、eBay、雅虎等)使用。
etcd
是一个采用 HTTP
协议的 键/值
对存储系统,它是一个分布式和功能层次配置系统,可用于构建服务发现系统。其很容易部署、安装和使用,提供了可靠的数据持久化特性。它是安全的并且文档也十分齐全。
4 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
实际配置。
5 Nginx
+ Consul
+ UpSync
原理
ConsulServer
用于存放负载均衡配置
ConsulWeb
作为可视化的配置文件修改界面
Nginx
会通过UpSync
间隔读取ConsulServer
上面的配置文件,同时会在本地缓存一份配置文件,以免配置中心宕机导致服务不可用。
6 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
出现以下信息就说明安装成功
./consul
[root@weaver-1 ~]# ./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
[root@weaver-1 ~]#
4. 启动
consul
./consul agent -dev -ui -node=consul-dev -client=192.168.153.11
5. 临时关闭防火墙
systemctl stop firewalld
6. 浏览器访问:http://192.168.153.11:8500/
7. 使用 PostMan 注册 Http 服务
http://192.168.153.11:8500/v1/catalog/register
{
"Datacenter":"dc1",
"Node":"tomcat",
"Address":"192.168.153.11",
"Service":{
"Id":"192.168.153.11:8001",
"Service":"test",
"tags":[
"dev"
],
"Port":8001
}
}
{
"Datacenter":"dc1",
"Node":"tomcat",
"Address":"192.168.153.11",
"Service":{
"Id":"192.168.153.11:8002",
"Service":"test",
"tags":[
"dev"
],
"Port":8002
}
}
Datacenter
:指定数据中心Address
:指定服务IPService.Id
:指定服务唯一标识Service.Service
:指定服务分组Service.tags
:指定服务标签(如测试环境、预发环境等)Service.Port
:指定服务端口。
7. 发现 Http 服务
http://192.168.153.11:8500//v1/catalog/service/item_jd_tomcat
7 nginx-upsync-module
注意:清除之前
Nginx
环境,重新安装。
7.1 nginx-upsync-module
简介
Upsync
是新浪微博开源的基于 Nginx
实现动态配置的三方模块。Nginx-Upsync-Module
的功能是拉取 Consul
的后端 server
的列表,并动态更新 Nginx
的路由信息。此模块不依赖于任何第三方模块。Consul
作为 Nginx
的 DB
,利用 Consul
的 KV
服务,每个 Nginx Work
进程独立的去拉取各个 upstream
的配置,并更新各自的路由。
7.2 nginx-upsync-module
安装
下载文件
cd /usr/local/
1. 下载
Nginx
wget http://nginx.org/download/nginx-1.9.10.tar.gz
作用:实现反向代理、负载负载库
2.下载
consul
wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip
作用:对动态负载均衡均配置实现注册
3. 下载
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
解压
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
make && make install
编译的是报错
./configure: error: SSL modules require the OpenSSL library.
解决办法
yum -y install openssl openssl-devel
Upstream
动态配置
## 动态去consul 获取注册的真实反向代理地址
upstream test {
server 127.0.0.1:11111;
upsync 192.168.153.11:8500/v1/kv/upstreams/test 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://test;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
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
/usr/local/consul agent -dev -ui -node=consul-dev -client=192.168.153.11
启动两台
Tomcat
服务
/root/tomcat-1/bin/startup.sh
/root/tomcat-2/bin/startup.sh
启动
Nginx
/usr/local/nginx/sbin/nginx
添加
nginx
Upstream
服务
- 使用
postmen
发送put
请求
http://192.168.153.11:8500/v1/kv/upstreams/test/192.168.153.11:8001
http://192.168.153.11:8500/v1/kv/upstreams/test/192.168.153.11:8002
测试:
修改负载均衡信息参数
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}
{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}
测试,可以发现权重生效了。