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,進行測試之用。
import tornado.ioloopimport tornado.webclass MainHandler(tornado.web.RequestHandler):call_cnt = 0def get(self):MainHandler.call_cnt+= 1self.write("GET / %s"%MainHandler.call_cnt)def post(self):MainHandler.call_cnt+= 1self.write("POST / %s"%MainHandler.call_cnt)class TestHandler(tornado.web.RequestHandler):call_cnt = 0def get(self):TestHandler.call_cnt += 1self.write("GET /test %s"%TestHandler.call_cnt)def post(self):TestHandler.call_cnt += 1self.write("POST /test %s"%TestHandler.call_cnt)def make_app():return tornado.web.Application([(r"/", MainHandler),(r"/test", TestHandler)])if __name__ == "__main__":app = make_app()app.listen(8812)tornado.ioloop.IOLoop.current().start()
這是一個用Tornado寫的簡易REST測試 API,用以下命令后台啟動nohup python test_api.py 1>/dev/null 2>log &;
# 測試Endpoint:/curl -i -X GET http://localhost:8812/curl -i -X POST http://localhost:8812/curl -i -X DELETE http://localhost:8812/# 測試Endpoint:/testcurl -i -X GET http://localhost:8812/testcurl -i -X POST http://localhost:8812/testcurl -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
執行
# 查看當前已經注冊的APIcurl -X GET http://localhost:8888/apis/# 注冊一個新的APIcurl -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前綴給去掉。
{"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注冊完成,我們可以測試一下:
curl -i -X GET http://localhost/testcurl -i -X POST https://localhost/test/test --insecure
如同我們預期的一樣返回正確的結果,說明API已經成功注冊。
當然,僅僅把API注冊了開放出去,其實也就是把多個API集成到一個EndPoint,實際意義並不大,下面我們來試一試高級一點的功能。
注冊插件
KONG自帶插件目前可以分為以下幾類:
身份認證,安全,流量控制,分析監控,格式轉換,日志。
KONG插件列表
有的API完全開放,不需要任何認證,有的API會涉及敏感數據,權限控制需要非常嚴格。有的API完全不在乎調用頻次或者日志,有的則反過來。
值得高興的是,KONG的插件獨立作用於每一個API,不同的API可以使用完全不同的插件。提供了相當靈活的配置策略。
現在我們首先給這個測試API加上基本的權限驗證。
curl -X POST http://localhost:8888/apis/testapi/plugins -d "name=key-auth"# 這個時候再去調用這個API就會返回401 Unauthorizedcurl -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
# 獲取所有Consumer列表curl -X GET http://localhost:8888/consumers/# 創建一個新的Consumercurl -X POST http://localhost:8888/consumers/ -d "username=testuser"
然后,我們在key-auth插件中為它創建一個key
curl -X POST http://localhost:8888/consumers/testuser/key-auth -d "key=testkey"
這次加上apikey首部進行驗證,成功調用
curl -i -X GET http://localhost/test --header "apikey: testkey"
刪除用戶
curl -i -X DELETE http://localhost:8888/consumers/testuser
刪除插件
# 查詢API擁有的插件IDcurl -i -X GET http://localhost:8888/apis/testapi/plugins/# 根據插件ID刪除插件。curl -i -X DELETE http://localhost:8888/apis/testapi/plugins/e49a2b2e-4c36-4c6a-bd1a-b5b065e63bb8
以上是基本的API,Consumer,Plugin的基本介紹。
ACL插件
# 注冊overseas_index APIcurl -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"# 為海外版API注冊key-auth插件curl -X POST http://localhost:8888/apis/overseas_index/plugins --data "name=key-auth"# 創建overseas_user 用戶curl -X POST http://localhost:8888/consumers/ -d "username=overseas_test"# 為overseas_test創建權限驗證curl -X POST http://localhost:8888/consumers/overseas_test/key-auth -d ""# 嘗試查詢海外版指標接口curl -X POST https://10.182.20.127/stat/overseas/realtime -d "app_id=53ace32656240b11c2071b1a" -d "date=2015-07-22" -H "apikey: 5e3b2a7a735744b39aeea9ebc2de1f01" --insecure# 給測試APIcurl -X POST https://10.182.20.128/test -d "app_id=53ace32656240b11c2071b1a" -d "date=2015-07-22" -H "apikey: 5e3b2a7a735744b39aeea9ebc2de1f01" --insecure
設置ACL插件
# 給另外一個testapi加上權限驗證插件,現在任何通過認證的用戶都可以調用此API# 這不是我們所希望的,所以需要加入ACL控制curl -X POST http://localhost:8888/apis/testapi/plugins --data "name=key-auth"# 為用戶overseas_test創建ACL群組:overseas_usercurl -X POST http://localhost:8888/consumers/overseas_test/acls \--data "group=overseas_user"# 為用戶overseas_test創建ACL群組:overseas_usercurl -X POST http://localhost:8888/consumers/overseas_test/acls \--data "group=overseas_user"
