4

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).

jdoe
  • 41
  • 1
  • 1
  • 2

1 Answers1

4

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

Artem Golenyaev
  • 253
  • 1
  • 7
  • This didn't work for me. I have followed link https://medium.com/@dilipkumar/standalone-mongodb-on-kubernetes-cluster-19e7b5896b27. I have completed upto step 5. In step 2 I have used yaml mention above by you. But i am not able to connect from NoSqlBooster. – Hardik Patel Apr 23 '20 at 09:18