概括:
我有一个运行 kubectl port-forward 的 docker 容器,将作为 k8s 服务运行的 postgres 服务的端口 (5432) 转发到本地端口 (2223)。在 Dockerfile 中,我暴露了相关端口 2223。然后我通过发布该端口来运行容器 ( -p 2223:2223)
现在,当我尝试通过访问 postgres 时psql -h localhost -p 2223,出现以下错误:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Run Code Online (Sandbox Code Playgroud)
但是,当我docker exec -ti对上述容器执行操作并运行上述 psql 命令时,我能够连接到 postgres。
Dockerfile 命令:
EXPOSE 2223
CMD ["bash", "-c", "kubectl -n namespace_test port-forward service/postgres-11-2 2223:5432"]
Run Code Online (Sandbox Code Playgroud)
Docker 运行命令:
docker run -it --name=k8s-conn-12 -p 2223:2223 my_image_name:latest
Run Code Online (Sandbox Code Playgroud)
docker …