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).
Best Answer
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:
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 usingkubectl describe service mongodb-service
orkubectl get service mongodb-service -o yaml
commands.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 theport
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