others-How to install kong api gateway and konga using docker?

1. Purpose

In this post, I would demonstrate how to install kong api gateway and konga using docker.

2. The solution

2.1 What is kong api gateway?

An API gateway acts as a proxy for your application’s microservices, exposing the public-facing API endpoints, routing incoming client requests to the relevant services, transforming them as required and aggregating the response data before sending the response to the client.

image-20211201201551105

Kong provides a flexible abstraction layer that securely manages communication between clients and microservices via API. … Kong can help by acting as a gateway (or a sidecar) for microservices requests while providing load balancing, logging, authentication, rate-limiting, transformations, and more through plugins.

To get all the plugins, you can navigate to https://docs.konghq.com/hub/, where list all the plugins of kong api gateway.

2.2 How to install kong api gateway using docker?

2.2.1 Create a docker volume to store data for the kong database
docker volume create kong-vol

Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container. The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.

image-20211201202336290

Docker volumes are important because when a Docker container is destroyed, it’s entire file system is destroyed too. So if we want to keep this data, it is necessary that we use Docker volumes.

2.2.2 Create a docker network for kong api gateway containers
docker network create kong-net

Docker networking allows you to attach a container to as many networks as you like. You can also attach an already running container. Go ahead and attach your running web app to the my_bridge.

In order to use the name instead of IP address(which is dynamic) to communicate with each other(kong and its postgres database), we must attach both containers to the same docker network.

image-20211201202950314

Docker networking enables a user to link a Docker container to as many networks as he/she requires. Docker Networks are used to provide complete isolation for Docker containers. Note: A user can add containers to more than one network.

2.2.3 Run the postgres database and initialize the database
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

You can see that we are attaching the postgresql database to the docker network kong-net.

Now we should initialize the database(create schema and tables):

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

We got this result:

Bootstrapping database...
migrating core on database 'kong'...
core migrated up to: 000_base (executed)
core migrated up to: 003_100_to_110 (executed)
core migrated up to: 004_110_to_120 (executed)
core migrated up to: 005_120_to_130 (executed)
core migrated up to: 006_130_to_140 (executed)
core migrated up to: 007_140_to_150 (executed)
core migrated up to: 008_150_to_200 (executed)
core migrated up to: 009_200_to_210 (executed)
core migrated up to: 010_210_to_211 (executed)
core migrated up to: 011_212_to_213 (executed)
core migrated up to: 012_213_to_220 (executed)
core migrated up to: 013_220_to_230 (executed)
migrating acl on database 'kong'...
acl migrated up to: 000_base_acl (executed)
acl migrated up to: 002_130_to_140 (executed)
acl migrated up to: 003_200_to_210 (executed)
acl migrated up to: 004_212_to_213 (executed)
migrating acme on database 'kong'...
acme migrated up to: 000_base_acme (executed)
migrating basic-auth on database 'kong'...
basic-auth migrated up to: 000_base_basic_auth (executed)
basic-auth migrated up to: 002_130_to_140 (executed)
basic-auth migrated up to: 003_200_to_210 (executed)
migrating bot-detection on database 'kong'...
bot-detection migrated up to: 001_200_to_210 (executed)
migrating hmac-auth on database 'kong'...
hmac-auth migrated up to: 000_base_hmac_auth (executed)
hmac-auth migrated up to: 002_130_to_140 (executed)
hmac-auth migrated up to: 003_200_to_210 (executed)
migrating ip-restriction on database 'kong'...
ip-restriction migrated up to: 001_200_to_210 (executed)
migrating jwt on database 'kong'...
jwt migrated up to: 000_base_jwt (executed)
jwt migrated up to: 002_130_to_140 (executed)
jwt migrated up to: 003_200_to_210 (executed)
migrating key-auth on database 'kong'...
key-auth migrated up to: 000_base_key_auth (executed)
key-auth migrated up to: 002_130_to_140 (executed)
key-auth migrated up to: 003_200_to_210 (executed)
migrating oauth2 on database 'kong'...
oauth2 migrated up to: 000_base_oauth2 (executed)
oauth2 migrated up to: 003_130_to_140 (executed)
oauth2 migrated up to: 004_200_to_210 (executed)
oauth2 migrated up to: 005_210_to_211 (executed)
migrating rate-limiting on database 'kong'...
rate-limiting migrated up to: 000_base_rate_limiting (executed)
rate-limiting migrated up to: 003_10_to_112 (executed)
rate-limiting migrated up to: 004_200_to_210 (executed)
migrating response-ratelimiting on database 'kong'...
response-ratelimiting migrated up to: 000_base_response_rate_limiting (executed)
migrating session on database 'kong'...
session migrated up to: 000_base_session (executed)
session migrated up to: 001_add_ttl_index (executed)
41 migrations processed
41 executed
Database is up-to-date
2.2.4 Start kong container
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

2.3 Install konga

2.3.1 What is konga?

Konga is a fully-featured open-source, multi-user GUI, that makes the hard task of managing multiple Kong installations easy way. It can be integrated with MySQL, postgresSQL, MongoDB databases out of the box, and provides the GUI for better understanding and maintain architecture

konga

2.3.2 How to install konga?

1) we need to create a volume for konga too.

docker volume create konga-postgresql

2) start the docker container for 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" \
-e "POSTGRES_PASSWORD=konga" \
postgres:9.6

Notice the --network option to specify the same network as kong.

3) initialize konga database as follows:

docker run --rm  --network=kong-net  \
pantsel/konga:latest \
-a postgres \
-c prepare \
-u postgres://konga:konga@konga-database:5432/konga

We got this:

debug: Preparing database...
Using postgres DB Adapter.
Database exists. Continue...
debug: Hook:api_health_checks:process() called
debug: Hook:health_checks:process() called
debug: Hook:start-scheduled-snapshots:process() called
debug: Hook:upstream_health_checks:process() called
debug: Hook:user_events_hook:process() called
debug: User had models, so no seed needed
debug: Kongnode had models, so no seed needed
debug: Emailtransport seeds updated
debug: Database migrations completed!

4) Start konga container:

docker run -d -p 1337:1337  \
               --network kong-net  \
               -e "DB_ADAPTER=postgres"  \
               -e "DB_URI=postgres://konga:konga@konga-database:5432/konga"  \
               -e "NODE_ENV=production"  \
               -e "DB_PASSWORD=konga" \
               --name konga \
               pantsel/konga

image-20211130154744821

It’s running and working.

3. Summary

In this post, I demonstrated how to install kong api gateway and konga using docker, you can see that it’s very simple and quick to install them using container technology. That’s it, thanks for your reading.