DEV Community

AnushaPalle
AnushaPalle

Posted on Edited on

What Is an API Gateway? Getting Started with Kong Using Docker

What Is an API Gateway? Getting Started with Kong Using Docker

Meta description: A beginner's introduction to API gateways and Kong, with a working Docker setup and your first request.

What you'll learn

  • What an API gateway is and why REST APIs need one
  • How Kong is structured
  • How to run Kong locally with Docker and check that it works

The problem: too many front doors

Say you've built a few REST services: /products, /orders and /users. Each service must handle:

  • Authentication (who is calling?)
  • Rate limiting (stop abuse)
  • Logging and monitoring
  • HTTPS, CORS, request size limits

If every team re-implements these, you get duplicated code, inconsistent security and a lot of maintenance.

An API gateway solves this. It is a single entry point that sits in front of all your services:

Client  →  Kong (gateway)  →  Service A
                            →  Service B
                            →  Service C
Enter fullscreen mode Exit fullscreen mode

Analogy: think of an airport. Passengers (requests) don't walk straight onto planes (services). They go through check-in, security and boarding. The airport handles the checks once, so the planes don't have to.

Meet Kong

Kong is a popular open-source API gateway built on NGINX. It is fast, and you extend it with plugins (security, traffic control, transformations, logging).

Port Name Who uses it
8000 Proxy Your API clients send traffic here
8001 Admin API You, to configure Kong
8002 Kong Manager You, a web UI for the same configuration

Remember this split. Clients use 8000, administrators use 8001/8002. In production, the admin ports must never be public.

Two ways Kong stores its config

  1. With a database (PostgreSQL): you change config live through the Admin API. This is what we use for learning.
  2. DB-less (declarative): the config is a YAML file loaded at startup. Great for version control and automation.

Run Kong with Docker

Create docker-compose.yml:

services:
  kong-db:
    image: postgres:13
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: kong
      POSTGRES_DB: kong
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "kong"]
      interval: 5s
      retries: 10

  kong-migrations:
    image: kong:3.7
    command: kong migrations bootstrap
    environment: &kong-db
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-db
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: kong
    depends_on:
      kong-db:
        condition: service_healthy

  kong:
    image: kong:3.7
    environment:
      <<: *kong-db
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
      KONG_ADMIN_GUI_URL: http://localhost:8002
    ports:
      - "8000:8000"
      - "8001:8001"
      - "8002:8002"
    depends_on:
      kong-migrations:
        condition: service_completed_successfully
Enter fullscreen mode Exit fullscreen mode

Start it:

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Check that it works

curl -i http://localhost:8001/status
curl -i http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

The first call returns Kong's status. The second returns:

{"message":"no Route matched with those values"}
Enter fullscreen mode Exit fullscreen mode

That error is good news. Kong is running, but you haven't told it where to send traffic yet. We do that in the next post.

Common mistakes

  • Exposing port 8001 publicly. Anyone who reaches it can reconfigure your gateway.
  • Forgetting the migrations step. Kong won't start without its database tables.
  • Confusing 8000 and 8001. 8000 is for clients, 8001 is for you.

Key takeaways

  • An API gateway is one entry point that handles cross-cutting concerns for many services.
  • Kong runs a proxy (8000) and an admin interface (8001, UI on 8002).
  • Docker makes a local Kong setup a single command.

Next: Services, Routes, Consumers and Kong Manager.

Top comments (0)