Connect to services running on different Kubernetes clusters at once

6/24/2020

I have db1 running on cluster1, and db2 on cluster2. They are not accessible externally, so to connect to db while developing/debugging, I use port-forwarding.

You can only set one cluster as the current/default context at a time, so you can port-forward one db at a time. So instead of connecting to one cluster at a time from my local machine, I start 2 docker containers each pointed to a different cluster, and so I can port-forward both db connections, and then map the container port to a different host port, where I can ultimately access my DB via a client.

$ docker run -d --name cluster1 -p 3306:3306 IMAGE
$ docker run -d --name cluster2 -p 3307:3306 IMAGE

Inside each container, I am doing

$ kubectl port-forward db-0 3306

On the host machine, I am trying to connect to localhost and the port numbers 3306-3307. I am getting:

Server not running on the provided host and port.

Instead of using -p 3306:3306 with the docker run command above, I also tried --expose 3306 and then connect using the container IP which I get using docker inspect and port 3306, but that didn't work either.

Is there anything I am missing? I am keen to know if there's any alternate way of archiving what I am trying to do. Thanks.

-- vis
docker
kubernetes

1 Answer

6/25/2020

As OP mentioned in comments, my suggestion worked so I decided to post it as the answer with complete explaination.


kubectl by default binds to local interface and therefore can be accessed only using localhost ip (127.0.0.1).

$ kubectl port-forward <podname> 8080:8080 &
Forwarding from 127.0.0.1:8080 -> 8080

$ ss -lnt | grep 8080
State      Recv-Q Send-Q  Local Address:Port        Peer Address:Port
LISTEN     0      128         127.0.0.1:8080                   *:*

To make it accessible from outside of container you need to use kubectl port-forward --address 0.0.0.0. This will bind to all interfaces.

$ kubectl port-forward <podname> 8080:8080 --address 0.0.0.0 &
Forwarding from 0.0.0.0:8080 -> 8080

$ ss -lnt | grep 8080
State      Recv-Q Send-Q  Local Address:Port        Peer Address:Port
LISTEN     0      128                 *:8080                   *:*
-- Matt
Source: StackOverflow