4

I am looking to see if I can connect to a remote minikube cluster (Ubuntu box) using local (Mac) kubectl. I currently use Docker and can do this very easily using docker-machine. Simply eval to the machine name, and docker will use the remote machine.

I was wondering if there was anything similar for minikube/kubectl? I have found a few articles that mention that I need to copy my remote ~/.minikube directory to my local, and change some config about. But this seems rather complicated for something a tool like docker-machine does seamlessly.

Is there a similar tool available, or if not, could someone help me with steps needed to connect to a remote cluster?

Remote Machine Currently I use the docker driver (this is the complete output of the command, just the one line):

$ minikube config view
- driver: docker

And have a number of NodePort services:

$ kubectl get service -A
NAMESPACE     NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default       apigateway        NodePort    10.100.122.255   <none>        8080:30601/TCP           19h
default       discoveryserver   NodePort    10.101.106.231   <none>        8761:30602/TCP           19h
default       elasticsearch     NodePort    10.97.197.14     <none>        9200:30604/TCP           19h
default       harness           NodePort    10.97.233.245    <none>        9090:30603/TCP           19h
default       kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP                  19h
default       mongo             NodePort    10.97.172.108    <none>        27017:32625/TCP          19h
kube-system   kube-dns          ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   19h
$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/meanwhileinhell/.minikube/ca.crt
    server: https://192.168.50.2:8443   <<<<<< `minikube ip`
  name: minikube
contexts:
- context:
    cluster: minikube
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /home/meanwhileinhell/.minikube/profiles/minikube/client.crt
    client-key: /home/meanwhileinhell/.minikube/profiles/minikube/client.key

Local machine

$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop
- cluster:
    certificate-authority: /Users/mih.mac/remote/.minikube/ca.crt
    server: https://192.168.1.5:8443   <<<<<< Static IP of my remote machine
  name: minikube
contexts:
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-desktop
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: docker-desktop
kind: Config
preferences: {}
users:
- name: docker-desktop
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
- name: minikube
  user:
    client-certificate: /Users/mih.mac/remote/.minikube/client.crt
    client-key: /Users/mih.mac/remote/.minikube/client.key
MeanwhileInHell
  • 141
  • 1
  • 5
  • So, You are trying to run minikube cluster with docker driver on docker-machine and access it from the host? – Matt Dec 09 '20 at 12:24
  • 1
    No, not using docker-machine. I was just mentioning it as for a standalone Docker environment, docker-machine makes this task very simple. I was hoping that there would be a similar tool for accessing a K8s/minikube cluster remotely. – MeanwhileInHell Dec 09 '20 at 15:37
  • Did you manage to find solution ? – Malgorzata Mar 10 '21 at 08:35

3 Answers3

4

There is no tool available. Way to remotely access minikube is to do SSH tunneling .

1- You need to be able to SSH from the Mac to the Ubuntu box.

2- Add appropriate SSH port forwarding flags.Run the following command

ssh -N -p 22 <user>@<public_ip> -L 127.0.0.1:18443:<minikube_ip>:8443

Where:

user is your name

public_ip is the public IP of your server

minikube_ip is the IP address of minikube, you can find it on the server using the command minikube ip. It will likely be 192.168.49.2.

3- Then just plug appropriate K8s credentials into kubectl on the Mac.

Please refer to the link for more information:

https://www.zepworks.com/posts/access-minikube-remotely-kvm/ https://www.chevdor.com/post/2021/02/docker_to_k8s/

1

Uou can use socat forward minikube default IP and port (192.168.49.2:8443) to system default network card IP.

socat TCP4-LISTEN:8443,fork TCP4:192.168.49.2:8443

Dave M
  • 4,514
  • 22
  • 31
  • 30
1

Steps to connect to a remote minikube cluster from kubectl:

  1. create a minikube cluster listening on a public interface, add a remote hostname and/or IP addresses to a generated certificate minikube start --listen-address=0.0.0.0 --apiserver-names=example.com --apiserver-ips=1.2.3.4
  2. use docker ps to obtain a public port for the API server - it should proxy to an 8443 port inside minikube container (it will look like 0.0.0.0:32774->8443/tcp - the port is 32774)
  3. get kube config for minikube using minikube kubectl -- config view and save it to a local file (for example ~/minikube.config)
  4. edit clusters[0].cluster.server field in the ~/minikube.config - set it to the appropriate remote host and port obtained in step 2
  5. create a local directory for certificates and a key: mkdir -p ~/.minikube/profiles/minikube
  6. copy client.key, client.crt and ca.crt from the minikube host to a local host: scp 'example.com:.minikube/profiles/minikube/client.*' ~/.minikube/profiles/minikube/ and scp 'example.com:.minikube/ca.crt' ~/.minikube/
  7. run locally kubectl --kubeconfig ~/minikube.config get pod -A to verify that it works
nikicat
  • 111
  • 4