By Marone: June 2020

Install and run Kong
with docker

kong brand logo

Table of contents

Goal

This is a qiuck article about how install and run Kong with docker.

Used technologies

Kong Gateway v2.x
Docker 19.x (Running on Windows)
cURL 7.x

What is Kong

Kong Gateway is a open source API Gateway. Kong Server is a lua application built on top of NGINX and acts like a API front-end.
The server forwards the traffic between consumers and the API's, the flexible Kong's plugin architecture make easy to add more functionality such as a rate limiting, authentication,request/response transformation, and more.
kong gateway
Kong exposes two endpoints:
Since launching Kong on 2015, Kong needs a database to store configuration such as Services, Routes, ...etc. The datastore could be either Cassandra or Postgres. From version 1.1 it has become possible to run kong without a database, this approach will be highlighted in this article.

Installation

Step1: Create a Docker network

docker network create kong-net

Step2: Start and prepare Postgres DB

docker run -d --name kong-database --network=kong-net \
-e "POSTGRES_USER=kong" -e "POSTGRES_DB=kong" -e "POSTGRES_PASSWORD=kong" \
-p 5432:5432 postgres:9.6

docker run --rm --network=kong-net \
-e "KONG_DATABASE=postgres" -e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_PASSWORD=kong" kong:latest kong migrations bootstrap

Step3: Start kong

docker run -d --name kong --network=kong-net \
-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

List docker containers (snippet)

docker ps -a
IMAGE                         COMMAND                  PORTS                                                                NAMES
kong:latest                   "/docker-entrypoint."   0.0.0.0:8000-8001->8000-8001/tcp, 0.0.0.0:8443-8444->8443-8444/tcp   kong
restapi                       "sh -c 'java $JAVA_O"   0.0.0.0:8080->8080/tcp                                               upbeat_volhard
postgres:9.6                  "docker-entrypoint.s"   0.0.0.0:5432->5432/tcp                                               kong-database

Now Kong is up and running

Configuring a service

Step1: Add a service

$ curl -X POST --url http://192.168.99.100:8001/services/ --data 'name=my-api' --data 'url=http://192.168.99.100:8080'

The Request Body contains:
name: The Service name
url: The upstream url

Response:
{
   "host":"192.168.99.100",
   "created_at":1591437925,
   "connect_timeout":60000,
   "id":"30671beb-26f0-4bb8-a45c-b78b8c4e1dbd",
   "protocol":"http",
   "name":"my-api",
   "read_timeout":60000,
   "port":8080,
   ...
}

Step2: Add a route

$ curl -X POST --url http://192.168.99.100:8001/services/my-api/routes --data 'paths[]=/helloapi'

The url specified the service name as attribute {/my-api}, which we created at step 1. With paths we define a path that match this new route.
Response:
{
   "id":"f0eabb2c-e208-4916-bc14-a1dbed4d1486",
   "path_handling":"v0",
   "paths":[
      "\/helloapi"
   
],
   "destinations":null,
   "headers":null,
   "protocols":[
      "http",
      "https"
   
],
   "methods":null,
   "snis":null,
   "service":{
      "id":"30671beb-26f0-4bb8-a45c-b78b8c4e1dbd"
   
},
   ...
}

Call the API directly

$ curl -i http://192.168.99.100:8080/hello
HTTP/1.1 200
Content-Type: text/plain
Content-Length: 11
Date: Sat, 06 Jun 2020 10:01:06 GMT

hello world

Call the API behind kong

$ curl -i -X GET --url http://192.168.99.100:8000/helloapi/hello
HTTP/1.1 200
Content-Type: text/plain; charset=UTF-8
Content-Length: 11
Connection: keep-alive
Date: Sat, 06 Jun 2020 10:24:58 GMT
X-Kong-Upstream-Latency: 5
X-Kong-Proxy-Latency: 1
Via: kong/2.0.4

hello world


References