[英]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 允许在集群外访问服务的几种方法(即
hostNetwork
、 hostPort
、 NodePort
、 LoadBalancer
、 Ingress
)
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.