整理的文檔比較早,kong版本多是0.10.3版本。詳情請看官網最新文檔git
使用kong代理後端請求
1.開放幾個接口以下:
本地請求1:http://aaa.wyc.com:8888/aaa
github
返回:server_name:aaa.wyc.com,port:8888後端
本地請求2:http://aaa.wyc.com:8889/aaa
api
返回:server_name:aaa.wyc.com,port:8889服務器
。。。負載均衡
1.向kong裏添加一條轉發,將uri爲/test的請求轉發到本地請求request1
規則以下:curl
curl -i -X POST \ --url http://127.0.0.1:8001/apis/ \ --data 'name=wyc' \ --data 'uris=/test' \ --data 'upstream_url=http://aaa.wyc.com:8888/aaa' \ --data 'preserve_host=true' \ --data 'strip_uri=true'
而後請求
http://localhost:8000/test
,返回server_name:aaa.wyc.com,port:8888
,說明添加成功。測試
2.在kong裏添加一個名爲test_v1的upstreamlua
curl -i -X POST \ --url http://127.0.0.1:8001/upstreams/ \ --data 'name=test_v1' \ --data 'slots=10'
生成:url
{ "orderlist": [ 1, 6, 7, 8, 2, 4, 10, 5, 9, 3 ], "slots": 10, "id": "6beedc5d-87fc-4382-92be-1dc752f28383", "name": "test_v1", "created_at": 1505217458404 }
3.在以上生成的upstream中添加target
curl -i -X POST \ --url http://127.0.0.1:8001/upstreams/test_v1/targets \ --data 'target=aaa.wyc.com:8888' \ --data 'weight=10'
生成:
{ "target": "aaa.wyc.com:8888", "id": "479dbc3a-74fe-47e7-b7b5-3ff188f5c08a", "weight": 10, "created_at": 1505269393596, "upstream_id": "6beedc5d-87fc-4382-92be-1dc752f28383" }
若是要刪除某個target,則將其weiget設置爲0
curl -i -X POST \ --url http://127.0.0.1:8001/upstreams/test_v1/targets \ --data 'target=aaa.wyc.com:8888' \ --data 'weight=0'
查看正常活躍的target:
curl -i -X GET \ --url http://127.0.0.1:8001/upstreams/test_v1/targets/active
1.修改request1的upstream爲test_v1
{ "http_if_terminated": true, "id": "d7685fd9-4caa-4f61-9b2c-8cc874ebc4d9", "retries": 5, "preserve_host": false, "created_at": 1496395744000, "upstream_connect_timeout": 60000, "upstream_url": "http://test_v1/aaa", "upstream_read_timeout": 60000, "upstream_send_timeout": 60000, "https_only": false, "strip_uri": true, "uris": [ "/test" ], "name": "wyc", "hosts": {} }
而後重複執行上面步驟:
請求http://localhost:8000/test
,返回server_name:aaa.wyc.com,port:8888
。代理請求成功。
2.在upstream(test_v1)中再添加兩條target
{ "data": [ { "weight": 10, "id": "66e30dcb-f504-408c-8269-fc86cbdc9cc6", "target": "aaa.wyc.com:8890", "created_at": 1505285074670, "upstream_id": "6beedc5d-87fc-4382-92be-1dc752f28383" }, { "weight": 10, "id": "e4c20a71-50d6-4b0e-989b-67f04758dbcb", "target": "aaa.wyc.com:8889", "created_at": 1505285069882, "upstream_id": "6beedc5d-87fc-4382-92be-1dc752f28383" }, { "weight": 10, "id": "cc79f82f-d9d8-4a8a-9dbd-9cb59f35f078", "target": "aaa.wyc.com:8888", "created_at": 1505273186303, "upstream_id": "6beedc5d-87fc-4382-92be-1dc752f28383" } ], "total": 3 }
三個target的權重相同,應該輪流返回如上接口返回的信息。可是結果貌似不對:
https://github.com/Mashape/kong/issues/2887
實際測試:請求kong老是代理請求到upstream的其中一個target,例如:aaa.wyc.com:8889.
若是將aaa.wyc.com:8889所在服務down掉,kong將自動選擇其餘的健康的節點,將請求代理過去。
1.https://github.com/openresty/lua-resty-upstream-healthcheck
官方提供的健康檢查,使用http方式定時調用,須要對應的上游服務器暴露一個api來驗證。
將上述的接口放在test_v1的upstream中:
upstream test_v1 { server 127.0.0.1:8889; server 127.0.0.1:8890; server 127.0.0.1:8891 backup; }
定義一個status的location查看upstream狀態:
Nginx Worker PID: 45014 Upstream test_v1 Primary Peers 127.0.0.1:8889 up 127.0.0.1:8890 up Backup Peers 127.0.0.1:8891 up
將其中一個upstream去除:server 127.0.0.1:8890;返回結果:
Nginx Worker PID: 45160 Upstream test_v1 Primary Peers 127.0.0.1:8889 up 127.0.0.1:8890 DOWN Backup Peers 127.0.0.1:8891 up
2.https://github.com/upyun/lua-resty-checkups
3.https://github.com/upyun/slardar