Posts Install Kong and Konga on Docker
Post
Cancel

Install Kong and Konga on Docker

How to install Kong API Gateway and Konga on Docker

Step1: Create a Docker network

1
docker network create my-network

Step2: Install Postgress and prepare Postgres DB for Kong

1
2
3
docker run -d --name kong-database --network=my-network \
-e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" -e "POSTGRES_PASSWORD=kong" \
-p 5432:5432 postgres:9.6
1
2
3
docker run --rm --network=my-network \
-e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" kong:latest kong migrations bootstrap

Step3: Install and start Kong

1
2
3
4
5
6
docker run -d --name kong --network=my-network \
-e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" -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

Step4: Check Kong is started?

1
2
docker ps
curl -X POST --url http://localhost:8001/services/ --data 'name=my-api' --data 'url=http://localhost:8080'

Install Konga(Kong Admin)

Prepare Konga database

1
docker run --rm --network=my-network pantsel/konga -c prepare -a postgres -u postgresql://kong:kong@kong-database:5432/konga_db

Install and start Konga

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

ACCESS

1
Access Konga at http://localhost:1337

Connect Konga to Kong

dont’t use localhost , use ifconfig ip image

Next, you need to activate the created connection by clicking on activate button.

image

If successful, you will receive a sidebar menu with full features like this:

image

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
对应的 Kong 配置

配置 upstream
# curl -X POST http://localhost:8001/upstreams --data "name=helloUpstream"

配置 target
# curl -X POST http://localhost:8001/upstreams/helloUpstream/targets --data "target=localhost:3000" --data "weight=100"

配置 service
# curl -X POST http://localhost:8001/services --data "name=hello" --data "host=helloUpstream"

配置 route
# curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id=8695cc65-16c1-43b1-95a1-5d30d0a50409"

这一切都是动态的,无需手动 reload nginx.conf

为 Kong 新增路由信息时涉及到了 upstream,target,service,route 等概念,便是 Kong 最核心的四个对象。

为 hello 服务添加50次/秒的限流:

登录后复制
# curl -X POST http://localhost:8001/services/hello/plugins \
	--data "name=rate-limiting" \
	--data "config.second=50"

为 hello 服务添加 jwt 插件:

# curl -X POST http://localhost:8001/services/login/plugins \
	--data "name=jwt"

同理,插件也可以安装在 route 之上

#	curl -X POST http://localhost:8001/routes/{routeId}/plugins \
	--data "name=rate-limiting" \
	--data "config.second=50"

# curl -X POST http://localhost:8001/routes/{routeId}/plugins \
	--data "name=jwt"

reference

  • https://learncode24h.com/how-to-install-kong-api-gateway-and-konga-on-docker/
  • https://blog.51cto.com/caiyuanji/2483242
This post is licensed under CC BY 4.0 by the author.