API Gateway With Kong
Intro API Kong Gateway And Docker Demo
Kong Gateway
Kong Gateway is a lightweight, fast, and flexible cloud-native API gateway. An API gateway is a reverse proxy that lets you manage, configure, and route requests to your APIs. — Konghq
What Is Kong?
Kong act as Restful API to provide comunicate between client and microservice mesh. Kong Gateway runs in front of any RESTful API and can be extended through modules and plugins. Based on documentation, the user can do several things with Kong Gateway
- Leverage workflow automation and modern GitOps practices
- Decentralize applications/services and transition to microservices
- Create a thriving API developer ecosystem
- Proactively identify API-related anomalies and threats
- Secure and govern APIs/services, and improve API visibility across the entire organization.
Kong gateway offers 2 product for API Gateway service, enterprise and Open-Source. The most striking difference is in the use of plugins, if the need to use the basic functions of Kong is enough to use OSS, if it is necessary to use advanced plugins, it is necessary to use the enterprise version. In this article we’ll show how to install Kong Gateway on Docker.
How It Works?
And here is an illustration, how Kong Gateway manages incoming traffic and responds to it:
- From Client send Request to Kong By Hit Kong Endpoint
- Kong will provide request and forward to Backend
- Backend will respos request and sending response back to Kong
- Kong will Forward Response from Backend to Client
Where can install?
Kong provide any method for installation (Containerized and VM Based) you can see more in here
Demo
Overview
In this demo we’ll install Kong on docker with DB Less Mode and DB Mode
Prerequires
For this installation, make sure that you have installed docker engine on your machine
Step-by-Step
For the first demo we will using Kong DB Less Mode
Create Docker Network
docker network create kong-net
Create Volume
docker volume create kong-vol
docker volume inspect kong-vol
Prepare your declarative configuration file
create file in mount point volume with *.yaml/yml extension, example: kong.yml
kong.yaml
...._format_version: "2.1"
_transform: trueservices:
- name: my-service
url: https://example.com
plugins:
- name: key-auth
routes:
- name: my-route
paths:
- /consumers:
- username: my-user
keyauth_credentials:
- key: my-key
....
Start the Kong
docker run -d --name kong \
--network=kong-net \
-v "kong-vol:/usr/local/kong/declarative" \
-e "KONG_DATABASE=off" \
-e "KONG_DECLARATIVE_CONFIG=/usr/local/kong/declarative/kong.yml" \
-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 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
kong:latest
Check
curl -i http://localhost:8001/curl -i http://localhost:8001/services
The Second Demo we use DB
Create Network
docker network create kong-net
Start database (2 Option Choose one)
#Postgres
docker run -d --name kong-database \
--network=kong-net \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
-e "POSTGRES_PASSWORD=kong" \
postgres:9.6#Casandra
docker run -d --name kong-database \
--network=kong-net \
-p 9042:9042 \
cassandra:3
Prepare database
docker run --rm \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations bootstrap
Start Kong
docker run -d --name kong \
--network=kong-net \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_PG_USER=kong" \
-e "KONG_PG_PASSWORD=kong" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
-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 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
kong:latest
Test
curl -i http://localhost:8001/
Closing
What Next?
For the next article we’ll try to use Kong Gateway Pluging (Kong Administration) to help our job with kong. Stay tune …
By Sulaiman, Ops Team Btech
Our Tagline
#ContinuousLearning