Kong API Gateway

Baisali Pradhan
5 min readFeb 8, 2022

Nowadays most of the company moves from monolithic to microservices. And Microservices API Gateway plays a very much important role in communication between them.

The API Gateway is a server. It is a single entry point into a system. API Gateway encapsulates the internal system architecture. It provides an API that is tailored to each client. It also has other responsibilities such as authentication, monitoring, load balancing, caching, request shaping and management, and static response handling.

So Let’s know about Kong API Gateway !!

What is Kong?

Kong is a widely adopted, open-source API Gateway, written in Lua. It runs on top of Nginx and provides a simple RESTful API that can be used to provision your infrastructure in a dynamic way.

For this tutorial, we will use the lightsail of AWS.

Open AWS console and search lightsail in the search bar.

Click on create instance button.

  1. Select OS Only and Ubuntu 20.04 LTS as shown below.

2. Select pricing as $10 USD as it is free for 1st 3 months.

3. Give a name to your instance

And now hit the create instance button. And here you go🕺. Now your instance is ready to use.

Go to the network option and click on “create static ip” and Attach it to auth-service.

It is needed because using this IP address you can run the project from anywhere.

Then install Docker, PostgreSQL, and nodejs in the terminal.

  1. Create docker.yml file by using command touch docker-compose.yml
  2. Open the docker file by using nano docker-compose.yml
  3. Add services for PostgreSQL, Kong, and Konga (UI for kong ) by using the below command.

version: “3”

networks:
kong-net:
driver: bridge

services:

#######################################
# Postgres: The database used by Kong
#######################################
kong-database:
image: postgres:9.6
restart: always
networks:
— kong-net
environment:
POSTGRES_USER: kong
POSTGRES_DB: kong
ports:
— “5432:5432”
healthcheck:
test: [“CMD”, “pg_isready”, “-U”, “kong”]
interval: 5s
timeout: 5s
retries: 5

#######################################
# Kong database migration
#######################################
kong-migration:
image: kong:latest
command: “kong migrations bootstrap”
networks:
— kong-net
restart: on-failure
environment:
KONG_PG_HOST: kong-database
links:
— kong-database
depends_on:
— kong-database

#######################################
# Kong: The API Gateway
#######################################
kong:
image: kong:latest
restart: always
networks:
— kong-net
environment:
KONG_PG_HOST: kong-database
KONG_PROXY_LISTEN: 0.0.0.0:8000
KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
KONG_ADMIN_LISTEN: 0.0.0.0:8001
depends_on:
— kong-migration
— kong-database
healthcheck:
test: [“CMD”, “curl”, “-f”, “
http://kong:8001”]
interval: 5s
timeout: 2s
retries: 15
ports:
— “8001:8001”
— “8000:8000”

#######################################
# Konga database prepare
#######################################
konga-prepare:
image: pantsel/konga:next
command: “-c prepare -a postgres -u postgresql://kong@kong-database:5432/konga_db”
networks:
— kong-net
restart: on-failure
links:
— kong-database
depends_on:
— kong-database

#######################################
# Konga: Kong GUI
#######################################
konga:
image: pantsel/konga:next
restart: always
networks:
— kong-net
environment:
DB_ADAPTER: postgres
DB_HOST: kong-database
DB_USER: kong
TOKEN_SECRET: km1GUr4RkcQD7DewhJPNXrCuZwcKmqjb
DB_DATABASE: konga_db
NODE_ENV: production
depends_on:
— kong-database
ports:
— “1337:1337”

4. Install docker-compose by the command: sudo apt install docker-compose

5. Run the docker-compose file by using the command docker-compose up

Create a simple hello world project using Nodejs.

Dockerize the project using Dockerfile.

Your folder structure should look like below then push your code to docker.

push your locally run docker container to docker hub.

Click on the public view and then copy the pull command. Paste it on the lightsail terminal to pull your project on an EC2 instance.

Now you can see your project on docker. You can run your project on docker by using the command Docker run <” PROJECT IMAGE ID>

Configuring a service

Step1: Add a service
$ curl -X POST — url http://localhost:8001/services/ — data ‘name=my-api’ — data ‘url=http://<YOUR IP ADDRES >:3000'

Step2: Add a route
$ curl -X POST — url http://localhost:8001/services/my-api/routes — data ‘paths[]=/helloapi’

Step3: Call the API directly
$ curl -i http://<YOUR IP ADDRESS>:3000 in the terminal of lightsail or you can use insomnia or postman to check the URL. Here I’m using Insomnia.

Call the API behind kong
$ curl -i -X GET — url http://localhost:8000/helloapi and here we go !!👏👏

You can also check your output in insomnia or postman

--

--

Baisali Pradhan
Baisali Pradhan

No responses yet