在上一章《Nginx系列教程(11) - HTTP动态负载均衡(一)》我们了解到了负载均衡按静态和动态分为三种:
- Consul+Consul-template 每次发现配置更改需要raload nginx,重启Nginx。
- Consul+OpenResty 实现无需raload动态负载均衡
- Consul+upsync+Nginx 实现无需raload动态负载均衡
并介绍了常用的服务注册于发现框架,并以Consul做了简单的安装与使用介绍。
动态负载均衡示意图如下:
本文我们主要来讲解三方应用nginx-upsync-module
来实现动态负载均衡。
注意:如果按照本文操作,必须把之前Nginx的环境清除,重新安装 ! Nginx版本必须1.9以上。
1.nginx-upsync-module简介
Upsync是新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul后端server的列表,并动态更新Nginx的路由信息。此模块不依赖于任何第三方模块。Consul作为Nginx的DB,利用Consul的KV服务,每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。
2.nginx-upsync-module安装
2.1 下载
1.下载Nginx (作用:实现反向代理、负载负载库)
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">wget</span> http://nginx.org/download/nginx-1.9.10.tar.gz
</code></span>
2.下载Consul (作用:对动态负载均衡均配置实现注册)
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">wget</span> https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip
</code></span>
3.下载nginx-upsync-module (作用:nginx动态获取最新upstream信息)
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">wget</span> https://github.com/weibocom/nginx-upsync-module/archive/master.zip
</code></span>
2.2 安装(按步骤执行)
2.2.1 步骤一:安装nginx-upsync-module:
下载后,压缩包的名字叫master.zip
<span style="color:#000000"><code class="language-java">unzip master<span style="color:#969896">.</span>zip
</code></span>
解压完成后会在当前目录生成一个文件夹:nginx-upsync-module-master
,下面安装Nginx的时候会用到这个目录。
首先解压:
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">tar</span> -zxvf nginx-1.9.10.tar.gz
</code></span>
配置Nginx:
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">groupadd</span> nginx
<span style="color:#795da3">useradd</span> -g nginx -s /sbin/nologin nginx
<span style="color:#795da3">mkdir</span> -p /var/tmp/nginx/client/
<span style="color:#795da3">mkdir</span> -p /usr/local/nginx
</code></span>
编译Nginx:(注意最后--add-module=../nginx-upsync-module-master
,配的路径便是nginx-upsync-module解压的目录路径)
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">cd</span> nginx-1.9.10
./configure --prefix<span style="color:#df5000">=</span>/usr/local/nginx --user<span style="color:#df5000">=</span>nginx --group<span style="color:#df5000">=</span>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<span style="color:#df5000">=</span>/var/tmp/nginx/client/ --http-proxy-temp-path<span style="color:#df5000">=</span>/var/tmp/nginx/proxy/ --http-fastcgi-temp-path<span style="color:#df5000">=</span>/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path<span style="color:#df5000">=</span>/var/tmp/nginx/uwsgi --http-scgi-temp-path<span style="color:#df5000">=</span>/var/tmp/nginx/scgi --with-pcre --add-module<span style="color:#df5000">=</span><span style="color:#969896">..</span>/nginx-upsync-module-master
<span style="color:#795da3">make</span> <span style="color:#df5000">&&</span> <span style="color:#795da3">make</span> <span style="color:#795da3">install</span>
</code></span>
编译如果报错:./configure: error: SSL modules require the OpenSSL library.
解决办法:
<span style="color:#000000"><code class="language-shell">yum -y <span style="color:#795da3">install</span> openssl openssl-devel
</code></span>
2.安装Consul:
<span style="color:#000000"><code class="language-shell">unzip master.zip
unzip consul_0.7.1_linux_amd64.zip
</code></span>
如果解压出现该错误:-bash: unzip
: 未找到命令,解决办法:
<span style="color:#000000"><code class="language-shell">yum -y <span style="color:#795da3">install</span> unzip
</code></span>
2.3 配置
<span style="color:#000000"><code class="language-py"><span style="color:#969896">##动态去consul 获取注册的真实反向代理地址</span>
upstream xxx<span style="color:#969896">{</span>
server <span style="color:#c76b29">127.0</span><span style="color:#c76b29">.0</span><span style="color:#c76b29">.1</span><span style="color:#969896">:</span><span style="color:#c76b29">11111</span><span style="color:#969896">;</span>
upsync <span style="color:#c76b29">192.168</span><span style="color:#c76b29">.162</span><span style="color:#c76b29">.130</span><span style="color:#969896">:</span><span style="color:#c76b29">8500</span><span style="color:#df5000">/</span>v1<span style="color:#df5000">/</span>kv<span style="color:#df5000">/</span>upstreams<span style="color:#df5000">/</span>xxx upsync_timeout<span style="color:#df5000">=</span>6m upsync_interval<span style="color:#df5000">=</span>500ms upsync_type<span style="color:#df5000">=</span>consul strong_dependency<span style="color:#df5000">=</span>off<span style="color:#969896">;</span>
upsync_dump_path <span style="color:#df5000">/</span>usr<span style="color:#df5000">/</span>local<span style="color:#df5000">/</span>nginx<span style="color:#df5000">/</span>conf<span style="color:#df5000">/</span>servers<span style="color:#df5000">/</span>servers_test<span style="color:#969896">.</span>conf<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
server <span style="color:#969896">{</span>
listen <span style="color:#c76b29">80</span><span style="color:#969896">;</span>
server_name localhost<span style="color:#969896">;</span>
location <span style="color:#df5000">/</span> <span style="color:#969896">{</span>
proxy_pass http<span style="color:#969896">:</span><span style="color:#df5000">//</span>xxx<span style="color:#969896">;</span>
index index<span style="color:#969896">.</span>html index<span style="color:#969896">.</span>htm<span style="color:#969896">;</span>
<span style="color:#969896">}</span>
<span style="color:#969896">}</span>
</code></span>
- upsync指令指定从consul哪个路径拉取上游服务器配置;
- upsync_timeout配置从consul拉取上游服务器配置的超时时间;
- upsync_interval配置从consul拉取上游服务器配置的间隔时间;
- upsync_type指定使用consul配置服务器;
- strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时nginx启动同样失败。
- upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。
注意:替换 consul 注册中心地址
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">mkdir</span> /usr/local/nginx/conf/servers/
</code></span>
upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。
2.4 启动
首先关闭防火墙,我的linux Ip地址192.168.162.130:
<span style="color:#000000"><code class="language-shell">./consul agent -dev -ui -node<span style="color:#df5000">=</span>consul-dev -client<span style="color:#df5000">=</span>192.168.162.130
</code></span>
<span style="color:#000000"><code class="language-shell">./nginx
</code></span>
运行成功!
2.5 测试
两台tomcat服务器,端口分别是8081和8082,下面通过HTTP PUT的方式注册到Console Server。Nginx服务会检测到Upstream有改变,便会自动写入上游服务器列表。下面是注册的两种方式:
1. 使用linux命令方式发送put请求:
<span style="color:#000000"><code class="language-shell"><span style="color:#795da3">curl</span> -X PUT http://192.168.162.130:8500/v1/kv/upstreams/xxx/192.168.162.1:8081
<span style="color:#795da3">curl</span> -X PUThttp://192.168.162.130:8500/v1/kv/upstreams/xxx/192.168.162.1:8082
</code></span>
或使用postmen 发送put请求:
<span style="color:#000000"><code class="language-shell">http://192.168.162.130:8500/v1/kv/upstreams/xxx/192.168.162.1:8081
http://192.168.162.130:8500/v1/kv/upstreams/xxx/192.168.162.1:8082
</code></span>
postman请求如下:
两个服务已经注册上了
2.启动两台tomcat (8081和8082),如果不知道怎么启动多个tomcat,可以参考文章:《Linux配置多个Tomcat同时运行以及tomcat 的端口介绍》
<span style="color:#000000"><code class="language-shell">/usr/local/tomcat8081/bin/startup.sh
/usr/local/tomcat8082/bin/startup.sh
</code></span>
3.浏览器输入http://192.168.162.130/
刷新:
会发下8081和8082 tomat会轮询访问。
4.如果想让8081的权重大一些,比如要8081访问3次,8082才访问1次,如何设置呢?
答:可以在Consul控制台设置,先贴出标准格式:
<span style="color:#000000"><code class="language-java"><span style="color:#969896">{</span><span style="color:#df5000">"weight"</span><span style="color:#df5000">:</span><span style="color:#c76b29">3</span><span style="color:#969896">,</span> <span style="color:#df5000">"max_fails"</span><span style="color:#df5000">:</span><span style="color:#c76b29">2</span><span style="color:#969896">,</span> <span style="color:#df5000">"fail_timeout"</span><span style="color:#df5000">:</span><span style="color:#c76b29">10</span><span style="color:#969896">,</span> <span style="color:#df5000">"down"</span><span style="color:#df5000">:</span><span style="color:#c76b29">0</span><span style="color:#969896">}</span>
</code></span>
设置8081权重为3,点击UPDATE:
设置8082权重为1,点击UPDATE:
连续刷新页面,我们会发下tomcat8081出现3次,tomcat8082出现1次,轮流显现。
总结