|
| 1 | +.. _connect-from-outside-k8s: |
| 2 | + |
| 3 | +===================================================== |
| 4 | +Connect to a MongoDB Resource from Outside Kubernetes |
| 5 | +===================================================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +The following procedure describes how to connect to a MongoDB resource |
| 16 | +deployed by |k8s| from outside of the |k8s| cluster. |
| 17 | + |
| 18 | +.. important:: |
| 19 | + |
| 20 | + You can only connect to a :doc:`standalone |
| 21 | + </tutorial/deploy-standalone>` or a :doc:`sharded cluster |
| 22 | + </tutorial/deploy-sharded-cluster>` resource from outside of the |
| 23 | + |k8s| cluster. |
| 24 | + |
| 25 | +Procedure |
| 26 | +--------- |
| 27 | + |
| 28 | +To connect to a MongoDB resource deployed by |k8s| from outside of |
| 29 | +the |k8s| cluster, you must set the resource's |
| 30 | +:setting:`spec.exposedExternally` flag to ``true``. |
| 31 | + |
| 32 | +When this flag is set to ``true``, the |k8s-op-short| creates a |
| 33 | +`NodePort service |
| 34 | +<https://kubernetes.io/docs/concepts/services-networking/service/>`__. |
| 35 | +The NodePort service exposes the deployment as a network service, |
| 36 | +thereby enabling access from outside of the |k8s| cluster. |
| 37 | + |
| 38 | +Example Deployment Configurations |
| 39 | +--------------------------------- |
| 40 | + |
| 41 | +.. tabs-deployments:: |
| 42 | + |
| 43 | + .. tab:: |
| 44 | + :tabid: standalone |
| 45 | + |
| 46 | + The following example standalone configuration object exposes |
| 47 | + the deployment outside of the |k8s| cluster by setting |
| 48 | + ``spec.exposedExternally`` to ``true``: |
| 49 | + |
| 50 | + .. literalinclude:: /reference/k8s/example-exposed-standalone.yaml |
| 51 | + :language: yaml |
| 52 | + :emphasize-lines: 15 |
| 53 | + |
| 54 | + Once your standalone instance is deployed in |com|, run the |
| 55 | + following command to get information on the NodePort service |
| 56 | + created by the Operator: |
| 57 | + |
| 58 | + .. code-block:: sh |
| 59 | + |
| 60 | + kubectl get services -n <namespace> |
| 61 | + |
| 62 | + The list output by this command should contain an entry |
| 63 | + similar to the following: |
| 64 | + |
| 65 | + .. code-block:: sh |
| 66 | + :copyable: false |
| 67 | + |
| 68 | + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 69 | + |
| 70 | + standalone-svc-external NodePort 10.102.27.116 <none> 27017:30994/TCP 8m30s |
| 71 | + |
| 72 | + In this case, the :binary:`~bin.mongod` is exposed on port |
| 73 | + ``27017`` in the |k8s| container, and the NodePort service |
| 74 | + exposes the ``mongod`` via port ``30994``. |
| 75 | + |
| 76 | + When you connect to your deployment, you must specify the external |
| 77 | + :abbr:`DNS (Domain Name System)` of a node in the |k8s| cluster as |
| 78 | + the ``--host`` option in your ``mongo`` command. If a node in the |
| 79 | + |k8s| cluster has an external DNS of |
| 80 | + ``ec2-54-212-23-143.us-west-2.compute.amazonaws.com``, you |
| 81 | + can connect to this standalone instance from outside of |k8s| |
| 82 | + using the following command: |
| 83 | + |
| 84 | + .. code-block:: sh |
| 85 | + |
| 86 | + mongo --host ec2-54-212-23-143.us-west-2.compute.amazonaws.com --port 30994 |
| 87 | + |
| 88 | + .. include:: /includes/fact-get-external-dns.rst |
| 89 | + |
| 90 | + .. tab:: |
| 91 | + :tabid: shard |
| 92 | + |
| 93 | + The following example sharded cluster configuration object exposes |
| 94 | + the deployment outside of the |k8s| cluster by setting |
| 95 | + ``spec.exposedExternally`` to ``true``: |
| 96 | + |
| 97 | + .. literalinclude:: /reference/k8s/example-exposed-sharded.yaml |
| 98 | + :language: yaml |
| 99 | + :emphasize-lines: 19 |
| 100 | + |
| 101 | + Once your sharded cluster is deployed in |com|, run the |
| 102 | + following command to get information on the NodePort service |
| 103 | + created by the Operator: |
| 104 | + |
| 105 | + .. code-block:: sh |
| 106 | + |
| 107 | + kubectl get services -n <namespace> |
| 108 | + |
| 109 | + The list output by this command should contain an entry |
| 110 | + similar to the following: |
| 111 | + |
| 112 | + .. code-block:: sh |
| 113 | + :copyable: false |
| 114 | + |
| 115 | + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 116 | + |
| 117 | + shardedcluster-svc-external NodePort 10.106.44.30 <none> 27017:30078/TCP 10s |
| 118 | + |
| 119 | + In this case, the :binary:`~bin.mongos` is exposed on port |
| 120 | + ``27017`` in the |k8s| container, and the NodePort service |
| 121 | + exposes the ``mongos`` via port ``30078``. |
| 122 | + |
| 123 | + When you connect to your deployment, you must specify the external |
| 124 | + :abbr:`DNS (Domain Name System)` of a node in the |k8s| cluster as |
| 125 | + the ``--host`` option in your ``mongo`` command. If a node in the |
| 126 | + |k8s| cluster has an external DNS of |
| 127 | + ``ec2-54-212-23-143.us-west-2.compute.amazonaws.com``, you |
| 128 | + can connect to this standalone instance from outside of |k8s| |
| 129 | + using the following command: |
| 130 | + |
| 131 | + .. code-block:: sh |
| 132 | + |
| 133 | + mongo --host ec2-54-212-23-143.us-west-2.compute.amazonaws.com --port 30078 |
| 134 | + |
| 135 | + .. include:: /includes/fact-get-external-dns.rst |
| 136 | + |
| 137 | + |
0 commit comments