<p>I'm running a k8s cluster on google cloud.
I have a deployment running mongoDB.
I'd like to connect to this DB locally (to use a GUI for example).</p>
<p>I'm aware that I can do something like
I can do</p>
<pre><code>kubectl get pods

kubectl exec -it &lt;name of mongo db pod &gt; sh

mongo
</code></pre>
<p>But what if I wanted to use a GUI?</p>
<p>I created a nodeport service as follows</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: orders-nodeport-mongo-service
spec:
  type: NodePort
  selector:
    app: orders-mongo-pod
  ports:
    - name: db
      protocol: TCP
      port: 27017 # mongo listens for traffic at this port
      targetPort: 27017
      nodePort: 31113
</code></pre>
<p>Then</p>
<pre><code>kubectl get services 
</code></pre>
<p>i see</p>
<blockquote>
<p>orders-nodeport-mongo-service NodePort 10.20.6.146 
27017:31113/TCP 15m</p>
</blockquote>
<p>But when i try to connect to</p>
<pre><code>mongo --host 10.20.6.146:31113
</code></pre>
<p>It doesn't work.. I'm doing something stupid here and I can't figure it out</p>
<p>As an FYI, here is my deployment</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders-mongo-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: orders-mongo-pod
  template:
    metadata:
      labels:
        app: orders-mongo-pod
    spec:
      containers:
        - name: orders-mongo-container
          image: mongo
</code></pre>

Asool
<p><code>10.20.6.146</code> is a cluster IP. To connect to the NodePort you would need to know the node public IP. Normally, to access services from outside of the cluster, you would create an <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">ingress</a> resource or change the service type to LoadBalancer (see <a href="https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/" rel="nofollow noreferrer">Exposing an External IP Address to Access an Application in a Cluster</a>).</p>
<p>For debugging purposes you can use <a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">port forwarding</a>:</p>
<pre class="lang-sh prettyprint-override"><code># This call will block. The first 27017 is a local port and can be any free port.
kubectl port-forward deployment/orders-mongo-deployment 27017:27017

# In another console run
mongo --host localhost:27017

</code></pre>
