Quick Start Guide Kong API Gateway

Administrator 1
kong gateway

Kong Gateway is an open source, cloud-native API gateway that enables you to manage your APIs and microservices easily. It is built on top of Nginx, a high-performance web server, and provides various features such as traffic control, security, authentication, rate-limiting, and analytics. In this quick start guide, we will go through the steps to install and configure Kong Gateway.

Architecture

The architecture of Kong Gateway consists of several components that work together to provide these features.

Components

  1. Nginx

Kong Gateway is built on top of Nginx, a high-performance web server that is used as a reverse proxy. Nginx handles incoming requests and forwards them to the appropriate microservice based on the configured routing rules.

  1. Kong Core

Kong Core is the heart of Kong Gateway. It is responsible for managing the configuration, plugins, and API traffic. Kong Core is written in Lua and runs inside Nginx as a Lua module.

  1. Kong Admin API

The Kong Admin API is a RESTful API that allows you to manage Kong Gateway configurations, plugins, and services. It provides a unified interface for configuring and managing Kong Gateway.

  1. Datastore

Kong Gateway stores its configuration and state in a datastore. Kong Gateway supports various datastores, including PostgreSQL, Cassandra, and MySQL.

  1. Plugins

Kong Gateway provides a rich set of plugins that allow you to add functionality to your API gateway. Plugins include authentication, rate-limiting, caching, logging, and many more.

  1. Logging and Metrics

Kong Gateway provides logging and metrics functionality that allows you to monitor the performance and health of your API gateway. Kong Gateway supports various logging and metrics backends, including Elasticsearch, InfluxDB, and Prometheus.

Architecture Flow

  1. The client sends a request to the API gateway, which is handled by Nginx.
  2. Nginx forwards the request to Kong Core, which applies the configured plugins and routing rules.
  3. If the request matches a configured service, Kong Core forwards the request to the appropriate microservice.
  4. The response from the microservice is then returned to the client through Nginx.

The Kong Admin API provides a unified interface for managing the configuration, plugins, and services of Kong Gateway. The datastore stores the configuration and state of Kong Gateway, and plugins provide additional functionality such as authentication, rate-limiting, and caching. Logging and metrics are provided by various backends, including Elasticsearch, InfluxDB, and Prometheus.

Overall, the architecture of Kong Gateway is designed to be scalable, flexible, and easy to use.

Installation

Kong Gateway can be installed on various platforms, including Linux, macOS, and Windows. For this guide, we will focus on installing it on Ubuntu Linux.

Prerequisites

Before installing Kong Gateway, you will need the following:

  • Ubuntu Linux 18.04 or later
  • Docker
  • Docker Compose

Installation steps

  1. Install Docker and Docker Compose.sqlCopy codesudo apt-get update sudo apt-get install docker.io docker-compose
  2. Clone the Kong Gateway Docker Compose repository.bashCopy codegit clone https://github.com/Kong/docker-kong.git
  3. Change to the cloned directory.bashCopy codecd docker-kong/compose
  4. Start Kong Gateway.Copy codedocker-compose up -d

Configuration

After installing Kong Gateway, you can configure it by using the Kong Admin API or Kongfig, a command-line tool for managing Kong configurations.

Admin API

The Kong Admin API is a RESTful API that allows you to manage Kong Gateway configurations, plugins, and services. You can access the Kong Admin API by sending HTTP requests to http://localhost:8001.

To add a new service to Kong Gateway, you can use the following curl command:

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=my-service' \
  --data 'url=http://example.com'

This command creates a new service named my-service with the upstream URL http://example.com.

To add a new route to the service, you can use the following curl command:

curl -i -X POST \
  --url http://localhost:8001/services/my-service/routes \
  --data 'hosts[]=example.com'

This command creates a new route that matches requests with the Host header equal to example.com.

Kongfig

Kongfig is a command-line tool for managing Kong configurations. It allows you to define services, routes, plugins, and consumers in a YAML file and then apply the changes to Kong Gateway.

To install Kongfig, you can use the following command:

pip install kongfig

To add a new service using Kongfig, you can create a services.yml file with the following contents:

services:
  - name: my-service
    url: http://example.com

Then, you can apply the changes to Kong Gateway by running the following command:

kongfig apply -f services.yml

This command creates a new service named my-service with the upstream URL http://example.com.

To add a new route to the service, you can create a routes.yml file with the following contents:

routes:
  - name: my-route
    paths:
      - /my-route
    service: my-service

Then, you can apply the changes to Kong Gateway by running the following command:

kongfig apply -f routes.yml

This command creates a new route that matches requests with the path /my-route and forwards them to the my-service service.

Testing

Follow below steps to test newly created Kongo API Gateway

  1. Create a service: Use the Kong Gateway Admin API to create a service. A service is the endpoint that you want to expose through Kong Gateway. For example, you can create a service for a local REST API running on port 3000.
kotlinCopy code$ curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=my_service' \
  --data 'url=http://localhost:3000'

2. Create a route: Use the Kong Gateway Admin API to create a route. A route maps a request from a client to a service. For example, you can create a route that maps requests to the “/api” endpoint to the “my_service” service.

rubyCopy code$ curl -i -X POST \
  --url http://localhost:8001/services/my_service/routes \
  --data 'paths[]=/api'

3. Test the service: Use a tool like curl or Postman to test the service through Kong Gateway. Send a request to the Kong Gateway endpoint that maps to the service and route you just created.

$ curl -i -X GET \
  --url http://localhost:8000/api

This should send a request to the local REST API running on port 3000 through Kong Gateway.

Conclusion

In this quick start guide, we have gone through the steps to install and configure Kong Gateway. Hope this helps 🙂

Tags:

One Reply to “Quick Start Guide Kong API Gateway”

Leave a Reply