Search code examples
api-gatewaykong

Kong doesn't load entities when using with PostgreSQL


I want to use Kong as an API Gateway. I am using Kong image.

Here is my compose file:

services:
  api_gateway:
    container_name: api_gateway
    image: kong
    volumes:
      - ./:/app
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong_postgresql
      - KONG_PG_PORT=5432
      - KONG_PG_USER=kong
      - KONG_PG_DB=kong
      - KONG_PG_PASSWORD=password
      - 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, 0.0.0.0:8444 ssl
      - KONG_DECLARATIVE_CONFIG=/app/kong.yml
    ports:
      - 8000:8000
      - 8443:8443
      - 8001:8001
      - 8444:8444
    command: bash -c "while !</dev/tcp/kong_postgresql/5432; do sleep 10; done; kong migrations bootstrap && kong start"
    restart: always

  kong_postgresql:
    image: postgres
    container_name: kong_postgresql
    restart: always
    expose:
      - 5432
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: kong
    ports:
      - 5432:5432




And here is my kong.yml:

_format_version: "3.0"

_transform: true

services:
  - name: customer_service
    url: http://customer:81/

  - name: auth_service
    url: http://auth:84/

routes:
  - name: basket-requests
    service: basket_service
    paths:
      - /basket

  - name: auth-requests
    service: auth_service
    paths:
      - /auth

plugins:
  - name: jwt
    service: basket_service
    enabled: true
    config:
      uri_param_names:
        - jwt
      key_claim_name: kid
      maximum_expiration: 100
      claims_to_verify:
        - exp


The database and Kong starts successfully. But when I make request at http://localhost:8001/services it returns an empty array. Why is this happening? Does not Kong support declarative configuration with using a database? I want to load my entities to postgresql from my kong.yml file.

I tried setting KONG_DATABASE=off and then it is OK. If a make a request at http://localhost:8001/services it returns the services in kong.yml file.


Solution

  • If you want to use both the declarative configuration file and the database, you can use the kong config db_import command to import the configuration from the file into the database.

    try this:

    docker exec -it api_gateway bash
    kong config db_import /app/kong.yml
    

    Here you can find some important notes about db_import command :https://docs.konghq.com/deck/latest/faqs/