2019-11-15
轉載於:https://mojotv.cn/misc/kong-

Kong是一個在Nginx運行的Lua應用程序,由lua-nginx-module實現.Kong和OpenResty一起打包發行,其中已經包含了lua-nginx-module.OpenResty不是Nginx的分支,而是一組擴展其功能的模塊.
您可以通過增加更多 Kong Server 機器對 Kong 服務進行水平擴展,通過前置的負載均衡器向這些機器分發請求.根據文檔描述,兩個Cassandra節點就足以支撐絕大多數情況,但如果網絡非常擁擠,可以考慮適當增加更多節點.
對於開源社區來說,Kong 中最誘人的一個特性是可以通過插件擴展已有功能,這些插件在 API 請求響應循環的生命周期中被執行.插件使用 Lua 編寫,而且Kong還有如下幾個基礎功能:HTTP 基本認證,密鑰認證,CORS( Cross-origin Resource Sharing,跨域資源共享),TCP,UDP,文件日志,API 請求限流,請求轉發以及 nginx 監控.
Kong 是在客戶端和(微)服務間轉發API通信的API網關,通過插件擴展功能.Kong 有兩個主要組件:
- Kong Server :基於nginx的服務器,用來接收 API 請求.
- Apache Cassandra:用來存儲操作數據.
1. services 服務
Service entities, as the name implies, are abstractions of each of your own upstream services. Examples of Services would be a data transformation microservice, a billing API, etc.
Service 顧名思義,就是我們自己定義的上游服務,通過Kong匹配到相應的請求要轉發的地方, Service 可以與下面的Route進行關聯,一個Service可以有很多Route,匹配到的Route就會轉發到Service中, 當然中間也會通過Plugin的處理,增加或者減少一些相應的Header或者其他信息.
- Service服務,通過Kong匹配到相應的請求要轉發的地方(eg: 理解nginx 配置文件中server)
2. Routes 路由
Route entities define rules to match client requests. Each Route is associated with a Service, and a Service may have multiple Routes associated to it. Every request matching a given Route will be proxied to its associated Service.
The combination of Routes and Services (and the separation of concerns between them) offers a powerful routing mechanism with which it is possible to define fine-grained entry-points in Kong leading to different upstream services of your infrastructure.
Route 路由相當於nginx 配置中的location
Route實體定義匹配客戶端請求的規則. 每個路由都與一個服務相關聯,而服務可能有多個與之相關聯的路由. 每一個匹配給定路線的請求都將被提交給它的相關服務.
路由和服務的組合(以及它們之間的關注點分離)提供了一種強大的路由機制, 可以在Kong中定義細粒度的入口點,從而引導您的訪問到不同upstream服務.
3. Consumers 消費者
The Consumer object represents a consumer - or a user - of a Service. You can either rely on Kong as the primary datastore, or you can map the consumer list with your database to keep consistency between Kong and your existing primary datastore.
最簡單的理解和配置consumer的方式是,將其於用戶進行一一映射,即一個consumer代表一個用戶(或應用).但是對於KONG而言,這些都無所謂. Consumer的核心原則是您可以為其添加插件,從而自定義他的請求行為. 所以,或許您會有一個手機APP應用,並為他的每個版本都定義一個consumer, 又或者您有一個應用或幾個應用,並為這些應用定義統一個consumer,這些都無所謂.
- Consumer是使用Service的用戶(eg: github賬號就是一個Consumer是使用github Open API Service)
- Consumer的核心原則是您可以為其添加Plugin插件,從而自定義他的請求行為.(eg: 安裝kong Oauth2插件)
- Consumer下可以創建多個APP(eg:您的github賬號中您可以創建多個Github Apps )
4. Kong Centos7 安裝
# 下載kong rpm 的安裝包 wget https://bintray.com/kong/kong-rpm/download_file?file_path=centos/7/kong-1.4.0.el7.amd64.rpm -O kong-1.4.0.el7.amd64.rpm sudo yum install epel-release # 安裝下載好的kong rpm 安裝文件 sudo yum install kong-1.4.0.*.rpm --nogpgcheck # 復制默認kong 配置文件 cp /etc/kong/kong.conf.default /etc/kong/kong.conf # run kong kong
5. Kong Docker 安裝
# 創建docker network docker network create kong-net # 安裝pg9.6 docker run -d --name kong-database \ --network=kong-net \ -p 5432:5432 \ -e "POSTGRES_USER=kong" \ -e "POSTGRES_DB=kong" \ postgres:9.6 # 數據庫遷移 docker run --rm \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ kong:1.4.0 kong migrations bootstrap # 運行kong 鏡像 docker run -d --name kong \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ -p 8000:8000 \ -p 8443:8443 \ -p 8001:8001 \ -p 8444:8444 \ kong:1.4.0 # 測試 kong管理API curl -i http://localhost:8001/
END