Boilerplate Code for an API Gateway with Kong

I'm setting up an API gateway using Kong and would like a basic boilerplate to start with. I need example configurations for routing, authentication, and some common plugins. What's a good starting point?

1 Answers

āœ“ Best Answer
```html

šŸš€ Kong API Gateway Boilerplate

Here's a basic boilerplate to get you started with Kong API Gateway. This includes configurations for routing, authentication, and a few common plugins. Remember to adjust the configurations to fit your specific needs.

šŸ“ Project Structure


kong-boilerplate/
ā”œā”€ā”€ kong.conf
ā”œā”€ā”€ docker-compose.yml
└── declarative-config.yml

šŸ“œ kong.conf (Kong Configuration)


database: postgres  # or cassandra
pgx_host: kong-db
pgx_port: 5432
pgx_user: kong
pgx_password: your_db_password
admin_api_listen: 0.0.0.0:8001, 0.0.0.0:8444 ssl
plugins: bundled

🐳 docker-compose.yml (Docker Compose Configuration)


version: '3.8'

services:
  kong-db:
    image: postgres:13
    environment:
      POSTGRES_USER: kong
      POSTGRES_DB: kong
      POSTGRES_PASSWORD: your_db_password
    ports:
      - "5432:5432"
    volumes:
      - kong_data:/var/lib/postgresql/data

  kong:
    image: kong:latest
    depends_on:
      - kong-db
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-db
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: your_db_password
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: 0.0.0.0:8000, 0.0.0.0:8443 ssl
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "8444:8444"
    volumes:
      - ./kong.conf:/usr/local/kong/kong.conf
      - ./declarative-config.yml:/usr/local/kong/declarative-config.yml
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 3

volumes:
  kong_data:

āš™ļø declarative-config.yml (Declarative Configuration)


_format_version: "3.0"

services:
- name: example-service
  url: http://example.com
  routes:
  - name: example-route
    paths:
    - /example

plugins:
- name: key-auth
  service: example-service

consumers:
- username: example-user
  plugins:
  - name: key-auth
    config:
      key: example-api-key

šŸ› ļø Setup Instructions

  1. Install Docker and Docker Compose: Ensure you have Docker and Docker Compose installed on your system.
  2. Create the files: Create the kong.conf, docker-compose.yml, and declarative-config.yml files in a directory.
  3. Adjust configurations: Modify the configurations in each file to match your specific requirements (e.g., database passwords, service URLs).
  4. Start Kong: Run docker-compose up -d in the directory containing the files.
  5. Access Kong Admin API: Access the Kong Admin API at http://localhost:8001.

šŸ”Œ Common Plugins Configuration

  • Key Authentication: As shown in declarative-config.yml, the key-auth plugin is configured.
  • Rate Limiting: Add rate limiting to your service or route.

plugins:
- name: rate-limiting
  service: example-service
  config:
    policy: local
    limit: 100
    minute: 1
  • CORS: Enable CORS to handle cross-origin requests.
  • 
    plugins:
    - name: cors
      service: example-service
      config:
        origins: http://example.com
        methods: GET, POST, OPTIONS
        credentials: true
        exposed_headers: Content-Type
    

    āš ļø Important Considerations

    • Security: Always secure your Kong Admin API and database credentials.
    • Customization: Adapt the configurations to match your specific API requirements.
    • Monitoring: Implement monitoring to track the performance of your Kong API Gateway.

    Know the answer? Login to help.