Current Status

Actively playing with all possible backend services

24 July, 2018

Learnings - Kong API Gateway

 Hi folks, Many days I was playing with Kong in Docker, quick scaffolding for anyone interest to play with it. This summarises my basic learnings.
What is Kong?
Kong is an API gateway. Some of the popular features deployed through Kong include authentication, security, traffic control, serverless, analytics & monitoring, request/response transformations and logging.
A typical Kong setup is made of two main components:
1)Kong’s server, based on the widely adopted NGINX HTTP server
2)Kong’s datastore, Apache Cassandra and PostgreSQL can be used.

Terminology:
What are Kong plugins?
Kong API gateway features are provided by plugins. Authentication, rate-limiting, transformation, logging etc, are all implemented independently as plugins. Plugins can be installed and configured via the Admin API running alongside Kong.
What is a Kong Service?
a Service is the name Kong uses to refer to the upstream APIs and microservices it manages.

What is a Route?
Routes specify how (and if) requests are sent to their Services after they reach Kong. A single Service can have many Routes.

Who are Consumers?
Consumers are associated to individuals using Kong APIs, and can be used for tracking, access management, and more.
What is an Upstream service?
Upstream service refers to API/service sitting behind Kong(our backend service), to which client requests are forwarded.
What is an API?
API is a legacy entity/term used to represent your upstream services. Deprecated in favor of Services since 0.13.0.

Running it locally: 

Steps

  1. Create a Docker network:
>docker network create kong-network
(you can have any name for "kong-network") 

2. Run Kong DB :
run Postgre DB ( here I have used Postgre, but you can use Casandra also)

>docker run -d --name kong-database \
--network=kong-network \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.6
*Docker tip: next time (start): docker start kong-database or you can start with process id
Eg:
>docker ps -a
>docker start 8c960a6b930f

3. Configure database /run the migration
>docker run --rm \
--network=kong-network \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
kong:latest kong migrations up

*using --rm switch - container gets destroyed automatically as soon as it is stopped

4. Run Kong on docker in same network

>docker run -d --name kong \
--network=kong-network \
--expose=8000 \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=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 8001:8001 \
-p 8444:8444 \
kong:latest
test:
>curl -X GET http://127.0.0.1:8001/
5. Creating dummy endpoint for testing

You can use any HTTP rest endpoint mocking tool, I prefer https://www.mocky.io/
***Note: if you want to test your own API running in separate docker container, make sure that API docker container is in the same network (which makes 'localhost' access to Kong container)
or simply add 'Service' to Kong with the IP as the 'host'

6. Add a service
>curl -i -X POST http://localhost:8001/services/ \
-d "name=mockyService1" \
-d "protocol=http" \
-d "host=mocky.io" \
-d "port=80"
or all-in-one :
>curl -i -X POST http://localhost:8001/services/ \
-d "name=mockyService1" \
-d "url=http://www.mocky.io:80"
test:
>curl -X GET http://127.0.0.1:8001/services/

delete a service?
>curl -X DELETE http://127.0.0.1:8001/services/ac0582e9-40d5-49bc-b711-615216d51b5f

7. Add routes
>curl -i -X POST http://localhost:8001/services/mockyService1/routes/ \
-d "protocols[]=http" \
-d "hosts[]=requesthostname.com" \
-d "paths[]=/v2/5b5558d3320000b804827d47" \
-d "strip_path=false"
instead, you could use the JSON format:
>curl -s -X POST http://localhost:8001/services/mockyService1/routes/ \
-H 'Content-Type: application/json' \
-d '{ "protocols": ["http"], "hosts": ["requesthostname.com"], "paths": ["/v2/5b5558d3320000b804827d47"], "strip_path": false }'
test:
>curl -i http://localhost:8000/v2/5b5558d3320000b804827d47 -H 'Host: requesthostname.com'

how to delete a route?
get id from :
>curl -X GET http://127.0.0.1:8001/services/serviceName/routes

>curl -X DELETE http://127.0.0.1:8001/routes/5c293b78-38d3-4129-b8b1-e7001e1d37c4

References:
Kong Docker installation:
https://docs.konghq.com/install/docker/

Kong Kubernetes installation:
https://docs.konghq.com/install/kubernetes/

Other installations:
https://konghq.com/install/
Kubernetes Ingress Controller for Kong :
https://www.youtube.com/watch?v=2LInu_ZiL3E

Some Popular Posts