1. 前言
上一文我們對(duì)Kong網(wǎng)關(guān)的簡(jiǎn)單介紹,今天我們來搭建Kong的環(huán)境。以便于以后對(duì)Kong的進(jìn)一步了解和學(xué)習(xí)。
2. Kong支持的環(huán)境

從上圖來看Kong對(duì)Linux、MacOS、容器、云 支持的還是比較全面的。
3. 安裝Kong
你的機(jī)器中必須有Docker環(huán)境,這個(gè)是前提。。Kong 安裝有兩種方式一種是沒有數(shù)據(jù)庫(kù)依賴的DB-less 模式,另一種是with a Database 模式。我們這里使用第二種帶Database的模式,因?yàn)檫@種模式功能更全。
3.1 構(gòu)建Kong的容器網(wǎng)絡(luò)
首先我們創(chuàng)建一個(gè)Docker自定義網(wǎng)絡(luò),以允許容器相互發(fā)現(xiàn)和通信。在下面的創(chuàng)建命令中kong-net是我們創(chuàng)建的Docker網(wǎng)絡(luò)名稱。
docker network create kong-net
3.2 搭建數(shù)據(jù)庫(kù)環(huán)境
Kong 目前使用Cassandra(Facebook開源的分布式的NoSQL數(shù)據(jù)庫(kù)) 或者PostgreSql,你可以執(zhí)行以下命令中的一個(gè)來選擇你的Database。請(qǐng)注意定義網(wǎng)絡(luò) --network=kong-net 。
- Cassandra容器:
docker run -d --name kong-database \
--network=kong-net \
-p 9042:9042 \
cassandra:3
- PostgreSQL容器:
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
這里有個(gè)小問題。如果你使用的是PostgreSQL,想掛載卷持久化數(shù)據(jù)到宿主機(jī)。通過 -v 命令是不好用的。這里推薦你使用 docker volume create 命令來創(chuàng)建一個(gè)掛載。
docker volume create kong-volume
然后上面的PostgreSQL就可以通過- v kong-volume:/var/lib/postgresql/data 進(jìn)行掛載了。
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
- v kong-volume:/var/lib/postgresql/data
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
3.3 初始化或者遷移數(shù)據(jù)庫(kù)
我們使用docker run --rm來初始化數(shù)據(jù)庫(kù),該命令執(zhí)行后會(huì)退出容器而保留內(nèi)部的數(shù)據(jù)卷(volume)。
docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations bootstrap
這個(gè)命令我們還是要注意的,一定要跟你聲明的網(wǎng)絡(luò),數(shù)據(jù)庫(kù)類型、host名稱一致。同時(shí)注意Kong的版本號(hào),本文是在Kong 1.4.x 版本下完成的。
3.4 啟動(dòng)Kong容器
3.3步驟完成初始化或者遷移數(shù)據(jù)庫(kù)后,我們就可以啟動(dòng)一個(gè)連接到數(shù)據(jù)庫(kù)容器的Kong容器,請(qǐng)務(wù)必保證你的數(shù)據(jù)庫(kù)容器啟動(dòng)狀態(tài),同時(shí)檢查所有的環(huán)境參數(shù) -e 是否是你定義的環(huán)境。
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:latest
3.5 驗(yàn)證
可通過 curl -i <wiz_tmp_plugin_tag id="wizKMHighlighterSpan_t_t" style="color: rgb(0, 0, 0); cursor: pointer; border-bottom-width: 1px; border-bottom-color: rgb(0, 0, 204); border-bottom-style: dashed; background-color: rgb(234, 188, 244);">http</wiz_tmp_plugin_tag>://localhost:8001/ 或者瀏覽器調(diào)用 http://localhost:8001/ 來驗(yàn)證Kong Admin 是否聯(lián)通 。
[圖片上傳失敗...(image-6e277e-1577349919300)]
4. 安裝Kong 管理UI
Kong 企業(yè)版提供了管理UI,開源版本是沒有的。但是有很多的開源的管理 UI ,其中比較好用的是Konga。項(xiàng)目地址:https://github.com/pantsel/konga

4.1 Konga 特性
Konga 主要是用 AngularJS 寫的,運(yùn)行于nodejs服務(wù)端。具有以下特性:
- 管理所有Kong Admin API對(duì)象。
- 支持從遠(yuǎn)程源(數(shù)據(jù)庫(kù),文件,API等)導(dǎo)入使用者。
- 管理多個(gè)Kong節(jié)點(diǎn)。使用快照備份,還原和遷移Kong節(jié)點(diǎn)。
- 使用運(yùn)行狀況檢查監(jiān)視節(jié)點(diǎn)和API狀態(tài)。
- 支持電子郵件和閑置通知。
- 支持多用戶。
- 易于數(shù)據(jù)庫(kù)集成(MySQL,postgresSQL,MongoDB,SQL Server)。
4.2 docker安裝Konga
同樣我們今天通過Docker來安裝Konga。安裝步驟同樣遵循配置數(shù)據(jù)庫(kù),初始化數(shù)據(jù)庫(kù),啟動(dòng)容器的流程。
4.2.1 Konga數(shù)據(jù)庫(kù)容器
上面在4.1特性介紹中我們介紹了Konga支持的數(shù)據(jù)庫(kù)類型。這里我們依然使用PostgreSQL。請(qǐng)注意我新定義了掛載卷konga-postgresql 。
docker volume create konga-postgresql
docker run -d --name konga-database \
--network=kong-net \
-p 5433:5432 \
-v konga-postgresql:/var/lib/postgresql/data \
-e "POSTGRES_USER=konga" \
-e "POSTGRES_DB=konga" \
postgres:9.6
4.2.2 初始化Konga數(shù)據(jù)庫(kù)
初始化 PostgreSQL 數(shù)據(jù)庫(kù)。
docker run --rm --network=kong-net \
pantsel/konga:latest
-a postgres
-u postgres://konga@konga-database:5432/konga
相關(guān)命令解讀:
| 命令 | 描述 | 默認(rèn) |
|---|---|---|
| -c | 執(zhí)行的命令,這里我們執(zhí)行的是prepare | - |
| -a | adapter 簡(jiǎn)寫 ,可以是postgres 或者mysql | - |
| -u | db url 數(shù)據(jù)庫(kù)連接全稱 | - |
到此Konga的數(shù)據(jù)庫(kù)環(huán)境就搞定了。
4.2.3 環(huán)境參數(shù)
Konga 還有一些可配置的環(huán)境參數(shù):
| VAR | DESCRIPTION | VALUES | DEFAULT |
|---|---|---|---|
| HOST | The IP address that will be bind by Konga's server | - | '0.0.0.0' |
| PORT | The port that will be used by Konga's server | - | 1337 |
| NODE_ENV | The environment |
production,development
|
development |
| SSL_KEY_PATH | If you want to use SSL, this will be the absolute path to the .key file. Both SSL_KEY_PATH & SSL_CRT_PATH must be set. |
- | null |
| SSL_CRT_PATH | If you want to use SSL, this will be the absolute path to the .crt file. Both SSL_KEY_PATH & SSL_CRT_PATH must be set. |
- | null |
| KONGA_HOOK_TIMEOUT | The time in ms that Konga will wait for startup tasks to finish before exiting the process. | - | 60000 |
| DB_ADAPTER | The database that Konga will use. If not set, the localDisk db will be used. |
mongo,mysql,postgres
|
- |
| DB_URI | The full db connection string. Depends on DB_ADAPTER. If this is set, no other DB related var is needed. |
- | - |
| DB_HOST | If DB_URI is not specified, this is the database host. Depends on DB_ADAPTER. |
- | localhost |
| DB_PORT | If DB_URI is not specified, this is the database port. Depends on DB_ADAPTER. |
- | DB default. |
| DB_USER | If DB_URI is not specified, this is the database user. Depends on DB_ADAPTER. |
- | - |
| DB_PASSWORD | If DB_URI is not specified, this is the database user's password. Depends on DB_ADAPTER. |
- | - |
| DB_DATABASE | If DB_URI is not specified, this is the name of Konga's db. Depends on DB_ADAPTER. |
- | konga_database |
| DB_PG_SCHEMA | If using postgres as a database, this is the schema that will be used. | - | public |
| KONGA_LOG_LEVEL | The logging level |
silly,debug,info,warn,error
|
debug on dev environment & warn on prod. |
| TOKEN_SECRET | The secret that will be used to sign JWT tokens issued by Konga | - | - |
| NO_AUTH | Run Konga without Authentication | true/false | - |
| BASE_URL | Define a base URL or relative path that Konga will be loaded from. Ex: www.example.com/konga | - | |
| KONGA_SEED_USER_DATA_SOURCE_FILE | Seed default users on first run. Docs. | - | |
| KONGA_SEED_KONG_NODE_DATA_SOURCE_FILE | Seed default Kong Admin API connections on first run Docs | - |
4.2.4 啟動(dòng)Konga
通過以下命令就可以啟動(dòng)Konga容器了
docker run -d -p 1337:1337 \
--network kong-net \
-e "DB_ADAPTER=postgres" \
-e "DB_URI=postgres://konga@konga-database:5432/konga" \
-e "NODE_ENV=production" \
-e "DB_PASSWORD=konga" \
--name konga \
pantsel/konga
運(yùn)行完后,如果成功可以通過http://localhost:1337 鏈接到控制臺(tái)。通過注冊(cè)后進(jìn)入,然后在dashboard面板里面添加Kong的管理Api路徑http://yourdomain 。這里添加docker別名 http://kong:8001 。