Kubernetes:无法从不同的命名空间访问 mongodb 副本集服务

Kubernetes: Cannot access mongodb replicaset service from a different namespace

如果我表达问题的方式有问题,请告诉我,这样我下次可以做得更好或编辑问题。

我做了什么

使用 rancher 与 Amazon EKS 创建集群。

在 'default' 命名空间中部署了一个 nodejs 应用程序。

已使用默认设置从 rancher 应用程序目录安装 MongoDB 复制集。

当我使用 mongodb://mongodb-replicaset:27017/tradeit_system?replicaSet=rs 作为连接字符串时。

我收到错误。

MongoNetworkError: failed to connect to server [mongodb-replicaset-:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb-replicaset mongodb-replicaset:27017]

然后我在 kubernetes documentation 中读到,要访问不同命名空间中的服务,您还需要指定命名空间和服务名称。

所以我这样做 mongodb://mongodb-replicaset.mongodb-replicaset:27017/tradeit_system?replicaSet=rss 作为连接 url 我得到了错误。

MongoError: no primary found in replicaset or invalid replica set name

因此,如果您想要访问它并引用集群域,则必须在主机字符串中包含名称空间,而您没有这样做。

引用自this document

The domain managed by this Service takes the form: $(service name).$(namespace).svc.cluster.local, where “cluster.local” is the cluster domain.

因此在您的情况下,您的 pod DNS 将写为:

mongodb-replicaset.mongodb-replicaset.svc.cluster.local