Kong:docker-compose [PostgreSQL错误]无法检索PostgreSQL server_version_num:未提供主机或服务,或未知

2024-01-08

我正在尝试学习如何使用Kong https://konghq.com/对于我的 API 服务器,但遇到了错误:

kong_1           | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:388: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: host or service not provided, or not known

我的 docker-compose.yaml 如下:

version: "3"

networks:
    kong-net:
        driver: bridge

services:
    #  Create a service named db.
    kong-postgres:
        #   Use the Docker Image postgres. This will pull the newest release.
        image: "postgres"
        #   Give the container a name. You can changes to something else.
        container_name: "kong-postgres"
        #   Setup the username, password, and database name. You can changes these values.
        environment:
            - POSTGRES_USER=kong
            - POSTGRES_PASSWORD=kong
            - POSTGRES_DB=kong
        #   Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
        ports:
            - "5432:5432"
        restart: on-failure
        #   Set a volume some that database is not lost after shutting down the container.
        #   I used the name postgres-data but you can changed it to something else.
        volumes:
            - ./postgres-data:/var/lib/postgresql/data

    kong:
        image: "kong:latest"
        command: "kong migrations bootstrap"
        depends_on:
          - kong-postgres
        environment:
          KONG_ADMIN_LISTEN: '0.0.0.0:8001,0.0.0.0:8444 ssl'
          KONG_DATABASE: postgres
          KONG_PG_HOST: kong-postgres
          KONG_PG_DATABASE: kong
          KONG_PG_PASSWORD: kong
          KONG_PG_USER: kong
        networks:
          - kong-net
        ports:
          - "8000:8000/tcp"
          - "8001:8001/tcp"
          - "8443:8443/tcp"
          - "8444:8444/tcp"
        healthcheck:
          test: ["CMD", "kong", "health"]
          interval: 10s
          timeout: 10s
          retries: 10
        restart: on-failure

还尝试通过 2 个步骤运行它:

  1. docker-compose up kong-postgres, 没关系:
    $ docker-compose up kong-postgres       
    Starting kong-postgres ... done
    Attaching to kong-postgres
    kong-postgres    | 
    kong-postgres    | PostgreSQL Database directory appears to contain a database; Skipping initialization
    kong-postgres    | 
    kong-postgres    | 2019-11-20 08:22:37.057 UTC [1] LOG:  starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
    kong-postgres    | 2019-11-20 08:22:37.057 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    kong-postgres    | 2019-11-20 08:22:37.057 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    kong-postgres    | 2019-11-20 08:22:37.060 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    kong-postgres    | 2019-11-20 08:22:37.128 UTC [25] LOG:  database system was shut down at 2019-11-20 08:08:28 UTC
    kong-postgres    | 2019-11-20 08:22:37.176 UTC [1] LOG:  database system is ready to accept connections

数据库可以通过以下方式连接psql -h localhost -p 5432 -U kong -d kong:

$ psql -h localhost -p 5432 -U kong -d kong 
Password for user kong: 
psql (11.5, server 12.1 (Debian 12.1-1.pgdg100+1))
WARNING: psql major version 11, server major version 12.
         Some psql features might not work.
Type "help" for help.

kong=# \q
  1. docker-compose up kong失败:
    $ docker-compose up kong
    kong-postgres is up-to-date
    Recreating kong_kong_1 ... done
    Attaching to kong_kong_1
    kong_1           | Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: host or service not provided, or not known
    kong_1           | 
    kong_1           |   Run with --v (verbose) or --vv (debug) for more details

附注:官方 Docker Compose 模板 https://github.com/Kong/docker-kong/tree/master/compose也失败了:

kong-migrations-up_1  | Error: Cannot run migrations: database needs bootstrapping; run 'kong migrations bootstrap'
kong-migrations-up_1  | 
kong-migrations-up_1  |   Run with --v (verbose) or --vv (debug) for more details

version: "3.7"

volumes:
  kong_data: {}

networks:
 kong-net:

services:

  #######################################
  # Postgres: The database used by Kong
  #######################################
  kong-database:
    image: postgres:9.6
    container_name: kong-postgres
    restart: on-failure
    networks:
      - kong-net
    volumes:
      - kong_data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: kong
      POSTGRES_PASSWORD: ${KONG_PG_PASSWORD:-kong}
      POSTGRES_DB: kong
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "kong"]
      interval: 30s
      timeout: 30s
      retries: 3

  #######################################
  # Kong database migration
  #######################################
  kong-migration:
    image: ${KONG_DOCKER_TAG:-kong:latest}
    command: kong migrations bootstrap
    networks:
      - kong-net
    restart: on-failure
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_DATABASE: kong
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: ${KONG_PG_PASSWORD:-kong}
    depends_on:
      - kong-database

  #######################################
  # Kong: The API Gateway
  #######################################
  kong:
    image: ${KONG_DOCKER_TAG:-kong:latest}
    restart: on-failure
    networks:
      - kong-net
    environment:
      KONG_DATABASE: postgres
      KONG_PG_HOST: kong-database
      KONG_PG_DATABASE: kong
      KONG_PG_USER: kong
      KONG_PG_PASSWORD: ${KONG_PG_PASSWORD:-kong}
      KONG_PROXY_LISTEN: 0.0.0.0:8000
      KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443
      KONG_ADMIN_LISTEN: 0.0.0.0:8001
    depends_on:
      - kong-database
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 10
    ports:
      - "8000:8000"
      - "8001:8001"
      - "8443:8443"
      - "8444:8444"

  #######################################
  # Konga database prepare
  #######################################
  konga-prepare:
    image: pantsel/konga:latest
    command: "-c prepare -a postgres -u postgresql://kong:${KONG_PG_PASSWORD:-kong}@kong-database:5432/konga"
    networks:
      - kong-net
    restart: on-failure
    depends_on:
      - kong-database

  #######################################
  # Konga: Kong GUI
  #######################################
  konga:
    image: pantsel/konga:latest
    restart: always
    networks:
        - kong-net   
    environment:
      DB_ADAPTER: postgres
      DB_URI: postgresql://kong:${KONG_PG_PASSWORD:-kong}@kong-database:5432/konga
      NODE_ENV: production
    depends_on:
      - kong-database
    ports:
      - "1337:1337"

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Kong:docker-compose [PostgreSQL错误]无法检索PostgreSQL server_version_num:未提供主机或服务,或未知 的相关文章

随机推荐