日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 單片機(jī) > 架構(gòu)師社區(qū)
[導(dǎo)讀]寫在前面 這次又被問到Nginx四層負(fù)載均衡的問題了,別慌,我們一起來細(xì)細(xì)分析這個(gè)看似簡單的問題。 負(fù)載均衡可以分為靜態(tài)負(fù)載均衡和動(dòng)態(tài)負(fù)載均衡,接下來,我們就一起來分析下Nginx如何實(shí)現(xiàn)四層靜態(tài)負(fù)載均衡和四層動(dòng)態(tài)負(fù)載均衡。 靜態(tài)負(fù)載均衡 Nginx的四層靜



寫在前面

這次又被問到Nginx四層負(fù)載均衡的問題了,別慌,我們一起來細(xì)細(xì)分析這個(gè)看似簡單的問題。

負(fù)載均衡可以分為靜態(tài)負(fù)載均衡和動(dòng)態(tài)負(fù)載均衡,接下來,我們就一起來分析下Nginx如何實(shí)現(xiàn)四層靜態(tài)負(fù)載均衡和四層動(dòng)態(tài)負(fù)載均衡。

靜態(tài)負(fù)載均衡

Nginx的四層靜態(tài)負(fù)載均衡需要啟用ngx_stream_core_module模塊,默認(rèn)情況下,ngx_stream_core_module是沒有啟用的,需要在安裝Nginx時(shí),添加--with-stream配置參數(shù)啟用,如下所示。

./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream  --with-http_ssl_module

配置四層負(fù)載均衡

配置HTTP負(fù)載均衡時(shí),都是配置在http指令下,配置四層負(fù)載均衡,則是在stream指令下,結(jié)構(gòu)如下所示.

stream {
 upstream mysql_backend {
  ......
 }
 server {
  ......
 }
}

配置upstream

upstream mysql_backend {
 server 192.168.175.201:3306 max_fails=2 fail_timeout=10s weight=1;
 server 192.168.175.202:3306 max_fails=2 fail_timeout=10s weight=1;
 least_conn;
}

配置server

server { #監(jiān)聽端口,默認(rèn)使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成listen 3307 udp; listen 3307; #失敗重試 proxy_next_upstream on;
 proxy_next_upstream_timeout 0;
 proxy_next_upstream_tries 0; #超時(shí)配置 #配置與上游服務(wù)器連接超時(shí)時(shí)間,默認(rèn)60s proxy_connect_timeout 1s; #配置與客戶端上游服務(wù)器連接的兩次成功讀/寫操作的超時(shí)時(shí)間,如果超時(shí),將自動(dòng)斷開連接 #即連接存活時(shí)間,通過它可以釋放不活躍的連接,默認(rèn)10分鐘 proxy_timeout 1m; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_upload_rate 0; #從上游服務(wù)器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_download_rate 0; #上游服務(wù)器 proxy_pass mysql_backend;
}

配置完之后,就可以連接Nginx的3307端口,訪問數(shù)據(jù)庫了。

Nginx完整配置

完整的Nginx配置如下:

user  hadoop hadoop;
worker_processes  auto;
 
error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info; #pid        logs/nginx.pid; events {
 use epoll;
    worker_connections  1024;
}
 
stream {
 upstream mysql_backend {
  server 192.168.175.100:3306 max_fails=2 fail_timeout=10s weight=1;
  least_conn;
 }
 server { #監(jiān)聽端口,默認(rèn)使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成listen 3307 udp; listen 3307; #失敗重試 proxy_next_upstream on;
  proxy_next_upstream_timeout 0;
  proxy_next_upstream_tries 0; #超時(shí)配置 #配置與上游服務(wù)器連接超時(shí)時(shí)間,默認(rèn)60s proxy_connect_timeout 1s; #配置與客戶端上游服務(wù)器連接的兩次成功讀/寫操作的超時(shí)時(shí)間,如果超時(shí),將自動(dòng)斷開連接 #即連接存活時(shí)間,通過它可以釋放不活躍的連接,默認(rèn)10分鐘 proxy_timeout 1m; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_upload_rate 0; #從上游服務(wù)器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_download_rate 0; #上游服務(wù)器 proxy_pass mysql_backend;
 }
}

動(dòng)態(tài)負(fù)載均衡

配置Nginx四層靜態(tài)負(fù)載均衡后,重啟Nginx時(shí),Worker進(jìn)程一直不退出,會(huì)報(bào)錯(cuò),如下所示。

nginx: worker process is shutting down;

這是因?yàn)閃orker進(jìn)程維持的長連接一直在使用,所以無法退出,只能殺掉進(jìn)程。可以使用Nginx的四層動(dòng)態(tài)負(fù)載均衡解決這個(gè)問題。

使用Nginx的四層動(dòng)態(tài)負(fù)載均衡有兩種方案:使用商業(yè)版的Nginx和使用開源的nginx-stream-upsync-module模塊。注意:四層動(dòng)態(tài)負(fù)載均衡可以使用nginx-stream-upsync-module模塊,七層動(dòng)態(tài)負(fù)載均衡可以使用nginx-upsync-module模塊。

使用如下命令為Nginx添加nginx-stream-upsync-module模塊和nginx-upsync-module模塊,此時(shí),Nginx會(huì)同時(shí)支持四層動(dòng)態(tài)負(fù)載均衡和HTTP七層動(dòng)態(tài)負(fù)載均衡。

git clone https://github.com/xiaokai-wang/nginx-stream-upsync-module.git
git clone https://github.com/weibocom/nginx-upsync-module.git
git clone https://github.com/CallMeFoxie/nginx-upsync.git
cp -r nginx-stream-upsync-module/* nginx-upsync/nginx-stream-upsync-module/
cp -r nginx-upsync-module/* nginx-upsync/nginx-upsync-module/
 
./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream --add-module=/usr/local/src/nginx-upsync --with-http_ssl_module

配置四層負(fù)載均衡

配置HTTP負(fù)載均衡時(shí),都是配置在http指令下,配置四層負(fù)載均衡,則是在stream指令下,結(jié)構(gòu)如下所示,

stream {
 upstream mysql_backend {
  ......
 }
 server {
  ......
 }
}

配置upstream

upstream mysql_backend {
 server 127.0.0.1:1111; #占位server upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
 upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf;
}
  • upsync指令指定從consul哪個(gè)路徑拉取上游服務(wù)器配置;
  • upsync_timeout配置從consul拉取上游服務(wù)器配置的超時(shí)時(shí)間;
  • upsync_interval配置從consul拉取上游服務(wù)器配置的間隔時(shí)間;
  • upsync_type指定使用consul配置服務(wù)器;
  • strong_dependency配置nginx在啟動(dòng)時(shí)是否強(qiáng)制依賴配置服務(wù)器,如果配置為on,則拉取配置失敗時(shí)Nginx啟動(dòng)同樣失敗。
  • upsync_dump_path指定從consul拉取的上游服務(wù)器后持久化到的位置,這樣即使consul服務(wù)器出現(xiàn)問題,本地還有一個(gè)備份。

配置server

server { #監(jiān)聽端口,默認(rèn)使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成listen 3307 udp; listen 3307; #失敗重試 proxy_next_upstream on;
 proxy_next_upstream_timeout 0;
 proxy_next_upstream_tries 0; #超時(shí)配置 #配置與上游服務(wù)器連接超時(shí)時(shí)間,默認(rèn)60s proxy_connect_timeout 1s; #配置與客戶端上游服務(wù)器連接的兩次成功讀/寫操作的超時(shí)時(shí)間,如果超時(shí),將自動(dòng)斷開連接 #即連接存活時(shí)間,通過它可以釋放不活躍的連接,默認(rèn)10分鐘 proxy_timeout 1m; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_upload_rate 0; #從上游服務(wù)器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_download_rate 0; #上游服務(wù)器 proxy_pass mysql_backend;
}

從Consul添加上游服務(wù)器

curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.201:3306
curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306

從Consul刪除上游服務(wù)器

curl -X DELETE http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306

配置upstream_show

server {
 listen 13307;
 upstream_show;
}

配置upstream_show指令后,可以通過curl http://192.168.175.100:13307/upstream_show查看當(dāng)前動(dòng)態(tài)負(fù)載均衡上游服務(wù)器列表。

Nginx完整配置

Nginx的完整配置如下:

user  hadoop hadoop;
worker_processes  auto;
 
error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info; #pid        logs/nginx.pid; events {
 use epoll;
    worker_connections  1024;
}
 
stream {
 upstream mysql_backend {
  server 127.0.0.1:1111; #占位server upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
  upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf;
 }
 server { #監(jiān)聽端口,默認(rèn)使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成listen 3307 udp; listen 3307; #失敗重試 proxy_next_upstream on;
  proxy_next_upstream_timeout 0;
  proxy_next_upstream_tries 0; #超時(shí)配置 #配置與上游服務(wù)器連接超時(shí)時(shí)間,默認(rèn)60s proxy_connect_timeout 1s; #配置與客戶端上游服務(wù)器連接的兩次成功讀/寫操作的超時(shí)時(shí)間,如果超時(shí),將自動(dòng)斷開連接 #即連接存活時(shí)間,通過它可以釋放不活躍的連接,默認(rèn)10分鐘 proxy_timeout 1m; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_upload_rate 0; #從上游服務(wù)器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認(rèn)為0,不限速 proxy_download_rate 0; #上游服務(wù)器 proxy_pass mysql_backend;
 }
 server {
  listen 13307;
  upstream_show;
 }
}


免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問題,請(qǐng)聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉