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}
檢視服務列表: