nginx-upsync-module使用

nginx-upsync-module使用

編譯模塊到nginx

安裝nginx依賴以及其他工具

apt-get install git
apt-get build-dep nginx

下載nginx-upsync-module源碼

git clone https://github.com/weibocom/nginx-upsync-module.git

下載nginx源碼


wget 'http://nginx.org/download/nginx-1.8.0.tar.gz'
tar -xzvf nginx-1.8.0.tar.gz
cd nginx-1.8.0/

開始編譯


./configure --add-module=/path/to/nginx-upsync-module
make
make install

啓動服務

測試例子中consul與nginx在同一服務器

啓動consul


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;
    }

    server {
        listen 8080;

        location = / {
            proxy_pass http://test;
        }

        location = /upstream_show {
            upstream_show;
        }

    }
}

測試

fedora 虛擬機中[172.18.18.10]使用docker創建nginx服務

docker run --name=nginx1 -p 1234:80 -d nginx
docker run --name=nginx2 -p 1235:80 -d nginx
docker run --name=nginx3 -p 1236:80 -d nginx
docker run --name=nginx4 -p 1237:80 -d nginx
docker run --name=nginx5 -p 1238:80 -d nginx
docker run --name=nginx6 -p 1239:80 -d nginx

添加服務

curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1234
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1235
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1236
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1237
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1238
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://127.0.0.1:8500/v1/kv/upstreams/test/172.18.18.10:1239

並修改每個nginx的默認顯示頁面,以方便展示負載輪訓情況

docker exec -it nginx1 sh
echo "nginx1">>/usr/share/nginx/html/index.html
docker exec -it nginx2 sh
echo "nginx2">>/usr/share/nginx/html/index.html
docker exec -it nginx3 sh
echo "nginx3">>/usr/share/nginx/html/index.html
docker exec -it nginx4 sh
echo "nginx4">>/usr/share/nginx/html/index.html
docker exec -it nginx5 sh
echo "nginx5">>/usr/share/nginx/html/index.html

回到nginx負載服務器。將服務添加到consul之後查/usr/local/nginx/conf/servers/servers_test.conf內容,或者通過瀏覽器查看服務情況

cat /usr/local/nginx/conf/servers/servers_test.conf
server 172.18.18.10:1235 weight=1 max_fails=2 fail_timeout=10s;
server 172.18.18.10:1236 weight=1 max_fails=2 fail_timeout=10s;
server 172.18.18.10:1234 weight=1 max_fails=2 fail_timeout=10s;
server 172.18.18.10:1237 weight=1 max_fails=2 fail_timeout=10s;
server 172.18.18.10:1238 weight=1 max_fails=2 fail_timeout=10s;
server 172.18.18.10:1239 weight=1 max_fails=2 fail_timeout=10s;

或者瀏覽器打開http://172.18.21.2:8080/upstream_show?test
顯示內容如下:

Upstream name: test; Backend server count: 5
        server 172.18.18.10:1235 weight=1 max_fails=2 fail_timeout=10s;
        server 172.18.18.10:1236 weight=1 max_fails=2 fail_timeout=10s;
        server 172.18.18.10:1234 weight=1 max_fails=2 fail_timeout=10s;
        server 172.18.18.10:1237 weight=1 max_fails=2 fail_timeout=10s;
        server 172.18.18.10:1238 weight=1 max_fails=2 fail_timeout=10s;

總結

此模塊只修改upstream 中的緩存信息,不能修改或添加其他配置

測試中遇到的問題

在添加服務時出現如下錯誤,導致服務添加不能實時進行,大約需要3分鐘左右時間。

consul日誌:

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

問題現象

當添加一個服務時,出現此問題,新增服務不能及時添加到負載中,不影響運行正常的服務。
此時再往consul繼續添加一個服務時,可能會導致此錯誤終止,並能成功添加當前兩條服務記錄。

發佈了112 篇原創文章 · 獲贊 11 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章