Getting started with Kong Gateway

Overview

In modern microservices architecture, managing APIs efficiently is very important. This is where Kong Gateway comes into the picture.

Kong Gateway is a high-performance open-source API Gateway built on top of Nginx. It helps organizations manage, secure, monitor, and route APIs and microservices efficiently.

Using Kong Gateway, you can:

  • Route API traffic to backend services
  • Apply authentication and authorization
  • Enable rate limiting
  • Monitor API traffic
  • Secure APIs with plugins
  • Manage APIs centrally

Kong is widely used in enterprise integration and cloud-native environments because of its scalability and plugin-based architecture.

Some important components in Kong Gateway include:

ComponentDescription
Proxy LayerRoutes incoming API requests
Admin APIUsed to configure Kong
Kong ManagerWeb UI for managing APIs
PluginsAdd authentication, logging, rate limiting, etc.
DatabaseStores Kong configuration

In this article, we will install Kong Gateway locally using Docker and understand the Docker Compose configuration step by step.

Installing Kong Gateway using Docker

Before starting, make sure the following are installed on your machine:
Create a file named:

docker-compose.yml

Add the following configuration:

version: "3.9"

volumes:
  kong_db_data:

networks:
  kong-ee-net:
    driver: bridge

x-kong-config: &kong-env
  KONG_DATABASE: postgres
  KONG_PG_HOST: kong-ee-database
  KONG_PG_DATABASE: kong
  KONG_PG_USER: kong
  KONG_PG_PASSWORD: kong

services:

  kong-ee-database:
    image: postgres:15
    container_name: kong-ee-database
    restart: unless-stopped
    networks:
      - kong-ee-net
    volumes:
      - kong_db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: kong
      POSTGRES_DB: kong
      POSTGRES_PASSWORD: kong
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U kong"]
      interval: 5s
      timeout: 5s
      retries: 5
    ports:
      - "5433:5432"

  kong-bootstrap:
    image: kong/kong-gateway:3.6
    container_name: kong-bootstrap
    restart: on-failure
    networks:
      - kong-ee-net
    depends_on:
      kong-ee-database:
        condition: service_healthy
    environment:
      <<: *kong-env
      KONG_PASSWORD: handyshake
    command: kong migrations bootstrap

  kong:
    image: kong/kong-gateway:3.6
    container_name: kong
    restart: unless-stopped
    networks:
      - kong-ee-net
    depends_on:
      kong-bootstrap:
        condition: service_completed_successfully
    environment:
      <<: *kong-env
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
      KONG_ADMIN_GUI_LISTEN: 0.0.0.0:8002
      KONG_ADMIN_GUI_URL: http://localhost:8002
      KONG_PASSWORD: handyshake
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "8002:8002"

Understanding the Docker Compose File

Volumes :

volumes:
  kong_db_data:

This creates a persistent Docker volume for PostgreSQL data. Even if the container restarts, the database data remains same.
Networks :

networks:
  kong-ee-net:
    driver: bridge

This creates a custom Docker bridge network so that all Kong-related containers can communicate with each other internally.
Shared Kong Environment Variables :

x-kong-config: &kong-env

This section defines reusable environment variables for Kong services.

VariablePurpose
KONG_DATABASEDatabase type
KONG_PG_HOSTPostgreSQL container hostname
KONG_PG_DATABASEDatabase name
KONG_PG_USERDatabase username
KONG_PG_PASSWORDDatabase password

PostgreSQL Database Container :

kong-ee-database:

This service creates the PostgreSQL database required by Kong Gateway.
Important configurations:

ConfigurationPurpose
image: postgres:15Uses PostgreSQL version 15
volumesStores DB data persistently
healthcheckVerifies DB readiness
portsExposes PostgreSQL on port 5433

Kong Bootstrap Container :

kong-bootstrap:

This container is responsible for initializing the Kong database.

The command:

kong migrations bootstrap

creates all required Kong tables and schema inside PostgreSQL.

This container runs only once during the initial setup and exits successfully after completing migrations.

Main Kong Gateway Container :

kong:

This is the actual Kong Gateway service.

It exposes:

PortPurpose
8000HTTP Proxy
8443HTTPS Proxy
8001Admin API
8002Kong Manager GUI

Running Kong Gateway

Start the containers using:

docker compose up -d

Verify the running containers:

docker ps

You should see:

  • kong
  • kong-ee-database
  • kong-bootstrap

The kong-bootstrap container may show an exited status after successful execution, which is completely normal.

Accessing Kong Gateway

Once the containers are running, you can access:

ServiceURL
Kong Proxyhttp://localhost:8000
Kong Admin APIhttp://localhost:8001
Kong Manager UIhttp://localhost:8002

To verify Kong is working:

curl http://localhost:8001

If everything is configured correctly, Kong will return a JSON response with version and configuration details.

Leave a Comment