官网地址
github地址

一、 安装Kong

1. 构建Kong的容器网络

  1. docker network create kong-net

2. 搭建数据库环境

Cassandra容器

  1. docker run -d --name kong-database \
  2. --network=kong-net \
  3. -p 9042:9042 \
  4. cassandra:latest

PostgreSQL容器

  1. docker run -d --name kong-database \
  2. --network=kong-net \
  3. -p 5432:5432 \
  4. -e "POSTGRES_USER=kong" \
  5. -e "POSTGRES_DB=kong" \
  6. -e "POSTGRES_PASSWORD=kong" \
  7. postgres:9.6

持久化数据到宿主机

这里有个小问题。如果你使用的是PostgreSQL,想挂载卷持久化数据到宿主机。通过 -v 命令是不好用的。这里推荐你使用 docker volume create 命令来创建一个挂载。

  1. docker volume create kong-volume

然后上面的PostgreSQL就可以通过- v kong-volume:/var/lib/postgresql/data 进行挂载了。

  1. docker run -d --name kong-database \
  2. --network=kong-net \
  3. -p 5432:5432 \
  4. -v kong-volume:/var/lib/postgresql/data \
  5. -e "POSTGRES_USER=kong" \
  6. -e "POSTGRES_DB=kong" \
  7. -e "POSTGRES_PASSWORD=kong" \
  8. postgres:9.6

初始化或者迁移数据库

我们使用docker run —rm来初始化数据库,该命令执行后会退出容器而保留内部的数据卷(volume)。

  1. docker run --rm --network=kong-net \
  2. -e "KONG_DATABASE=postgres" \
  3. -e "KONG_PG_HOST=kong-database" \
  4. -e "KONG_PG_PASSWORD=kong" \
  5. kong:2.7.1-alpine kong migrations bootstrap

3. 启动Kong容器

  1. docker run -d --name kong-gateway \
  2. --network=kong-net \
  3. -e "KONG_DATABASE=postgres" \
  4. -e "KONG_PG_HOST=kong-database" \
  5. -e "KONG_PG_USER=kong" \
  6. -e "KONG_PG_PASSWORD=kong" \
  7. -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
  8. -p 8000:8000 \
  9. -p 8443:8443 \
  10. -p 127.0.0.1:8001:8001 \
  11. -p 127.0.0.1:8444:8444 \
  12. kong:2.7.1-alpine

配置相关日志

  1. -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
  2. -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
  3. -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
  4. -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数据库

创建挂载卷

  1. docker volume create konga-postgresql

启动数据库

此处可以和kong共用一个数据库

  1. docker run -d --name konga-database \
  2. --network=kong-net \
  3. -p 5433:5432 \
  4. -v konga-postgresql:/var/lib/postgresql/data \
  5. -e "POSTGRES_USER=konga" \
  6. -e "POSTGRES_DB=konga" \
  7. -e "POSTGRES_PASSWORD=konga" \
  8. postgres:9.6

初始化Konga数据库

  1. docker run --rm --network=kong-net \
  2. pantsel/konga:latest \
  3. -c prepare \
  4. -a postgres \
  5. -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

  1. docker run -d -p 1337:1337 \
  2. -e "DB_ADAPTER=postgres" \
  3. -e "DB_URI=postgres://konga@konga-database:5432/konga" \
  4. -e "NODE_ENV=production" \
  5. -e "DB_PASSWORD=konga" \
  6. --name konga \
  7. pantsel/konga:latest

  1. docker run -p 1337:1337 \
  2. --network kong-net \
  3. --name konga \
  4. -e "NODE_ENV=production" \
  5. -e "DB_ADAPTER=postgres" \
  6. -e "DB_HOST=konga-database" \
  7. -e "DB_USER=konga" \
  8. -e "DB_PASSWORD=konga" \
  9. -e "DB_DATABASE=konga" \
  10. pantsel/konga

访问地址

运行完后,如果成功可以通过http://localhost:1337 链接到控制台。通过注册后进入,然后在dashboard面板里面添加Kong的管理Api路径http://yourdomain 。这里添加docker别名 http://kong:8001

安装kong-dashboard管理UI

  1. docker run --rm --network kong-net -p 8080:8080 pgbi/kong-dashboard:latest start --kong-url http://kong-gateway:8001

[

](http://kong-gateway:8001)