0

I am running a mongo docker container service inside a kubernetes cluster. Incoming traffic from outside is routed via ingress-nginx (https://kubernetes.github.io/ingress-nginx/) load balancer to the mongo docker container service.

Application is working as expected and I need to know whether there is a way I can connect the MongoDB compass client (which i installed in my local machine) to the running mongodb container to visualize the data easily.

I am having issues since the container is inside kubernetes and there is a load balancer in between.

Would be of great help if someone could assist. Thanks

RammusXu
  • 1,180
  • 1
  • 7
  • 21
Shanka Somasiri
  • 581
  • 1
  • 8
  • 30

1 Answers1

3

You can use port-forward

$ kubectl port-forward -h
Forward one or more local ports to a pod. This command requires the node to have 'socat' installed.

 Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.

 If there are multiple pods matching the criteria, a pod will be selected automatically. The forwarding session ends
when the selected pod terminates, and rerun of the command is needed to resume forwarding.

Examples:
  # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
  kubectl port-forward pod/mypod 5000 6000

  # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the
deployment
  kubectl port-forward deployment/mydeployment 5000 6000

  # Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the service
  kubectl port-forward service/myservice 5000 6000

  # Listen on port 8888 locally, forwarding to 5000 in the pod
  kubectl port-forward pod/mypod 8888:5000

  # Listen on port 8888 on all addresses, forwarding to 5000 in the pod
  kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000

  # Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
  kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000

  # Listen on a random port locally, forwarding to 5000 in the pod
  kubectl port-forward pod/mypod :5000

Options:
      --address=[localhost]: Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a
value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1 and will fail if neither of these
addresses are available to bind.
      --pod-running-timeout=1m0s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one
pod is running

Usage:
  kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

Use "kubectl options" for a list of global command-line options (applies to all commands).

hoque
  • 5,735
  • 1
  • 19
  • 29
  • BY any chance do you know how to add the port forward entry in the .yaml file?? – Shanka Somasiri Jun 02 '20 at 05:17
  • sorry don't get the question. can you clarify a bit more what do want to add in which .yaml file? you can add port on deployment container using `containerPort` – hoque Jun 02 '20 at 05:55
  • `apiVersion: v1 kind: Service metadata: name: mongo-srv spec: type: ClusterIP selector: app: auth-mongo ports: - name: auth-db protocol: TCP port: 27017 targetPort: 27017` @hoque: Above is the code to create the mongo-service which is placed inside the.YAML file. Imagine i need to do the port forward so that 127.0.0.1:7000 traffic is routed to mongo-service above. Command line is working fine. But i need a way to do the same using the .YAML file. – Shanka Somasiri Jun 03 '20 at 03:33