Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 396
Mike Avatar asked Nov 07 '22 08:11

Mike


1 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
like image 88
cahit beyaz Avatar answered Nov 15 '22 08:11

cahit beyaz