- Deployment Access >
- Connect to a MongoDB Resource from Outside Kubernetes
Connect to a MongoDB Resource from Outside Kubernetes¶
On this page
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¶
- Standalone
- Sharded Cluster
The following example standalone configuration object exposes
the deployment outside of the Kubernetes cluster by setting
spec.exposedExternally
to 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:
The list output by this command should contain an entry similar to the following:
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:
Tip
To obtain the external DNS of your Kubernetes cluster, you can run the following command:
This command displays the external DNS in the
Addresses.ExternalDNS
section of the output.
Alternatively, you can output the external DNS directly by running:
The following example sharded cluster configuration object exposes
the deployment outside of the Kubernetes cluster by setting
spec.exposedExternally
to 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:
The list output by this command should contain an entry similar to the following:
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:
Tip
To obtain the external DNS of your Kubernetes cluster, you can run the following command:
This command displays the external DNS in the
Addresses.ExternalDNS
section of the output.
Alternatively, you can output the external DNS directly by running: