Kong Introduction
What is Kong?
Kong is an open-source API Gateway platform built on top of NGINX, designed to secure, manage and orchestrate API traffic. As a lightweight, fast, and flexible API gateway, Kong sits in front of your APIs and microservices, routing client requests to appropriate backend services while providing crucial functionalities like authentication, rate limiting, and analytics.
Think of Kong as a smart traffic controller for your APIs - it receives all incoming requests, applies necessary policies and transformations, and directs them to the right destination.
Why Use Kong?
In today's world of distributed systems and microservices, managing API communication becomes increasingly complex. Here's why Kong has become a popular solution:
- Performance: Built on NGINX, Kong delivers high throughput with minimal latency
- Scalability: Easily scales horizontally to handle growing API traffic
- Extensibility: Supports plugins for adding functionalities without modifying your APIs
- Cloud-Native: Designed to work seamlessly in containerized environments (Docker, Kubernetes)
- Platform-Agnostic: Works with any infrastructure or deployment strategy
Kong Architecture
Kong follows a modular architecture with two main components:
- Kong Gateway: The core proxy that processes API requests and responses
- Kong Admin API: RESTful interface for configuring and managing Kong
Here's a visual representation of how Kong fits into your API ecosystem:
Core Concepts
Services
A Service in Kong represents a backend API or microservice that Kong proxies traffic to. It contains essential information about your backend service, such as its URL.
# Creating a service via Kong Admin API
curl -i -X POST http://localhost:8001/services \
--data name=example-service \
--data url=http://example.com/api
Response:
{
"id": "a5fb8d9e-9fdb-4a9d-a3c1-9c42f7fb1b3d",
"name": "example-service",
"protocol": "http",
"host": "example.com",
"port": 80,
"path": "/api",
"created_at": 1627984763,
"updated_at": 1627984763
}
Routes
Routes determine how requests are sent to Services. Each Route is associated with a Service and specifies rules for matching client requests (like paths, hosts, methods).
# Creating a route for our service
curl -i -X POST http://localhost:8001/services/example-service/routes \
--data 'paths[]=/users' \
--data name=users-route
Response:
{
"id": "d35165e2-d03e-461a-bdeb-dad0a112abfe",
"name": "users-route",
"service": { "id": "a5fb8d9e-9fdb-4a9d-a3c1-9c42f7fb1b3d" },
"paths": ["/users"],
"created_at": 1627984890,
"updated_at": 1627984890
}
With this configuration, any request to http://kong-host/users
will be forwarded to http://example.com/api/users
.