无法使用来自GCP群集的VPC对等连接到Mongo Atlas

时间:2020-06-09 06:23:43

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

我正在尝试将在GCP Kubernetes引擎集群上运行的Java应用程序与Mongo Atlas集群(M20)连接起来。以前,当我没有打开VPC Peering并使用常规连接字符串时,它运行良好。但是我现在尝试将GPC项目中的default VPC网络用于VPC对等。我遵循了https://docs.atlas.mongodb.com/security-vpc-peering/中的步骤。我选择了192.168.0.0/18的Atlas CIDR(b / c“ Atlas CIDR块必须至少为/ 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"通过Mongo Shell与我的GCP项目中的某些其他VM连接。但是在我的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?

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

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

1) 我必须创建一个新的GCP群集,并且在其中默认情况下启用了VPC本地(启用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)更安全,因为{{1} }实际上为旧样式提供了真实的IP地址,而不是新样式。

nslookup命令有助于:

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

答案 1 :(得分:0)

我猜测您应该使用split horizon设置,或者您的应用程序与副本集配置中使用的主机名/ IP地址之间没有连接。

从Atlas看,Atlas一侧的白名单应反映您的应用程序用于连接的IP。

答案 2 :(得分:0)

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