Asool
Asool

Reputation: 14199

Connecting to a MongoDB instance running in GKE via a GUI

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).

I'm aware that I can do something like I can do

kubectl get pods

kubectl exec -it <name of mongo db pod > sh

mongo

But what if I wanted to use a GUI?

I created a nodeport service as follows

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

Then

kubectl get services 

i see

orders-nodeport-mongo-service NodePort 10.20.6.146 27017:31113/TCP 15m

But when i try to connect to

mongo --host 10.20.6.146:31113

It doesn't work.. I'm doing something stupid here and I can't figure it out

As an FYI, here is my deployment

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

Upvotes: 0

Views: 803

Answers (1)

Mafor
Mafor

Reputation: 10681

10.20.6.146 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 ingress resource or change the service type to LoadBalancer (see Exposing an External IP Address to Access an Application in a Cluster).

For debugging purposes you can use port forwarding:

# 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

Upvotes: 1

Related Questions