Kong gateway is a cloud-native, platform-agnostic, scalable API Gateway distinguished for its high performance and extensibility via plugins.
This repository is an playground to explore kong gateway features.
Kong Gateway administrators work with an object model to define their desired traffic management policies. Two important objects in that model are services and routes. Services and routes are configured in a coordinated manner to define the routing path that requests and responses will take through the system.
In Kong Gateway, a service is an abstraction of an existing upstream application. Services can store collections of objects like plugin configurations, and policies, and they can be associated with routes.
When defining a service, the administrator provides a name and the upstream application connection information. The connection details can be provided in the url field as a single string, or by providing individual values for protocol, host, port, and path individually.
Services have a one-to-many relationship with upstream applications, which allows administrators to create sophisticated traffic management behaviors.
A route is a path to a resource within an upstream application. Routes are added to services to allow access to the underlying application. In Kong Gateway, routes typically map to endpoints that are exposed through the Kong Gateway application. Routes can also define rules that match requests to associated services. Because of this, one route can reference multiple endpoints. A basic route should have a name, path or paths, and reference an existing service.
You can also configure routes with:
- Protocols: The protocol used to communicate with the upstream application.
- Hosts: Lists of domains that match a route
- Methods: HTTP methods that match a route
- Headers: Lists of values that are expected in the header of a request
- Redirect status codes: HTTPS status codes
- Tags: Optional set of strings to group routes with
See Routes for a description of how Kong Gateway routes requests.
The following tutorial walks through managing and testing services and routes using the Kong Gateway Admin API. Kong Gateway also offers other options for configuration management including Kong Konnect and decK.
To start the kong gateway using docker compose:
make kongStart kong gateway with postgres:
make kong-postgresThis session follow the get started tutorial.
curl --head localhost:8001If Kong Gateway is running properly, it will respond with a 200 HTTP code, similar to the following:
HTTP/1.1 200 OK
Date: Mon, 22 Aug 2022 19:25:49 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Content-Length: 11063
X-Kong-Admin-Latency: 6
Server: kong/3.3.0The root route of the Admin API provides important information about the running Kong Gateway including networking, security, and plugin information. The full configuration is provided in the .configuration key of the returned JSON document.
curl -s localhost:8001 | jq '.configuration'curl -i -s -X POST http://localhost:8001/services \
--data name=example_service \
--data url='http://mockbin.org'curl -X GET http://localhost:8001/services/example_servicecurl --request PATCH \
--url localhost:8001/services/example_service \
--data retries=6curl -X GET http://localhost:8001/servicescurl -i -X POST http://localhost:8001/services/example_service/routes \
--data 'paths[]=/mock' \
--data name=example_routecurl -X GET http://localhost:8001/services/example_service/routes/example_routecurl --request PATCH \
--url localhost:8001/services/example_service/routes/example_route \
--data tags="tutorial"curl http://localhost:8001/routescurl -X GET http://localhost:8000/mock/requestscurl -i -X POST http://localhost:8001/plugins \
--data name=rate-limiting \
--data config.minute=5 \
--data config.policy=localValidate rate limiting:
for _ in {1..6}; do curl -s -i localhost:8000/mock/request; echo; sleep 1; donecurl -X POST http://localhost:8001/services/example_service/plugins \
--data "name=rate-limiting" \
--data config.minute=5 \
--data config.policy=localcurl -X POST http://localhost:8001/routes/example_route/plugins \
--data "name=rate-limiting" \
--data config.minute=5 \
--data config.policy=local- Create a consumer
curl -X POST http://localhost:8001/consumers/ \
--data username=jsmith- Enable rate limiting for the consumer
curl -X POST http://localhost:8001/plugins \
--data "name=rate-limiting" \
--data "consumer.username=jsmith" \
--data "config.second=5"https://docs.konghq.com/gateway/3.3.x/get-started/proxy-caching/
- Enable proxy caching
curl -i -X POST http://localhost:8001/plugins \
--data "name=proxy-cache" \
--data "config.request_method=GET" \
--data "config.response_code=200" \
--data "config.content_type=application/json; charset=utf-8" \
--data "config.cache_ttl=30" \
--data "config.strategy=memory"- Validate
curl -i -s -X GET http://localhost:8000/mock/requests | grep X-Cachecurl -X POST http://localhost:8001/services/example_service/plugins \
--data "name=proxy-cache" \
--data "config.request_method=GET" \
--data "config.response_code=200" \
--data "config.content_type=application/json; charset=utf-8" \
--data "config.cache_ttl=30" \
--data "config.strategy=memory"curl -X POST http://localhost:8001/routes/example_route/plugins \
--data "name=proxy-cache" \
--data "config.request_method=GET" \
--data "config.response_code=200" \
--data "config.content_type=application/json; charset=utf-8" \
--data "config.cache_ttl=30" \
--data "config.strategy=memory"- Create a consumer
curl -X POST http://localhost:8001/consumers/ \
--data username=sasha- Enable cache for the consumer
curl -X POST http://localhost:8001/consumers/sasha/plugins \
--data "name=proxy-cache" \
--data "config.request_method=GET" \
--data "config.response_code=200" \
--data "config.content_type=application/json; charset=utf-8" \
--data "config.cache_ttl=30" \
--data "config.strategy=memory"- Create a new consumer
curl -i -X POST http://localhost:8001/consumers/ \
--data username=luka- Assign the consumer a key
curl -i -X POST http://localhost:8001/consumers/luka/key-auth \
--data key=top-secret-key
- Enable key authentication
curl -X POST http://localhost:8001/plugins/ \
--data "name=key-auth" \
--data "config.key_names=apikey"- Send an unauthenticated request
curl -i http://localhost:8000/mock/request- Send the wrong key
curl -i http://localhost:8000/mock/request \
-H 'apikey:bad-key'- Send a valid request
curl -i http://localhost:8000/mock/request \
-H 'apikey:top-secret-key'