GKE 无法访问 MongoDB Atlas
GKE not able to reach MongoDB Atlas
我在尝试将容器化应用程序部署到 GKE 时遇到问题。它无法访问我的 MongoDB Atlas 集群。 运行 Docker 容器在本地不会产生任何问题并且运行完美。我绝不是 Docker 或 Kubernetes 方面的专家,但我假设它与 DNS 名称解析有关。
我遵循了本教程 - Deploying a containerized web application,另外在 MongoDB Atlas 控制台中将 LoadBalancer 的外部 IP 添加到我的 'Network Access' IP 白名单并使用端口映射 443 -> 8443 因为我使用的是 HTTPS。
只有我的应用在失败前能够生成的日志:
(mongodb): 2020/05/30 15:07:39 logger.go:96: 2020-05-30T15:07:39Z
[error] Failed to connect to mongodb. Check if mongo is running...
(mongodb): 2020/05/30 15:07:39 logger.go:132: 2020-05-30T15:07:39Z
[fatal] server selection error: server selection timeout, current
topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: biomas-
cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown,
State: Connected, Average RTT: 0, Last error: connection() :
connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-180]) incomplete read of message
header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-181]) incomplete read of message header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-179]) incomplete read of message header: EOF }, ] }
如果有一个简单的解决方法,那将是首选,因为应用程序仍处于开发阶段,所以我只需要一个使用上述技术的基本工作应用程序。
完整的工作流程:
Android App -> Golang API 运行 on Docker -> MongoDB Atlas
谢谢
正如@Marc 所说,您的流量是通过工作节点的 EXTERNAL-IP 而不是您的负载均衡器发出的。
要查找节点 EXTERNAL-IP IP,请使用:
kubectl get nodes -owide
更准确地说,仅输出 IP 使用(取自 kubectl Cheat Sheet):
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
接下来将这些 IP 列入白名单,你应该会很好,但请记住,在 Kubernetes 升级或集群扩展后,IP 可能会发生变化,因此我建议使用 Cloud NAT 始终为你的传出流量使用相同的 IP .
我在尝试将容器化应用程序部署到 GKE 时遇到问题。它无法访问我的 MongoDB Atlas 集群。 运行 Docker 容器在本地不会产生任何问题并且运行完美。我绝不是 Docker 或 Kubernetes 方面的专家,但我假设它与 DNS 名称解析有关。
我遵循了本教程 - Deploying a containerized web application,另外在 MongoDB Atlas 控制台中将 LoadBalancer 的外部 IP 添加到我的 'Network Access' IP 白名单并使用端口映射 443 -> 8443 因为我使用的是 HTTPS。
只有我的应用在失败前能够生成的日志:
(mongodb): 2020/05/30 15:07:39 logger.go:96: 2020-05-30T15:07:39Z
[error] Failed to connect to mongodb. Check if mongo is running...
(mongodb): 2020/05/30 15:07:39 logger.go:132: 2020-05-30T15:07:39Z
[fatal] server selection error: server selection timeout, current
topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: biomas-
cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown,
State: Connected, Average RTT: 0, Last error: connection() :
connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-180]) incomplete read of message
header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-181]) incomplete read of message header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-179]) incomplete read of message header: EOF }, ] }
如果有一个简单的解决方法,那将是首选,因为应用程序仍处于开发阶段,所以我只需要一个使用上述技术的基本工作应用程序。
完整的工作流程:
Android App -> Golang API 运行 on Docker -> MongoDB Atlas
谢谢
正如@Marc 所说,您的流量是通过工作节点的 EXTERNAL-IP 而不是您的负载均衡器发出的。
要查找节点 EXTERNAL-IP IP,请使用:
kubectl get nodes -owide
更准确地说,仅输出 IP 使用(取自 kubectl Cheat Sheet):
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
接下来将这些 IP 列入白名单,你应该会很好,但请记住,在 Kubernetes 升级或集群扩展后,IP 可能会发生变化,因此我建议使用 Cloud NAT 始终为你的传出流量使用相同的 IP .