KONG基礎使用


KONG是一個基於Nginx的API Gateway。提供了諸如身份認證,權限控制,流量控制,日志等一系列API相關的組件,可謂相當方便。
KONG項目首頁
KONG入門
KONG的Github地址
KONG Admin API一覽
KONG插件列表

安裝

以Linux RedHat 6.2為例:
從此處下載對應二進制rpm包,按操作安裝即可
KONG RedHat Release
注意如果yum出現無法獲取metalink的錯誤,
sudo vi /etc/yum.repos.d/epel.repo
注釋掉所有mirrorlist,取消所有baseurl的注釋即可。

簡介

KONG是一個API Gateway,顧名思義,就是API的出口。
內部系統的API可以隨意編寫,但如果要對外服務,就一定需要各種權限控制。

測試環境

首先,我們寫一個Mock API,進行測試之用。

  1. import tornado.ioloop
  2. import tornado.web
  3. class MainHandler(tornado.web.RequestHandler):
  4. call_cnt = 0
  5. def get(self):
  6. MainHandler.call_cnt+= 1
  7. self.write("GET / %s"%MainHandler.call_cnt)
  8. def post(self):
  9. MainHandler.call_cnt+= 1
  10. self.write("POST / %s"%MainHandler.call_cnt)
  11. class TestHandler(tornado.web.RequestHandler):
  12. call_cnt = 0
  13. def get(self):
  14. TestHandler.call_cnt += 1
  15. self.write("GET /test %s"%TestHandler.call_cnt)
  16. def post(self):
  17. TestHandler.call_cnt += 1
  18. self.write("POST /test %s"%TestHandler.call_cnt)
  19. def make_app():
  20. return tornado.web.Application([
  21. (r"/", MainHandler),
  22. (r"/test", TestHandler)
  23. ])
  24. if __name__ == "__main__":
  25. app = make_app()
  26. app.listen(8812)
  27. tornado.ioloop.IOLoop.current().start()

這是一個用Tornado寫的簡易REST測試 API,用以下命令后台啟動
nohup python test_api.py 1>/dev/null 2>log &;

  1. # 測試Endpoint:/
  2. curl -i -X GET http://localhost:8812/
  3. curl -i -X POST http://localhost:8812/
  4. curl -i -X DELETE http://localhost:8812/
  5. # 測試Endpoint:/test
  6. curl -i -X GET http://localhost:8812/test
  7. curl -i -X POST http://localhost:8812/test
  8. curl -i -X DELETE http://localhost:8812/test

現在我們已經有一個正常工作的REST API了。是時候使用KONG了。

KONG的使用基本和NGINX如出一轍。默認的工作目錄是/usr/local/kong,默認的配置文件是/etc/kong/kong.yml。如果需要工作在1024以下的端口,則要求root權限。
KONG的啟動命令很簡單,就是kong start
但是啟動之前需要編輯配置文件。
需要關注的主要是幾個點:
發送匿名統計報告,(默認打開,一定要關閉)
后端數據庫的配置,(默認Cassandra,建議Postgres)
內存緩存大小配置,(默認128M,建議1G)
出口端口(默認HTTP 8000,HTTPS 8443,建議直接改成80和443)
管理端口(默認8001,我改成8888比較吉利)
配置好之后,啟動KONG即可。

配置

KONG的配置完全通過REST API完成,Admin端口在配置文件中可以配置,下面全部使用http://localhost:8888/作為Admin EndPoint
使用什么語言的HTTP庫都可以完成這個工作,這里直接使用Linux自帶的curl。

注冊API

第一步,我們需要先注冊我們的API
假設我們想把上面那個API掛載到/test路徑下,
即用戶訪問http://localhost/test/ 時,會返回GET /
用戶訪問http://localhost/test/test 時,會返回GET /test

執行

  1. # 查看當前已經注冊的API
  2. curl -X GET http://localhost:8888/apis/
  3. # 注冊一個新的API
  4. curl -i -X POST http://localhost:8888/apis/ -d "name=testapi" -d "request_path=/test" -d "upstream_url=http://localhost:8812/" -d "strip_request_path=true"

這里注冊URL的幾個參數意思是,這個API注冊的名字叫testapi。它被掛載在網關的/test路徑下,上游轉發到http://localhost:8812去處理,轉發的時候把前面的/test前綴給去掉。

  1. {"upstream_url":"http:\/\/localhost:8812\/","request_path":"\/test","id":"a302c28c-eb8a-4e53-bac2-9acb68695f3b","created_at":1468379310000,"preserve_host":false,"strip_request_path":true,"name":"testapi"}

返回結果表明API創建的結果。一般會返回一個API的ID。
這就表明API注冊完成,我們可以測試一下:

  1. curl -i -X GET http://localhost/test
  2. curl -i -X POST https://localhost/test/test --insecure

如同我們預期的一樣返回正確的結果,說明API已經成功注冊。

當然,僅僅把API注冊了開放出去,其實也就是把多個API集成到一個EndPoint,實際意義並不大,下面我們來試一試高級一點的功能。

注冊插件

KONG自帶插件目前可以分為以下幾類:
身份認證,安全,流量控制,分析監控,格式轉換,日志。
KONG插件列表
有的API完全開放,不需要任何認證,有的API會涉及敏感數據,權限控制需要非常嚴格。有的API完全不在乎調用頻次或者日志,有的則反過來。
值得高興的是,KONG的插件獨立作用於每一個API,不同的API可以使用完全不同的插件。提供了相當靈活的配置策略。

現在我們首先給這個測試API加上基本的權限驗證。

  1. curl -X POST http://localhost:8888/apis/testapi/plugins -d "name=key-auth"
  2. # 這個時候再去調用這個API就會返回401 Unauthorized
  3. curl -i -X GET http://localhost/test

添加用戶

朴素的API可能壓根沒有嚴格的用戶概念,端口大開,隨便哪個阿貓阿狗都能進來掃一掃看一看。這可不行。
KONG有一個consumer的概念,consumer是全局共用的。
比如某個API啟用了key-auth,那么沒有身份的訪問者就無法調用這個API了。
需要首先創建一個Consumer,然后在key-auth插件中為這個consumer生成一個key。
然后就可以使用這個key來透過權限驗證訪問API了。

同理,如果另外一個API也開通了key-auth插件,那么這個consumer也是可以通過key-auth驗證訪問這個API的,如果要控制這種情況,就需要ACL插件。
(認證與權限乃是兩個不同的事物)

首先我們創建一個名為test_user的consumer

  1. # 獲取所有Consumer列表
  2. curl -X GET http://localhost:8888/consumers/
  3. # 創建一個新的Consumer
  4. curl -X POST http://localhost:8888/consumers/ -d "username=testuser"

然后,我們在key-auth插件中為它創建一個key

  1. curl -X POST http://localhost:8888/consumers/testuser/key-auth -d "key=testkey"

這次加上apikey首部進行驗證,成功調用

  1. curl -i -X GET http://localhost/test --header "apikey: testkey"

刪除用戶

  1. curl -i -X DELETE http://localhost:8888/consumers/testuser

刪除插件

  1. # 查詢API擁有的插件ID
  2. curl -i -X GET http://localhost:8888/apis/testapi/plugins/
  3. # 根據插件ID刪除插件。
  4. curl -i -X DELETE http://localhost:8888/apis/testapi/plugins/e49a2b2e-4c36-4c6a-bd1a-b5b065e63bb8

以上是基本的API,Consumer,Plugin的基本介紹。

ACL插件

  1. # 注冊overseas_index API
  2. curl -X POST http://localhost:8888/apis/ -d "name=overseas_index" -d "upstream_url=http://10.182.20.128:8848/" -d "request_path=/stat/overseas" -d "strip_request_path=true"
  3. # 為海外版API注冊key-auth插件
  4. curl -X POST http://localhost:8888/apis/overseas_index/plugins --data "name=key-auth"
  5. # 創建overseas_user 用戶
  6. curl -X POST http://localhost:8888/consumers/ -d "username=overseas_test"
  7. # 為overseas_test創建權限驗證
  8. curl -X POST http://localhost:8888/consumers/overseas_test/key-auth -d ""
  9. # 嘗試查詢海外版指標接口
  10. curl -X POST https://10.182.20.127/stat/overseas/realtime -d "app_id=53ace32656240b11c2071b1a" -d "date=2015-07-22" -H "apikey: 5e3b2a7a735744b39aeea9ebc2de1f01" --insecure
  11. # 給測試API
  12. curl -X POST https://10.182.20.128/test -d "app_id=53ace32656240b11c2071b1a" -d "date=2015-07-22" -H "apikey: 5e3b2a7a735744b39aeea9ebc2de1f01" --insecure

設置ACL插件

    1. # 給另外一個testapi加上權限驗證插件,現在任何通過認證的用戶都可以調用此API
    2. # 這不是我們所希望的,所以需要加入ACL控制
    3. curl -X POST http://localhost:8888/apis/testapi/plugins --data "name=key-auth"
    4. # 為用戶overseas_test創建ACL群組:overseas_user
    5. curl -X POST http://localhost:8888/consumers/overseas_test/acls \
    6. --data "group=overseas_user"
    7. # 為用戶overseas_test創建ACL群組:overseas_user
    8. curl -X POST http://localhost:8888/consumers/overseas_test/acls \
    9. --data "group=overseas_user"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM