连接到不同名称空间中的Kubernetes mongo db [英] Connect to Kubernetes mongo db in different namespace

查看:52
本文介绍了连接到不同名称空间中的Kubernetes mongo db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能指出如何使用mongo客户端(使用命令行客户端)或具有连接字符串的.net核心程序连接到mongo数据库实例?

Can anyone point out how to connect to the mongo db instance using mongo client using either command line client or from .net core programs with connection strings?

我们已经在digitalocean中创建了一个带有名称空间的示例集群,比如 mongodatabase .

We have created a sample cluster in digitalocean with a namespace, let's say mongodatabase.

我们安装了具有3个副本的mongo statefulset.我们能够使用以下命令成功连接 kubectl --kubeconfig = configfile.yaml -n mongodatabase exec -ti mongo-0 mongo 但是,当我们使用以下格式的Pod名称从其他命名空间或默认命名空间进行连接时,它将无法正常工作.

We installed the mongo statefulset with 3 replicas. We are able to successfully connect with the below command kubectl --kubeconfig=configfile.yaml -n mongodatabase exec -ti mongo-0 mongo But when we connect from a different namespace or from default namespace with the pod names in the below format, it doesn't work.

 kubectl --kubeconfig=configfile.yaml  exec -ti mongo-0.mongo.mongodatabase.cluster.svc.local mongo

其中 mongo-0.mongo.mongodatabase.cluster.svc.local 位于 pod-0.service_name.namespace.cluster.svc.local 中(也尝试过pod -0.statfulset_name.namespace.cluster.svc.local和pod-0.service_name.statefulsetname.namespace.cluster.svc.local)等,

where mongo-0.mongo.mongodatabase.cluster.svc.local is in pod-0.service_name.namespace.cluster.svc.local (also tried pod-0.statfulset_name.namespace.cluster.svc.local and pod-0.service_name.statefulsetname.namespace.cluster.svc.local) etc.,

在通过命令行与mongo客户端连接以及从java/.net core等程序连接时,可以使用正确的dns名称/连接字符串的任何人吗?

Can any one help with the correct dns name/connection string to be used while connecting with mongo client in command line and also from the programs like java/.net core etc.,?

我们还应该在这里使用kubernetes部署而不是statefulsets吗?

Also should we use kubernetes deployment instead of statefulsets here?

推荐答案

您需要通过命名空间dns引用mongo服务.因此,如果您的mongo服务是mymongoapp且已部署在mymongonamespace中,则您应该能够以mymongoapp.mymongonamespace的身份访问它.

You need to reference the mongo service by namespaced dns. So if your mongo service is mymongoapp and it is deployed in mymongonamespace, you should be able to access it as mymongoapp.mymongonamespace.

为了测试,我使用了bitnami/mongodb泊坞窗客户端.如下:

To test, I used the bitnami/mongodb docker client. As follows:

mymongonamespace内部,此命令有效

$ kubectl config set-context --current --namespace=mymongonamespace
$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp

但是当我切换到默认名称空间时,它不起作用

But when I switched to namespace default it didn't work

$ kubectl config set-context --current --namespace=default
$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp

然后使用名称空间对主机进行验证

Qualifying the host with the namespace then works

$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp.mymongonamespace

这篇关于连接到不同名称空间中的Kubernetes mongo db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆