【问题标题】:Not able to start kong service written in docker-compose无法启动用 docker-compose 编写的 kong 服务
【发布时间】:2018-05-27 12:39:40
【问题描述】:

我有以下 docker-compose.yml 文件来使用 postgresql 运行 kong。我也有迁移,我现在已经删除了。想知道为什么它会抛出 postgresql 的连接问题。

  • postgresql 容器已启动并正在运行。
  • 问题出在孔中

这是我的 docker-compose.yml:

version: '3.2'
networks:
  kong-net:

services:

  kong-database:
    image: postgres:9.5
    container_name: kong-database
    networks:
      - kong-net
    ports:
      - 5435:5435
    environment:
      - POSTGRES_DB=kong
      - POSTGRES_USER=kong
      - POSTGRES_PASSWORD=password

  kong:
    image: kong:latest
    restart: always
    networks:
      - kong-net
    depends_on:
      - kong-database
    ports:
      - 8000:8000
      - 8443:8443
      - 8009:8009
      - 7947:7947
      - 7947:7947/udp
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-database
      - KONG_PG_PASSWORD=password
      - KONG_ADMIN_LISTEN=0.0.0.0:8009

我正在更改 kong 的端口,因为已经有其他一些服务 在 8001 端口运行

这是来自docker-compose logs的错误日志

日志:

Attaching to kong_kong_1, kong-database
kong-database    | The files belonging to this database system will be owned by user "postgres".
kong-database    | This user must also own the server process.
kong-database    | 
kong-database    | The database cluster will be initialized with locale "en_US.utf8".
kong-database    | The default database encoding has accordingly been set to "UTF8".
kong-database    | The default text search configuration will be set to "english".
kong-database    | 
kong-database    | Data page checksums are disabled.
kong-database    | 
kong-database    | fixing permissions on existing directory /var/lib/postgresql/data ... ok
kong-database    | creating subdirectories ... ok
kong-database    | selecting default max_connections ... 100
kong-database    | selecting default shared_buffers ... 128MB
kong-database    | selecting dynamic shared memory implementation ... posix
kong-database    | creating configuration files ... ok
kong-database    | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
kong-database    | initializing pg_authid ... ok
kong-database    | initializing dependencies ... ok
kong-database    | creating system views ... ok
kong-database    | loading system objects' descriptions ... ok
kong-database    | creating collations ... ok
kong-database    | creating conversions ... ok
kong-database    | creating dictionaries ... ok
kong-database    | setting privileges on built-in objects ... ok
kong-database    | creating information schema ... ok
kong-database    | loading PL/pgSQL server-side language ... ok
kong-database    | vacuuming database template1 ... ok
kong-database    | copying template1 to template0 ... ok
kong-database    | copying template1 to postgres ... ok
kong_1           | prefix directory /usr/local/kong not found, trying to create it
kong_1           | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:169: [postgres error] could not retrieve server_version: connection refused
kong_1           | stack traceback:
kong_1           |  [C]: in function 'error'
kong_1           |  /usr/local/share/lua/5.1/kong/init.lua:169: in function 'init'
kong_1           |  init_by_lua:3: in main chunk
kong_1           | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:169: [postgres error] could not retrieve server_version: connection refused
kong_1           | stack traceback:
kong_1           |  [C]: in function 'error'
kong_1           |  /usr/local/share/lua/5.1/kong/init.lua:169: in function 'init'
kong_1           |  init_by_lua:3: in main chunk

【问题讨论】:

    标签: postgresql docker docker-compose kong


    【解决方案1】:

    您的第一个问题是kong 容器在kong-database 容器完成其初始化过程之前启动。这是“连接被拒绝”错误的来源。您可以通过首先启动数据库来验证这一点:

    docker-compose up kong-database
    

    然后等到你看到:

    PostgreSQL init process complete; ready for start up.
    

    然后在另一个窗口中,启动kong 容器:

    docker-compose up kong
    

    你会看到一个新的错误:

    kong_1           | nginx: [warn] [lua] log.lua:63: log(): postgres
      database 'kong' is missing migration: (response-transformer)
      2016-05-04-160000_resp_trans_schema_changes
    kong_1           | nginx: [error] init_by_lua error:
      /usr/local/share/lua/5.1/kong/init.lua:172: [postgres error] the
      current database schema does not match this version of Kong.
      Please run `kong migrations up` to update/initialize the database
      schema. Be aware that Kong migrations should only run from a
      single node, and that nodes running migrations concurrently will
      conflict with each other and might corrupt your database schema!
    

    这表示kong 成功连接到postgres,但是 看起来您需要运行某种数据库模式 在它工作之前进行初始化。

    这种启动依赖的典型解决方案是实现 数据库客户端启动中的等待循环,直到 数据库可用。比如:

    while ! psql -h kong-database -U kong -c 'select 1'; do
      sleep 1
    done
    

    【讨论】:

    • 但是 depends_on 的目的是什么。我想我已经习惯了,只有在数据库准备好之后,kong才必须启动。
    • depends_on 表示kong-database 容器是在kong 容器之前创建的,但是Docker 无法知道Postgres 何时真正准备好为请求提供服务。
    • 感谢您的回复,我找到了一些解决方案,例如将depends_on 与condition 一起使用,但这只能在docker version2 中使用。这不符合我的需要,因为我的最终目标是使用docker stack 进行部署。我尝试使用堆栈复制它以进行部署,并且 kong 服务未运行。 task: non-zero exit (1)
    • @TaraPrasadGurung 这里是 docker-compose 版本 3 的文档。docs.docker.com/compose/compose-file/#depends_on。基本上,您需要编写自己的 shell 脚本,然后等待数据库启动并运行,然后执行 kong。
    猜你喜欢
    • 2020-06-30
    • 2018-12-16
    • 1970-01-01
    • 2021-11-21
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    相关资源
    最近更新 更多