我有以下 docker-compose.yml 文件,用 postgresql 运行 kong 。我也在那里进行了迁移,我现在已经删除了。想知道为什么它会抛出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
答案 0 :(得分:0)
您的第一个问题是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