Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Next Generation RESTful API Management

DZone's Guide to

Next Generation RESTful API Management

We are going to set up the Kong API gateway in the Dockerized environment for Dynamic routing.

· Integration Zone ·
Free Resource

Kong architecture

Kong is a scalable, open source API Middleware. Kong runs in front of any RESTful API and is extended through Plugins, which provide extra functionality and services beyond the core platform.

Why Kong?

Kong is built on top of reliable technologies like NGINX + Openresty and Apache Cassandra or PostgreSQL and provides you with an easy-to-use RESTful API to operate and configure the system. Kong brings core management functionality out of the box for your API such as:

  • API Rate Limiting
  • API Metering
  • Dynamic Routing

Getting Started With Kong API Gateway Set Up:

We are going to set up the Kong API gateway in the Dockerized environment for Dynamic routing. Below is the architecture diagram to understand the overview.

Kong Service route flow

1. Create a docker network

docker network create kong-network

2. Create Cassandra DB.

docker run -d --name kong-database --network=kong-network -p 9042:9042 cassandra:3

Running Cassandra on docker.

3. Configure Cassandra for Kong

docker run --rm --network=kong-network -e "KONG_DATABASE=cassandra" -e "KONG_CASSANDRA_KEYSPACE=kong" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" kong:0.14.1 kong migrations up

This command will create schema in Cassandra.

4. Run Kong Application.

docker run -d --name kong --network=kong-network -e "KONG_DATABASE=cassandra" -e "KONG_CASSANDRA_KEYSPACE=kong" -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8443 ssl" -e "KONG_PROXY_LISTEN=0.0.0.0:80, 0.0.0.0:443 ssl" -p 80:80 -p 443:443 -p 8001:8001 -p 8444:8444 kong:0.14.1


Note: Kong API will be running on Port 8001 and we have configured proxy service to be run on 80 port.

This command will create a Kong container.

5. Verify Kong services.

curl -X GET http://127.0.0.1:8001/


6. Create test services to perform testing.

A Create the first service and update index.html to identified.

docker run -d --name nginx-v1 --network=kong-network nginx:latest

docker exec -it nginx-v1 bash

echo "API v1 Calls" > /usr/share/nginx/html/index.html

B. Create the second service and update index.html.

docker run -d --name nginx-v2 --network=kong-network nginx:latest

docker exec -it nginx-v2 bash

echo "API v2 Calls" > /usr/share/nginx/html/index.html

7. Register service in Kong (Create service).

A. curl -i -X POST --url http://localhost:8001/services/ --data 'name=service-v1' --data 'url=http://nginx-v1'

B. curl -i -X POST --url http://localhost:8001/services/ --data 'name=service-v2' --data 'url=http://nginx-v2'

The above command will generate a service id. This will be used in route create.

8. Create a routing rule.

Kong matches a request against the configured hosts, paths, and methods properties of a Route. we are going to use path based routing.

Create Route for service A

curl -i -X POST http://localhost:8001/routes/ -d 'paths[]=/v1' -d 'service.id={Service.id}


Create Route for service B

curl -i -X POST http://localhost:8001/routes/ -d 'paths[]=/v2' -d 'service.id={Service.id}


Note: Add service id without {}. We can find service ID from below api like

curl -X GET http://localhost:8001/service/service-v1

Great, we’ve just set up Kong in few easy steps. In the below section, we are going to test the Kong routing.

Kong Path based routing example


curl http://127.0.0.1/v1 And curl http://127.0.0.1/v2

This is just Kong startup guide. Kong comes up with great power and lots of integration capabilities. Will publish soon blog on Advance Kong with rate limiting and centralized logging. You can connect with me at jogendra.kumar. Thanks.

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

Topics:
kong gateway ,api gateway ,restful api management ,integration ,docker

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}