【Nginx】面試官:給我講講Nginx如何實現(xiàn)四層負載均衡?
寫在前面
這次又被問到Nginx四層負載均衡的問題了,別慌,我們一起來細細分析這個看似簡單的問題。
負載均衡可以分為靜態(tài)負載均衡和動態(tài)負載均衡,接下來,我們就一起來分析下Nginx如何實現(xiàn)四層靜態(tài)負載均衡和四層動態(tài)負載均衡。
靜態(tài)負載均衡
Nginx的四層靜態(tài)負載均衡需要啟用ngx_stream_core_module模塊,默認情況下,ngx_stream_core_module是沒有啟用的,需要在安裝Nginx時,添加--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
配置四層負載均衡
配置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 { #監(jiān)聽端口,默認使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成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; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_upload_rate 0; #從上游服務器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_download_rate 0; #上游服務器 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)聽端口,默認使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成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; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_upload_rate 0; #從上游服務器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_download_rate 0; #上游服務器 proxy_pass mysql_backend; } }
動態(tài)負載均衡
配置Nginx四層靜態(tài)負載均衡后,重啟Nginx時,Worker進程一直不退出,會報錯,如下所示。
nginx: worker process is shutting down;
這是因為Worker進程維持的長連接一直在使用,所以無法退出,只能殺掉進程??梢允褂肗ginx的四層動態(tài)負載均衡解決這個問題。
使用Nginx的四層動態(tài)負載均衡有兩種方案:使用商業(yè)版的Nginx和使用開源的nginx-stream-upsync-module模塊。注意:四層動態(tài)負載均衡可以使用nginx-stream-upsync-module模塊,七層動態(tài)負載均衡可以使用nginx-upsync-module模塊。
使用如下命令為Nginx添加nginx-stream-upsync-module模塊和nginx-upsync-module模塊,此時,Nginx會同時支持四層動態(tài)負載均衡和HTTP七層動態(tài)負載均衡。
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
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服務器出現(xiàn)問題,本地還有一個備份。
配置server
server { #監(jiān)聽端口,默認使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成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; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_upload_rate 0; #從上游服務器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為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查看當前動態(tài)負載均衡上游服務器列表。
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)聽端口,默認使用的是tcp協(xié)議,如果需要UDP協(xié)議,則配置成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; #限速配置 #從客戶端讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_upload_rate 0; #從上游服務器讀數(shù)據(jù)的速率,單位為每秒字節(jié)數(shù),默認為0,不限速 proxy_download_rate 0; #上游服務器 proxy_pass mysql_backend; } server { listen 13307; upstream_show; } }
免責聲明:本文內(nèi)容由21ic獲得授權后發(fā)布,版權歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!