一、 安装Kong
1. 构建Kong的容器网络
docker network create kong-net
2. 搭建数据库环境
Cassandra容器
docker run -d --name kong-database \
--network=kong-net \
-p 9042:9042 \
cassandra:latest
PostgreSQL容器
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6
持久化数据到宿主机
这里有个小问题。如果你使用的是PostgreSQL,想挂载卷持久化数据到宿主机。通过 -v 命令是不好用的。这里推荐你使用 docker volume create 命令来创建一个挂载。
docker volume create kong-volume
然后上面的PostgreSQL就可以通过- v kong-volume:/var/lib/postgresql/data 进行挂载了。
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" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6
初始化或者迁移数据库
我们使用docker run —rm来初始化数据库,该命令执行后会退出容器而保留内部的数据卷(volume)。
docker run --rm --network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" \
kong:2.7.1-alpine kong migrations bootstrap
3. 启动Kong容器
docker run -d --name kong-gateway \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-p 8000:8000 \
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
kong:2.7.1-alpine
配置相关日志
-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" \
4. 端口说明
Kong默认监听下面端口
8000,监听来自客户端的HTTP流量,转发到你的upstream服务上。
8443,监听HTTPS的流量,功能跟8000一样。可以通过配置文件禁止。
8001,Kong的HTTP监听的api管理接口。
8444,Kong的HTTPS监听的API管理接口。
作者:DreamsonMa
链接:https://www.jianshu.com/p/5049b3bb4b80
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
安装Kong 管理UI
Konga 主要是用 AngularJS 写的,运行于nodejs服务端。具有以下特性:
- 管理所有Kong Admin API对象。
- 支持从远程源(数据库,文件,API等)导入使用者。
- 管理多个Kong节点。使用快照备份,还原和迁移Kong节点。
- 使用运行状况检查监视节点和API状态。
- 支持电子邮件和闲置通知。
- 支持多用户。
- 易于数据库集成(MySQL,postgresSQL,MongoDB,SQL Server)。
创建Konga数据库
创建挂载卷
docker volume create konga-postgresql
启动数据库
此处可以和kong共用一个数据库
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" \
-e "POSTGRES_PASSWORD=konga" \
postgres:9.6
初始化Konga数据库
docker run --rm --network=kong-net \
pantsel/konga:latest \
-c prepare \
-a postgres \
-u postgresql://konga:konga@konga-database:5432/konga
相关命令解读
命令 | 描述 | 默认 |
---|---|---|
-c | 执行的命令,这里我们执行的是prepare | - |
-a | adapter 简写 ,可以是postgres 或者mysql | - |
-u | db url 数据库连接全称 | - |
其他环境参数
Konga 还有一些可配置的环境参数:
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 | - |
启动Konga
docker run -d -p 1337:1337 \
-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:latest
或
docker run -p 1337:1337 \
--network kong-net \
--name konga \
-e "NODE_ENV=production" \
-e "DB_ADAPTER=postgres" \
-e "DB_HOST=konga-database" \
-e "DB_USER=konga" \
-e "DB_PASSWORD=konga" \
-e "DB_DATABASE=konga" \
pantsel/konga
访问地址
运行完后,如果成功可以通过http://localhost:1337
链接到控制台。通过注册后进入,然后在dashboard面板里面添加Kong的管理Api路径http://yourdomain
。这里添加docker别名 http://kong:8001
。
安装kong-dashboard管理UI
docker run --rm --network kong-net -p 8080:8080 pgbi/kong-dashboard:latest start --kong-url http://kong-gateway:8001
[