【问题标题】:Cannot connect to Mongo Atlas using VPC peering from GCP cluster无法使用 GCP 集群中的 VPC 对等连接到 Mongo Atlas
【发布时间】:2020-06-09 06:23:43
【问题描述】:

我正在尝试将在 GCP Kubernetes 引擎集群上运行的 Java 应用程序与 Mongo Atlas 集群 (M20) 连接起来。以前,当我没有打开 VPC 对等互连并且我使用的是常规连接字符串时,它运行良好。但我现在正在尝试使用 VPC 对等互连,在我的 GCP 项目中使用 default VPC 网络。我按照https://docs.atlas.mongodb.com/security-vpc-peering/ 中的步骤操作。我选择了192.168.0.0/18的Atlas CIDR(b/c“The Atlas CIDR block must be at least a /18”),将GCP项目和Atlas集群链接后,将10.128.0.0/9加入IP白名单对于 Atlas 集群(b/c 它说这是 GCP 项目中 auto 的默认范围)。

我实际上可以通过 mongo "mongodb+srv://<cluster_name>-pri.crum0.gcp.mongodb.net/itls" 从我的 GCP 项目中的其他一些虚拟机通过 Mongo shell 进行连接。但是在我的 GCP 集群中的 pod 上运行的应用程序无法连接。我在 Java 应用程序中看到的确切错误是

Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@a07fbd8. Client view of cluster state is {type=REPLICA_SET, servers=[{address=<cluster_name>-shard-00-00-pri.crum0.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=<cluster_name>-shard-00-01-pri.crum0.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=<cluster_new>-shard-00-02-pri.crum0.gcp.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]

可能的问题:

1) is it possible to connect from a GCP cluster at all (or perhaps, why is this cluster somehow not part of default VPC network)?
2) is there something wrong in the Atlas CIDR range or my IP whitelist range?

任何帮助将不胜感激。

【问题讨论】:

  • 您能否在正在运行的 pod 中进行基本检查:找出您的 Java 应用程序 pod 名称 kubectl get pods,然后找到 kubectl exec java_pod_name ping &lt;cluster_name&gt;-pri.crum0.gcp.mongodb.net
  • 关于使用 mongo shell 访问的其他 VM。它使用的是什么IP?另外,你能运行一个 linux pod,比如 ubuntu,登录进去,下载 mongo shell 并检查你是否可以从那里连接?这样我们就可以检查它是java应用还是集群本身。
  • 如果您需要该命令,则:kubectl run -it --rm --generator=run-pod/v1 ubuntu --image=ubuntu -- /bin/bash 会将您登录到 pod 内的 shell,以便您可以 apt-get install 并运行 ping 和 mongo shell 命令。
  • 在 VPC 网络对等设置中查看导出的路由,并在全面白名单生效时检查哪个 IP 地址连接到 MongoDB。更多详情developer.mongodb.com/community/forums/t/…

标签: mongodb google-cloud-platform mongodb-atlas vpc google-vpc


【解决方案1】:

我最终进行了 2 处更改以使其正常工作。第一次更改是我缺少的明确要求。尚不确定是否绝对有必要进行第二次更改。

1) 我必须创建一个新的 GCP 集群,并且在其中默认启用 VPC-native(启用 IP 别名)。在我的旧集群中,此设置被禁用,我无法为正在运行的集群更改它。肯定需要打开此设置才能解决问题。

2) 虽然我使用的是 Mongo Java 驱动程序 3.11.1,但我确实使用了几个 nslookup 命令,并认为使用旧驱动程序样式的连接 URI 字符串(即 mongodb://&lt;username&gt;:&lt;password&gt;@&lt;cluster_name&gt;-shard-00-00-pri.crum0.gcp.mongodb.net:27017,&lt;cluster_name&gt;-shard-00-01-pri.crum0.gcp.mongodb.net:27017,&lt;cluster_name&gt;-shard-00-02-pri.crum0.gcp.mongodb.net:27017/itls?ssl=true&amp;replicaSet=&lt;cluster_name&gt;-shard-0&amp;authSource=admin&amp;retryWrites=true&amp;w=majority)更安全,因为 nslookup 实际上给出了真实的 IP 地址适合旧风格,但不适合新风格。

nslookup 有帮助的命令:

>> nslookup <cluster_name>-shard-00-00-pri.crum0.gcp.mongodb.net
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   <cluster_name>-shard-00-00-pri.crum0.gcp.mongodb.net
Address: 192.168.248.2
>> nslookup <cluster_name>-pri.crum0.gcp.mongodb.net
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
*** Can't find <cluster_name>-pri.crum0.gcp.mongodb.net: No answer

【讨论】:

  • 启用 VPC-native 对我来说是关键。感谢您的回答
【解决方案2】:

我猜你要么应该使用split horizon 设置,要么你的应用程序没有连接到副本集配置中使用的主机名/IP 地址。

Atlas 端的白名单应反映您的应用程序用于连接的 IP,如 Atlas 所见。

【讨论】:

    【解决方案3】:

    除了上面user1145925 的答案之外,我还必须在Mongo Atlas 上将GKE 的Pod address range 列入白名单。

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 2021-08-18
      • 2015-11-21
      相关资源
      最近更新 更多