Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of MongoDB Kubernetes Operator, refer to the upgrade documentation.

Connect to a MongoDB Resource from Outside Kubernetes

The following procedure describes how to connect to a MongoDB resource deployed by Kubernetes from outside of the Kubernetes cluster.

Important

You can only connect to a standalone or a sharded cluster resource from outside of the Kubernetes cluster.

Procedure

To connect to a MongoDB resource deployed by Kubernetes from outside of the Kubernetes cluster, you must set the resource’s spec.exposedExternally flag to true.

When this flag is set to true, the Kubernetes Operator creates a NodePort service. The NodePort service exposes the deployment as a network service, thereby enabling access from outside of the Kubernetes cluster.

Example Deployment Configurations

The following example standalone configuration object exposes the deployment outside of the Kubernetes cluster by setting spec.exposedExternally to true:

---
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
  name: <my-standalone>
  namespace: <metadata.namespace>       # Should match
                                        # metadata.namespace in
                                        # your configmap file.
spec:
  version: 4.2.1
  opsManager:                           # Alias of cloudManager
    configMapRef:
      name: <configMap.metadata.name>   # Should match metadata.name
                                        # in your configmap file.
  credentials: <mycredentials>
  type: Standalone
  persistent: true
  exposedExternally: true
...

Once your standalone instance is deployed in Cloud Manager or Ops Manager, run the following command to get information on the NodePort service created by the Operator:

kubectl get services -n <namespace>

The list output by this command should contain an entry similar to the following:

NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE

standalone-svc-external   NodePort    10.102.27.116   <none>        27017:30994/TCP   8m30s

In this case, the mongod is exposed on port 27017 in the Kubernetes container, and the NodePort service exposes the mongod via port 30994.

When you connect to your deployment, you must specify the external DNS of a node in the Kubernetes cluster as the --host option in your mongo command. If a node in the Kubernetes cluster has an external DNS of ec2-54-212-23-143.us-west-2.compute.amazonaws.com, you can connect to this standalone instance from outside of Kubernetes using the following command:

mongo --host ec2-54-212-23-143.us-west-2.compute.amazonaws.com --port 30994

Tip

To obtain the external DNS of your Kubernetes cluster, you can run the following command:

kubectl describe nodes

This command displays the external DNS in the Addresses.ExternalDNS section of the output.

Alternatively, you can output the external DNS directly by running:

kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="ExternalDNS")].address }'

The following example sharded cluster configuration object exposes the deployment outside of the Kubernetes cluster by setting spec.exposedExternally to true:

---
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
  name: <my-sharded-cluster>
  namespace: <metadata.namespace>       # Should match
                                        # metadata.namespace in
                                        # your configmap file.
spec:
  version: 4.2.1
  opsManager:                           # Alias of cloudManager
    configMapRef:
      name: <configMap.metadata.name>   # Should match metadata.name
                                        # in your configmap file.
  shardCount: 2
  mongodsPerShardCount: 3
  mongosCount: 2
  configServerCount: 3
  credentials: my-secret
  type: ShardedCluster
  persistent: true
  exposedExternally: true
...

Once your sharded cluster is deployed in Cloud Manager or Ops Manager, run the following command to get information on the NodePort service created by the Operator:

kubectl get services -n <namespace>

The list output by this command should contain an entry similar to the following:

NAME                          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE

shardedcluster-svc-external   NodePort    10.106.44.30    <none>        27017:30078/TCP   10s

In this case, the mongos is exposed on port 27017 in the Kubernetes container, and the NodePort service exposes the mongos via port 30078.

When you connect to your deployment, you must specify the external DNS of a node in the Kubernetes cluster as the --host option in your mongo command. If a node in the Kubernetes cluster has an external DNS of ec2-54-212-23-143.us-west-2.compute.amazonaws.com, you can connect to this standalone instance from outside of Kubernetes using the following command:

mongo --host ec2-54-212-23-143.us-west-2.compute.amazonaws.com --port 30078

Tip

To obtain the external DNS of your Kubernetes cluster, you can run the following command:

kubectl describe nodes

This command displays the external DNS in the Addresses.ExternalDNS section of the output.

Alternatively, you can output the external DNS directly by running:

kubectl get nodes -o jsonpath='{ $.items[*].status.addresses[?(@.type=="ExternalDNS")].address }'