小编Cha*_*osh的帖子

双端口转发 kubernetes + docker

概括:

我有一个运行 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 …

port-forwarding psql docker kubernetes kubectl

3
推荐指数
1
解决办法
3847
查看次数

标签 统计

docker ×1

kubectl ×1

kubernetes ×1

port-forwarding ×1

psql ×1