无法从GCP集群使用VPC对等连接到Mongo Atlas。

问题描述 投票:0回答:1

我正在尝试将一个运行在GCP Kubernetes引擎集群上的Java应用,与Mongo Atlas集群(M20)进行连接。之前,当我没有开启VPC Peering,使用常规连接字符串时,它工作得很好。但我现在尝试使用VPC Peering,并使用了 default VPC网络在我的GCP项目中。我按照以下步骤进行 https:/docs.atlas.mongodb.comsecurity-vpc-peering。. 我选择了Atlas CIDR为192.168.0.018(bc "Atlas CIDR块必须至少是18"),在连接GCP项目和Atlas集群后,将10.128.0.09添加到Atlas集群的IP白名单中(bc说这是GCP项目中自动的默认范围)。

其实我是可以通过Mongo shell连接的,通过 mongo "mongodb+srv://<cluster_name>-pri.crum0.gcp.mongodb.net/itls",从我的GCP项目中的其他虚拟机上。但是在我的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?

希望得到任何帮助。

mongodb google-cloud-platform mongodb-atlas vpc google-vpc
1个回答
1
投票

最后我做了2个改动,才使它正常工作。第一个改变是我缺少的一个明确要求。还不确定第二项改动是否绝对必要。

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

2)虽然我使用的是Mongo Java驱动3.11.1,但我还是在我的群集上做了以下设置 nslookup 命令,并认为使用老式驱动程序式的连接URI字符串(即 mongodb://<username>:<password>@<cluster_name>-shard-00-00-pri.crum0.gcp.mongodb.net:27017,<cluster_name>-shard-00-01-pri.crum0.gcp.mongodb.net:27017,<cluster_name>-shard-00-02-pri.crum0.gcp.mongodb.net:27017/itls?ssl=true&replicaSet=<cluster_name>-shard-0&authSource=admin&retryWrites=true&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

0
投票

我猜想你应该是用 裂缝 设置,或者您的应用程序没有连接到副本集配置中使用的主机名IP地址。

Atlas 端的白名单应该反映出您的应用程序用于连接的 IP,正如从 Atlas 中看到的那样。

© www.soinside.com 2019 - 2024. All rights reserved.