简体   繁体   English

如何从 Kubernetes 集群外部连接到 Mongodb

[英]How to connect to Mongodb from outside Kubernetes cluster

I need to connect to mongodb external of the kubernetes cluster.我需要连接到 kubernetes 集群外部的 mongodb。 I dont know how to do it when i search on internet all the time i found all information about how to connect to mongodb inside the K8s cluster .当我一直在互联网上搜索时,我不知道该怎么做,我找到了有关如何在 K8s 集群中连接到 mongodb 的所有信息。 On the other hand, I wouldn't be against it.另一方面,我不会反对。 but for the begining of the project i'must to connect to external mongodb .但是在项目开始时,我必须连接到外部 mongodb 。

Do you know how to do it ?你知道怎么做吗? or do you have any information who can help me ??或者你有任何可以帮助我的信息吗??

deployment.yaml :部署.yaml:

  - name: XXXX_CONFIG_API_MONGODB
    value: "mongodb://@IP:27017"

thanks in advance提前致谢

You need to update service not in deployment.您需要更新不在部署中的服务。 It's service related issue.这是服务相关的问题。

apiVersion: v1
kind: Service
metadata:
  name: mongod-db-service
spec:
  selector:
    app: mongod-db
  ports:
    - port: 27017
      targetPort: 27017
  type: LoadBalancer

Note:- "mongod-db" is kubernetes selector that should be same in deployment.注意:-“mongod-db”是 kubernetes 选择器,在部署中应该是相同的。

I need to connect to mongodb external of the kubernetes cluster.我需要连接到 kubernetes 集群外部的 mongodb。

K8s allows a few methods for service to be reachable outside the cluster (namely hostNetwork , hostPort , NodePort , LoadBalancer , Ingress ) K8s 允许在集群外访问服务的几种方法(即hostNetworkhostPortNodePortLoadBalancerIngress

This article is so far one of the best on topic. 这篇文章是迄今为止最好的主题之一。

In general you just need to create a service that'll point to your mongodb.一般来说,您只需要创建一个指向您的 mongodb 的服务。

It can be one of (but not limited to):它可以是以下之一(但不限于):

  • LoadBalancer type: LoadBalancer类型:
kind: Service 
apiVersion: v1 
metadata: 
  name: mongo 
spec: 
  type: LoadBalancer 
  ports: 
    - port: 27017 
  selector: 
    app: my-mongo-db  # this shall match labels from the Deployment
  • NodePort type: NodePort类型:
apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  selector:
    app: my-mongo-db
  type: NodePort
  ports:
    - 
      port: 27017
      nodePort: 30001 # al the incoming connections to NodeIP:30001 will be forwarded to your mongo-pod

There are more ways to achieve that (just don't want to copy paste here that article I have been referring to).有更多方法可以实现这一点(只是不想将我提到的那篇文章复制粘贴到这里)。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从集群外访问 Kubernetes 中的 MongoDB - How access MongoDB in Kubernetes from outside the cluster 如何从外部访问kubernetes集群上托管的mongodb replicaset? - How to access mongodb replicaset hosted on a kubernetes cluster from outside? 无法从 kubernetes 集群连接到 mongodb atlas 集群 - Unable to connect to mongodb atlas cluster from a kubernetes cluster 无法从 kubernetes 集群中的另一个 pod 连接到 mongodb - Cannot connect to mongodb from another pod in kubernetes cluster 如何从 mongodb 指南针连接 mongodb 集群? - How to connect mongodb cluster from mongodb compass? 使用群集外的StatefulSets在Kubernetes上公开MongoDB - Expose MongoDB on Kubernetes with StatefulSets outside cluster 如何从另一个集群 Pod 连接 kubernetes 集群上的 mongo 副本集 - How to connect mongo replicaset on kubernetes cluster from another cluster pods 无法连接到Kubernetes群集中的mongodb服务 - Cannot connect to a mongodb service in a Kubernetes cluster 当从 Kubernetes 集群内部而不是从外部调用时,MongoDB 会忽略身份验证 - MongoDB ignores authentication when called from inside Kubernetes cluster but not from outside 如何通过 MongoDB Compass 或 RoboMongo 等 UI 工具连接在 Kubernetes 集群上运行的 MongoDB? - How to connect MongoDB which is running on Kubernetes cluster through UI tools like MongoDB Compass or RoboMongo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM