What is Kong?
An API gateway based on Nginx like below picture. It allows programming in (1,2,3,4) location.
+------------------------------+
| |
| +-------+ |
| 1 | | 2 |
+-------------> +-------------->
| | | |
| | route | |
| 4 | | 3 |
<-------------+ <--------------+
| | nginx | |
| +-------+ |
| kong |
+------------------------------+
An Example of Using Kong
The Requirement
I need an API /country
, it can return the deployment location which the service is running in.
For example,
$ curl http://localhost:30080/country
cannot know where it comes from
$ curl http://localhost:30080/country -H "location:US"
it comes from US
It’s meaningless because I need to add a header when calling the API. Let’s see how to use Kong to resolve problems like this.
Installation
Kong installation is very simple. But you need to install Cassandra or PosgreSQL. See more details here.
Verify Installation
$ curl http://localhost:8001
Add a Service
Service - Kong uses it to refer to the upstream APIs and microservices it manages.
$ curl http://localhost:8001/services -d "name=sample" -d "url=http://localhost:30080"
Add a Route for the Service
Route - It specifies how (and if) requests are sent to their Services after they reach Kong. A single Service can have many Routes.
$ curl http://localhost:8001/services/sample/routes -d "hosts[]=service1.com"
Now we can call the API like:
$ curl http://localhost:8000/country -H "host:service1.com" -H "location:US"
it comes from US
Add a Plugin to the Route
Plugin - It allows you to easily add new features to your API or make your API easier to manage.
Now we add an existing plugin named “request transformer”. It can add header to API call based on domain name.
$ curl http://localhost:8001/routes/735b47c7-af4d-455f-846f-f78a71408fbb/plugins -d "name=request-transformer" -d "config.add.headers=location:US"
Now we can call the API like:
$ curl http://localhost:8000/country -H "host:service1.com"
it comes from US
Then add the second route and add a similar plugin to the route.
$ curl http://localhost:8001/services/sample/routes -d "hosts[]=service2.com"
$ curl http://localhost:8001/routes/3ef1cf68-c513-4937-8b88-8a1d5d9d54ab/plugins -d "name=request-transformer" -d "config.add.headers=location:UK"
And add below line to /etc/hosts
.
127.0.0.1 service1.com service2.com
Then we reach the target now.
$ curl http://service1.com:8000/country
it comes from US
$ curl http://service2.com:8000/country
it comes from UK
Benefits
- So many community provided plugins.
- Can write your own plugins.
- Dynamically add/remove service proxy without restarting.