How To
Connect to MongoDB from Outside Kubernetes
9 min
the following procedure describes how to connect to a mongodb resource deployed by kubernetes from outside of the kubernetes cluster create external mongodb services setup firewall setup robo3t connecting to mongodb replicaset primary member create external mongodb services by default, mongodb is internally accessible on port 27017 in kubernetes, if you want to expose a port to the outside world, you can use service with type nodeport step 1 lookup the swimlane namespace and save it as an environment variable $ kubectl get ns $ export namespace=\<swimlane namespace> step 2 create new service for mongo pod 0 $ vi mongodb service 0 yaml apiversion v1 kind service metadata name mongodb service 0 labels app mongodb replicaset spec type nodeport selector statefulset kubernetes io/pod name swimlane sw mongo 0 ports port 27017 nodeport 32000 $ kubectl apply f mongodb service 0 yaml n $namespace in line nodeport 32000, we specified the external port there, any port from the range 30000 32767 can be used step 3 create new service for mongo pod 1 $ vi mongodb service 1 yaml apiversion v1 kind service metadata name mongodb service 1 labels app mongodb replicaset spec type nodeport selector statefulset kubernetes io/pod name swimlane sw mongo 1 ports port 27017 nodeport 32001 $ kubectl apply f mongodb service 1 yaml n $namespace in line nodeport 32001, we specified the external port there, any port from the range 30000 32767 can be used step 4 create new service for mongo pod 2 $ vi mongodb service 2 yaml apiversion v1 kind service metadata name mongodb service 2 labels app mongodb replicaset spec type nodeport selector statefulset kubernetes io/pod name swimlane sw mongo 2 ports port 27017 nodeport 32002 $ kubectl apply f mongodb service 2 yaml n $namespace for the line "nodeport 32002", we specified the external port from there, any port from the range 30000 32767 can be used step 5 confirm services are working kubectl get svc l app=mongodb replicaset n $namespace setup firewall step 1 lookup ip addresses kubectl get nodes o wide step 2 open firewall to nodeport(s) you need to use the ip address of any node and grant tcp access to ports from the nodeport line, not from the port line in our example, we need to open ports 32000, 320001, and 320002 setup robo3t now, you can connect to mongodb using robo3t or any other client tools using the node ip addresses on which you opened ports 32000, 320001, and 320002 note once swimlane's mongo version is updated to 4 2 2, you also need to upgrade robo3t to 1 3 1 older versions of robo3t will be unable to connect to mongo 4 2 2 step 1 create a new mongodb connections for swimlane sw mongo 0 name swimlane sw mongo 0 address \<node internal ip> 32000 authentication enable step 2 create a new mongodb connections for swimlane sw mongo 1 name swimlane sw mongo 1 address \<node internal ip> 32001 step 3 create a new mongodb connections for swimlane sw mongo 2 name swimlane sw mongo 2 address \<node internal ip> 32002 note enable authentication and ssl connecting to mongodb replicaset primary member step 1 lookup the swimlane namespace and save it as an environment variable $ kubectl get ns $ export namespace=\<swimlane namespace> step 2 lookup mongodb admin password $ export mongo admin password= \<password here> \# confirm the export $ echo $mongo admin password step 3 determine mongodb replica set primary member kubectl n $namespace exec swimlane sw mongo 0 mongo u admin p $mongo admin password authenticationdatabase admin ssl sslallowinvalidhostnames sslallowinvalidcertificates admin eval="rs status();" | grep primary b5 from the output, note the name of the mongo pod (swimlane sw mongo 0 or swimlane sw mongo 1 or swimlane sw mongo 2) with statestr is equal to primary step 4 access mongodb replicaset member primary from robo3t now that you know which pod is running the mongodb replicaset member primary, use the corresponding mongodb connections created above to connect to the member