大网数据广告活动
服务器运维问答

How to expose and access MongoDb ports in Kubernetes?

· 52CE 我爱测

I have mongoDB deployed on Kubernetes. The Database is setup properly and can be accessed using its internal dns name. I need to expose this to the outside world. I have used node port/ loadbalancer to expose port 27017, but I ca…

问题原文

原帖链接 · jdoe · CC BY-SA 4.0

I have mongoDB deployed on Kubernetes. The Database is setup properly and can be accessed using its internal dns name.

I need to expose this to the outside world. I have used node port/ loadbalancer to expose port 27017, but I cannot connect to the port using IP:port. Not faced an issue with web applications using IP:Port, not sure how to do it for a database.

If tried to browser, http://Kubernetes_Public_IP:NodePort gives me the following error message - It looks like you are trying to access MongoDB over HTTP on the native driver port.

I'm using robomongo to connect to the database.Using RoboMongo, setting Address to Kubernetes Public IP, and the Port to NodePort, I get the following error - Unable to connect to Kubernetes_Public_IP:NodePort

Need help in exposing the port and connecting to it using any kind of agent (robomongo not necessary).

社区高分解答

原帖链接 · Artem Golenyaev · CC BY-SA 4.0

In Kubernetes, if you want to expose a Port to the outside world, you can use Service with Type NodePort or LoadBalancer.

Type LoadBalancer is usually used on cloud providers since they provide external load balancers for Kubernetes.

So, in your case, NodePort is the easiest way to expose the Port. Here is an example of Service YAML:

kind: Service
apiVersion: v1
metadata:
  name: mongodb-service
spec:
  type: NodePort
  selector:
    app: mongodb
  ports:
    - port: 27017
      nodePort: 32463 
      name: MongoPort

In line port: 27017, we specified your MongoDB port, it is also usually specified in Deployment for MongoDB.

In line nodePort: 32463, we specified the external port. There, any port from the range 30000-32767 can be posted. Or it can be skipped during the creation, in that case, Kubernetes assigns the port number automatically, and you can find it using kubectl describe service mongodb-service or kubectl get service mongodb-service -o yamlcommands.

After that, you can use any client, e.g. RoboMongo, to reach you MongoDB. You need to use the IP address of any node and the port from the nodePort line, not from the port line.

For example, if you have a cluster with three nodes with external IP addresses 12.13.14.151, 12.13.14.152, 12.13.14.153, you can use any of 12.13.14.151:32463, 12.13.14.152:32463, 12.13.14.153:32463 in your RoboMongo connection settings.

For more information about Services, you can check the following link:

Publishing services - service types

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。