How to connect to k8s cluster of docker desktop on another machine?

Viewed 3386

I have a macbook (192.168.1.101) and a macmini(192.168.1.104) over same wifi.

I launched a k8s cluster through docker-desktop on macmini and would like to access it through kubectl on macbook.

Here is how my ~/.kube/config on macmini looks like:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ******
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop
contexts:
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-desktop
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-for-desktop
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: docker-desktop
  user:
    client-certificate-data: ******
    client-key-data: ******

How can I write ~/.kube/config on macbook? Currently I followed official doc and got following errors.

$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: http://192.168.1.104:6443
  name: macmini-cluster
contexts:
- context:
    cluster: macmini-cluster
    user: macmini-user
  name: macmini-context
current-context: macmini-context
kind: Config
preferences: {}
users:
- name: macmini-user
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
$ kubectl get pods
The connection to the server 192.168.1.104 was refused - did you specify the right host or port?

Update:

I added port 6443 to server of cluster and tried to telnet macmini's port 6443, but got:

$ telnet 192.168.1.104 6443
Trying 192.168.1.104...
telnet: connect to address 192.168.1.104: Connection refused
telnet: Unable to connect to remote host

When I checked on macmini:

$ netstat -na|grep 6443
tcp4       0      0  127.0.0.1.6443         *.*                    LISTEN

There seems to be an unresolved related issue.

4 Answers

It seems your kubernetes api server did not bind to a local network accessible ipv4 address, instead it is bound to host's loopback adapter at 127.0.0.1

$ netstat -na|grep 6443
tcp4       0      0  127.0.0.1.6443         *.*                    LISTEN

Which means it can only be accessed by the machine running the process.

You need to proxy this port to your local ipv4 network. You can do this as below with command prompt running in kubernetes host computer as administrator:

netsh interface portproxy add v4tov4 listenaddress=192.168.1.104 listenport=6443 connectaddress=127.0.0.1 connectport=6443

In the macbook, the port number has to be specified as below. That's the port number of the K8S APIServer. (1)

server: http://192.168.1.104:6443

You can just copy your .kube/config file from the mac-mini desktop to macbook, you dont have to write the config file again if you want to use the same context.

There's an internal hostname docker-desktop pointing to kubernetes api-server, however, this hostname can be accessed by any of the inside containers without the --link option, which we can give a hack below to make a port-forwarding trick

docker run -d -p 0.0.0.0:6444:6443 bobrik/socat TCP-LISTEN:6443,fork TCP:docker-desktop:6443

I once thought to leverage kubernetes service, but no time to keep digging, hope anyone else has any ideas on this trick.

In addition to that, don't forget to make a little change on your ~/.kube/config below to avoid the x509 certificate verification

clusters:
- cluster:
    server: https://<your docker host>:6444
    insecure-skip-tls-verify: true
  name: docker-desktop
Related