Nginxシリーズ:upsyncモジュールを使用して負荷分散を実現

ここに画像の説明を挿入

1.nginxリロードの問題

問題の説明

Nginxのリロードには一定の損失があります。長い接続を使用している場合、nginxをリロードすると、長時間接続されているすべてのワーカープロセスが正常に終了し、ワーカープロセスのすべての接続が解放されると、プロセスは実際に終了します。 。

解決

現在、nginxのコミュニティバージョンには3つのオプションがあります。

TengineのDyupsモジュール。
Upsync + Consul of Weiboは、動的な負荷分散を実現します。
OpenRestyのbalancer_by_lua(Paiyunはオープンソースのslardar(Consul balancer_by_lua)を使用しています)。
この記事では、upsyncモジュールを使用して、構成ファイルが変更された後のリロードnginxプロセスによって引き起こされるパフォーマンス低下の問題を解決します。

その機能は、領事のバックエンドサーバーのリストを取得し、Nginxルーティング情報を更新することです。このモジュールは、サードパーティのモジュールに依存しません。Nginxのデータベースとして、consulはconsulのKVサービスを利用し、各Nginxワークプロセスは独立して各アップストリームの構成をプルし、独自のルートを更新します。

2、実際の戦闘

2.1パッチnginx

この手順を実行する必要はありません。実行しない場合は、コンパイル時にこのモジュールを削除してください

git clone https://github.com/xiaokai-wang/nginx_upstream_check_module
## 打补丁包
patch -p0 < /usr/local/src/nginx_upstream_check_module-master/check_1.9.2+.patch

nginx-upsync-moduleソースコードをダウンロードする

git clone https://github.com/weibocom/nginx-upsync-module.git
下载nginx源码
wget 'http://nginx.org/download/nginx-1.10.1.tar.gz'
tar -xzvf nginx-1.10.1.tar.gz
cd nginx-1.10.1/
开始编译
./configure --prefix=/data/app/nginx-1.10.1 --user=nginx --group=nginx  --with-http_ssl_module  --with-http_stub_status_module   --add-module=/usr/local/src/nginx-upsync-module-master/ --add-module=/usr/local/src/nginx_upstream_check_module-master/
make
make install

三、領事を始める

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
unzip consul_0.6.4_linux_amd64.zip
./consul agent -advertise=x.x.x.x -client=0.0.0.0 -dev

nginx構成ファイルを作成します

mkdir -p /usr/local/nginx/conf/servers
/usr/local/nginx/conf/nginx.conf
events {
    
    
 worker_connections  4096;  ## Default: 1024
}
 
http {
    
    
 upstream test {
    
    
 # fake server otherwise ngx_http_upstream will report error when startup
 server 127.0.0.1:11111;
 
 # all backend server will pull from consul when startup and will delete fake server
 upsync 127.0.0.1:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
 upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
 }
 
 upstream bar {
    
    
 server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
 }
 
 server {
    
    
 listen 8080;
 
 location = /proxy_test {
    
    
 proxy_pass http://test;
 }
 
 location = /bar {
    
    
 proxy_pass http://bar;
 }
 
 location = /upstream_show {
    
    
 upstream_show;
 }
 
 }
}

4、テスト

for i in `seq 3`;do mkdir html$i/test -p && echo $i >html$i/test/test.html; done; 
 
docker run -d -p 8001:80 -v /root/html1/:/usr/share/nginx/html nginx
docker run -d -p 8002:80 -v /root/html2/:/usr/share/nginx/html nginx
docker run -d -p 8003:80 -v /root/html3/:/usr/share/nginx/html nginx

5、サービスを追加

curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/192.168.56.12:8001
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/192.168.56.12:8002
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/192.168.56.12:8003

conf / servers /servers_test.confファイルにコンテンツがあるかどうかを確認します

cat conf/servers/servers_test.conf
server 192.168.56.12:8003 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.56.12:8002 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.56.12:8001 weight=1 max_fails=2 fail_timeout=10s;

または、ブラウザhttp://192.168.56.11:8080/upstream_show?testを開きます。

表示内容は以下のとおりです。

Upstream name: test; Backend server count: 3
 server 192.168.56.12:8003 weight=1 max_fails=2 fail_timeout=10s;
 server 192.168.56.12:8002 weight=1 max_fails=2 fail_timeout=10s;
 server 192.168.56.12:8001 weight=1 max_fails=2 fail_timeout=10s;

総括する

このモジュールは、アップストリームのキャッシュ情報のみを変更し、他の構成を変更または追加することはできません。

テストで発生した問題

サービスを追加する際に以下のエラーが発生し、サービスの追加がリアルタイムで行われず、約3分かかりました。

領事ログ:

2016/03/22 05:34:42 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (149.023µs) from=127.0.0.1:38853
 2016/03/22 05:34:43 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (146.759µs) from=127.0.0.1:38854
 2016/03/22 05:34:45 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (149.853µs) from=127.0.0.1:38855
 2016/03/22 05:34:46 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (111.46µs) from=127.0.0.1:38856
 2016/03/22 05:34:48 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (142.696µs) from=127.0.0.1:38857
 2016/03/22 05:34:48 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (112.089µs) from=127.0.0.1:38858
 2016/03/22 05:34:49 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (114.29µs) from=127.0.0.1:38859
 2016/03/22 05:34:50 [DEBUG] http: Request GET /v1/kv/upstreams/test?recurse&index=169 (148.245µs) from=127.0.0.1:38860

nginxログ

2016/03/22 05:35:09 [error] 18879#0: recv() failed (104: Connection reset by peer)
2016/03/22 05:35:09 [error] 18879#0: upsync_recv: recv error with upsync_server: 127.0.0.1:8500
2016/03/22 05:35:10 [error] 18879#0: recv() failed (104: Connection reset by peer)
2016/03/22 05:35:10 [error] 18879#0: upsync_recv: recv error with upsync_server: 127.0.0.1:8500
2016/03/22 05:35:11 [error] 18879#0: recv() failed (104: Connection reset by peer)
2016/03/22 05:35:11 [error] 18879#0: upsync_recv: recv error with upsync_server: 127.0.0.1:8500
2016/03/22 05:35:13 [error] 18879#0: recv() failed (104: Connection reset by peer)
2016/03/22 05:35:13 [error] 18879#0: upsync_recv: recv error with upsync_server: 127.0.0.1:8500
2016/03/22 05:35:13 [error] 18879#0: recv() failed (104: Connection reset by peer)
2016/03/22 05:35:13 [error] 18879#0: upsync_recv: recv error with upsync_server: 127.0.0.1:8500
2016/03/22 05:35:14 [error] 18879#0: recv() failed (104: Connection reset by peer)

問題現象

サービスを追加するときにこの問題が発生し、新しいサービスを時間内にロードに追加できず、通常のサービスには影響しません。このとき、引き続き領事館にサービスを追加すると、このエラーが終了し、現在の2つのサービスレコードを正常に追加できる場合があります。

6、ヘルプドキュメント

公式のgithubアドレス
nginx_upstream_check_modulehttps://www.cnblogs.com/Carpe ...

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/liuxingjiaoyu/article/details/112763969