天天看點

【Nginx】面試官:給我講講Nginx如何實作四層負載均衡?

靜态負載均衡

Nginx的四層靜态負載均衡需要啟用ngx_stream_core_module子產品,預設情況下,ngx_stream_core_module是沒有啟用的,需要在安裝Nginx時,添加--with-stream配置參數啟用,如下所示。

./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      

配置四層負載均衡

配置HTTP負載均衡時,都是配置在http指令下,配置四層負載均衡,則是在stream指令下,結構如下所示.

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 {
 #監聽端口,預設使用的是tcp協定,如果需要UDP協定,則配置成listen 3307 udp;
 listen 3307;
 #失敗重試
 proxy_next_upstream on;
 proxy_next_upstream_timeout 0;
 proxy_next_upstream_tries 0;
 #逾時配置
 #配置與上遊伺服器連接配接逾時時間,預設60s
 proxy_connect_timeout 1s;
 #配置與用戶端上遊伺服器連接配接的兩次成功讀/寫操作的逾時時間,如果逾時,将自動斷開連接配接
 #即連接配接存活時間,通過它可以釋放不活躍的連接配接,預設10分鐘
 proxy_timeout 1m;
 #限速配置
 #從用戶端讀資料的速率,機關為每秒位元組數,預設為0,不限速
 proxy_upload_rate 0;
 #從上遊伺服器讀資料的速率,機關為每秒位元組數,預設為0,不限速
 proxy_download_rate 0;
 #上遊伺服器
 proxy_pass mysql_backend;
}      

配置完之後,就可以連接配接Nginx的3307端口,通路資料庫了。

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 {
  #監聽端口,預設使用的是tcp協定,如果需要UDP協定,則配置成listen 3307 udp;
  listen 3307;
  #失敗重試
  proxy_next_upstream on;
  proxy_next_upstream_timeout 0;
  proxy_next_upstream_tries 0;
  #逾時配置
  #配置與上遊伺服器連接配接逾時時間,預設60s
  proxy_connect_timeout 1s;
  #配置與用戶端上遊伺服器連接配接的兩次成功讀/寫操作的逾時時間,如果逾時,将自動斷開連接配接
  #即連接配接存活時間,通過它可以釋放不活躍的連接配接,預設10分鐘
  proxy_timeout 1m;
  #限速配置
  #從用戶端讀資料的速率,機關為每秒位元組數,預設為0,不限速
  proxy_upload_rate 0;
  #從上遊伺服器讀資料的速率,機關為每秒位元組數,預設為0,不限速
  proxy_download_rate 0;
  #上遊伺服器
  proxy_pass mysql_backend;
 }
}      

動态負載均衡

配置Nginx四層靜态負載均衡後,重新開機Nginx時,Worker程序一直不退出,會報錯,如下所示。

nginx: worker process is shutting down;      

這是因為Worker程序維持的長連接配接一直在使用,是以無法退出,隻能殺掉程序。可以使用Nginx的四層動态負載均衡解決這個問題。

使用Nginx的四層動态負載均衡有兩種方案:使用商業版的Nginx和使用開源的nginx-stream-upsync-module子產品。注意:四層動态負載均衡可以使用nginx-stream-upsync-module子產品,七層動态負載均衡可以使用nginx-upsync-module子產品。

使用如下指令為Nginx添加nginx-stream-upsync-module子產品和nginx-upsync-module子產品,此時,Nginx會同時支援四層動态負載均衡和HTTP七層動态負載均衡。

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      

配置HTTP負載均衡時,都是配置在http指令下,配置四層負載均衡,則是在stream指令下,結構如下所示,

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

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哪個路徑拉取上遊伺服器配置;
  • upsync_timeout配置從consul拉取上遊伺服器配置的逾時時間;
  • upsync_interval配置從consul拉取上遊伺服器配置的間隔時間;
  • upsync_type指定使用consul配置伺服器;
  • strong_dependency配置nginx在啟動時是否強制依賴配置伺服器,如果配置為on,則拉取配置失敗時Nginx啟動同樣失敗。
  • upsync_dump_path指定從consul拉取的上遊伺服器後持久化到的位置,這樣即使consul伺服器出現問題,本地還有一個備份。

server {
 #監聽端口,預設使用的是tcp協定,如果需要UDP協定,則配置成listen 3307 udp;
 listen 3307;
 #失敗重試
 proxy_next_upstream on;
 proxy_next_upstream_timeout 0;
 proxy_next_upstream_tries 0;
 #逾時配置
 #配置與上遊伺服器連接配接逾時時間,預設60s
 proxy_connect_timeout 1s;
 #配置與用戶端上遊伺服器連接配接的兩次成功讀/寫操作的逾時時間,如果逾時,将自動斷開連接配接
 #即連接配接存活時間,通過它可以釋放不活躍的連接配接,預設10分鐘
 proxy_timeout 1m;
 #限速配置
 #從用戶端讀資料的速率,機關為每秒位元組數,預設為0,不限速
 proxy_upload_rate 0;
 #從上遊伺服器讀資料的速率,機關為每秒位元組數,預設為0,不限速
 proxy_download_rate 0;
 #上遊伺服器
 proxy_pass mysql_backend;
}      

從Consul添加上遊伺服器

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删除上遊伺服器

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

檢視目前動态負載均衡上遊伺服器清單。

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 {
  #監聽端口,預設使用的是tcp協定,如果需要UDP協定,則配置成listen 3307 udp;
  listen 3307;
  #失敗重試
  proxy_next_upstream on;
  proxy_next_upstream_timeout 0;
  proxy_next_upstream_tries 0;
  #逾時配置
  #配置與上遊伺服器連接配接逾時時間,預設60s
  proxy_connect_timeout 1s;
  #配置與用戶端上遊伺服器連接配接的兩次成功讀/寫操作的逾時時間,如果逾時,将自動斷開連接配接
  #即連接配接存活時間,通過它可以釋放不活躍的連接配接,預設10分鐘
  proxy_timeout 1m;
  #限速配置
  #從用戶端讀資料的速率,機關為每秒位元組數,預設為0,不限速
  proxy_upload_rate 0;
  #從上遊伺服器讀資料的速率,機關為每秒位元組數,預設為0,不限速
  proxy_download_rate 0;
  #上遊伺服器
  proxy_pass mysql_backend;
 }
 server {
  listen 13307;
  upstream_show;
 }
}