无法从在AWS EKS集群节点中运行的Docker容器连接到EC2上的PostgreSQL服务器。

huangapple go评论66阅读模式
英文:

Cannot Connect to an EC2 PostgreSQL Server from a Docker Container running in an AWS EKS Cluster Node

问题

我们有一个在AWS EC2实例上运行的PostgreSQL数据库。我们的目标是从运行在AWS EKS集群内的EC2节点上的Docker容器连接到此数据库。我们使用Python(pyodbc)来连接数据库。

我们遇到了这个错误:

2023-02-23T15:03:49.532858159Z     return pyodbc.connect(connection_str)
2023-02-23T15:03:49.532902079Z pyodbc.OperationalError: ('08001', '[08001] could not connect to server: Network is unreachable\n\tIs the server running on host "****" and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Network is unreachable\n\tIs the server running on host "5432" (0.0.21.56) and accepting\n\tTCP/IP connections on port 5432?\n (101) (SQLDriverConnect)')

我们通过Putty访问了EKS EC2节点。当我们运行相同的Python代码来连接数据库时,连接成功。

我们认为Docker容器存在连接问题。但我们无法找出问题所在。

这是在构建Docker镜像时使用的docker-compose.yaml文件。

version: "3.7"

services:
  dev:
    image: dbimage
    container_name: dbimage
    build:
      context: .
      target: dbimage
      args:
         - BUILDKIT_INLINE_CACHE=1
         - BUILD_DATE
         - GITREF
    networks:
      - cloud-network
    volumes:
        - .:/app
networks:
  cloud-network:
    driver: bridge
英文:

We have a PostgreSQL database running into an AWS EC2 instance. Our target is connecting to this database from a Docker container running into an EC2 node which is running inside an AWS EKS cluster. We use Python (pyodbc) to connect to the database.

We get this error:

2023-02-23T15:03:49.532858159Z     return pyodbc.connect(connection_str)
2023-02-23T15:03:49.532902079Z pyodbc.OperationalError: ('08001', '[08001] could not connect to server: Network is unreachable\n\tIs the server running on host "****" and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Network is unreachable\n\tIs the server running on host "5432" (0.0.21.56) and accepting\n\tTCP/IP connections on port 5432?\n (101) (SQLDriverConnect)')

We accessed the EKS EC2 node using Putty. When we run the same Python code for connecting to the database, the connection is successful.

We believe the Docker container has a connectivity issue. But we cannot figure out the issue.

This is the docker-compose.yaml file used when building the Docker image.

version: "3.7"

services:
  dev:
    image: dbimage
    container_name: dbimage
    build:
      context: .
      target: dbimage
      args:
         - BUILDKIT_INLINE_CACHE=1
         - BUILD_DATE
         - GITREF
    networks:
      - cloud-network
    volumes:
        - .:/app
networks:
  cloud-network:
    driver: bridge

答案1

得分: 0

这可以通过在docker run命令中添加--network host选项来解决。

英文:

This is solved by adding the --network host option in the docker run command.

huangapple
  • 本文由 发表于 2023年2月23日 23:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定