Docker - 检查 postgres 是否准备就绪
Docker - check if postgres is ready
我有一个 Kong API Gateway container and a postgres 容器,我需要在 运行 迁移之前检查 postgres 是否已经启动并从 Kong 容器准备就绪。我正在考虑在我的 Dockerfile 中使用 RUN yum install postgresql -y && yum clean all
并使用 psql
或 pg_isready
将 postgres 客户端实用程序安装到基于官方 Kong 图像的自定义图像中。我创建了一个名为 polling
的 postgres 用户,密码为空,专门用于通过这两个实用程序检查服务器的状态。它们都不起作用。
我尝试从自定义 Kong 映像执行这些命令:
psql。命令 psql -h postgres -U polling -w -c '\l'
失败并出现错误 psql: fe_sendauth: no password supplied
。但是用户没有密码。我究竟做错了什么?完整的 shell 脚本检查服务器是否准备好使用 psql 描述 here.
pg_isready。我不知道如何将此实用程序单独安装到基于官方 Kong 图像的自定义图像中,而该图像又基于 centos:7
图像,postgresql
包不包含 pg_isready
.仅安装了这些实用程序,并且可以在 /usr/bin
中找到它们:pg_config
、pg_dump
、pg_dumpall
、pg_restore
、psql
。如何安装pg_isready
?我不想在 Kong 映像中安装完整的服务器。
我们通过在端口 5432 上进行简单的 TCP 检查来解决这个问题,没有任何 PG 工具。我们直接使用 wait-for-it.sh
,效果很好。 Postgres 在服务器真正准备好服务之前不会打开端口,所以这显然没问题。
示例 Dockerfile:https://github.com/apim-haufe-io/wicked.kong/blob/master/Dockerfile
相应的启动脚本(对于这个特定问题,只有最后一行是有趣的):https://github.com/apim-haufe-io/wicked.kong/blob/master/startup.sh
片段:
wait-for-it.sh -h $KONG_PG_HOST -p 5432 -t 30 -- kong start --run-migrations
我的解决方案是根据官方 kong 图像创建一个新图像并像这样覆盖入口点:
#!/usr/bin/env bash
set -e
# Disabling nginx daemon mode
export KONG_NGINX_DAEMON="off"
# Setting default prefix (override any existing variable)
export KONG_PREFIX="/usr/local/kong"
# Prepare Kong prefix
if [ "" = "/usr/local/openresty/nginx/sbin/nginx" ]; then
kong prepare -p "/usr/local/kong"
fi
#waiting for postgres
until psql --host=$KONG_PG_HOST --username=$POLLING_USER $POLLING_DATABASE -w &>/dev/null
do
echo "Waiting for PostgreSQL..."
sleep 1
done
echo "Postgres is ready, running the migrations..."
kong migrations up
echo "READY TO START UP KONG USING CMD" $@;
exec "$@"
我试过等待它,但在 docker 容器中 运行 它可能是个问题,因为 docker 图像中可能没有安装 nc(有时有甚至不是 ping、telnet、curl ..)。因此,为了检查数据库是否启动和 运行ning,我在 docker 中使用了 HealthCheck 撰写文件,检查 return pg_isready 的值,postgres 数据库的一部分,所以你不需要在 docker 图像中安装任何东西:
version: '2.3'
services:
postgres-db:
image: postgresImage
healthcheck:
test: /usr/bin/pg_isready
interval: 5s
timeout: 10s
retries: 120
ports:
- '5432:5432'
aplication:
image: applicationImage
depends_on:
postgres-db:
condition: service_healthy
这里是 shell 一个使用 PostgreSQL 提供的 pg_isready
工具的衬垫。
外线通话docker:
DOCKER_CONTAINER_NAME="mypgcontainer"
timeout 90s bash -c "until docker exec $DOCKER_CONTAINER_NAME pg_isready ; do sleep 5 ; done"
我有一个 Kong API Gateway container and a postgres 容器,我需要在 运行 迁移之前检查 postgres 是否已经启动并从 Kong 容器准备就绪。我正在考虑在我的 Dockerfile 中使用 RUN yum install postgresql -y && yum clean all
并使用 psql
或 pg_isready
将 postgres 客户端实用程序安装到基于官方 Kong 图像的自定义图像中。我创建了一个名为 polling
的 postgres 用户,密码为空,专门用于通过这两个实用程序检查服务器的状态。它们都不起作用。
我尝试从自定义 Kong 映像执行这些命令:
psql。命令
psql -h postgres -U polling -w -c '\l'
失败并出现错误psql: fe_sendauth: no password supplied
。但是用户没有密码。我究竟做错了什么?完整的 shell 脚本检查服务器是否准备好使用 psql 描述 here.pg_isready。我不知道如何将此实用程序单独安装到基于官方 Kong 图像的自定义图像中,而该图像又基于
centos:7
图像,postgresql
包不包含pg_isready
.仅安装了这些实用程序,并且可以在/usr/bin
中找到它们:pg_config
、pg_dump
、pg_dumpall
、pg_restore
、psql
。如何安装pg_isready
?我不想在 Kong 映像中安装完整的服务器。
我们通过在端口 5432 上进行简单的 TCP 检查来解决这个问题,没有任何 PG 工具。我们直接使用 wait-for-it.sh
,效果很好。 Postgres 在服务器真正准备好服务之前不会打开端口,所以这显然没问题。
示例 Dockerfile:https://github.com/apim-haufe-io/wicked.kong/blob/master/Dockerfile
相应的启动脚本(对于这个特定问题,只有最后一行是有趣的):https://github.com/apim-haufe-io/wicked.kong/blob/master/startup.sh
片段:
wait-for-it.sh -h $KONG_PG_HOST -p 5432 -t 30 -- kong start --run-migrations
我的解决方案是根据官方 kong 图像创建一个新图像并像这样覆盖入口点:
#!/usr/bin/env bash
set -e
# Disabling nginx daemon mode
export KONG_NGINX_DAEMON="off"
# Setting default prefix (override any existing variable)
export KONG_PREFIX="/usr/local/kong"
# Prepare Kong prefix
if [ "" = "/usr/local/openresty/nginx/sbin/nginx" ]; then
kong prepare -p "/usr/local/kong"
fi
#waiting for postgres
until psql --host=$KONG_PG_HOST --username=$POLLING_USER $POLLING_DATABASE -w &>/dev/null
do
echo "Waiting for PostgreSQL..."
sleep 1
done
echo "Postgres is ready, running the migrations..."
kong migrations up
echo "READY TO START UP KONG USING CMD" $@;
exec "$@"
我试过等待它,但在 docker 容器中 运行 它可能是个问题,因为 docker 图像中可能没有安装 nc(有时有甚至不是 ping、telnet、curl ..)。因此,为了检查数据库是否启动和 运行ning,我在 docker 中使用了 HealthCheck 撰写文件,检查 return pg_isready 的值,postgres 数据库的一部分,所以你不需要在 docker 图像中安装任何东西:
version: '2.3'
services:
postgres-db:
image: postgresImage
healthcheck:
test: /usr/bin/pg_isready
interval: 5s
timeout: 10s
retries: 120
ports:
- '5432:5432'
aplication:
image: applicationImage
depends_on:
postgres-db:
condition: service_healthy
这里是 shell 一个使用 PostgreSQL 提供的 pg_isready
工具的衬垫。
外线通话docker:
DOCKER_CONTAINER_NAME="mypgcontainer"
timeout 90s bash -c "until docker exec $DOCKER_CONTAINER_NAME pg_isready ; do sleep 5 ; done"