Install Minikube in VirtualBox on Remote Machine for Kubectl

At work, we are using Kubernetes as a way to run our application services. To test and debug deployments before they go into code review and to the development environment, a local Kubernetes is beneficial. That is where Minikube comes into play. Unfortunately for me, our application services require more resources than my work laptop can provide, especially RAM. Either I close all applications and run Minikube, or I have a helpful browser and IDE window open 😉.

Since I need the local K8s cluster from time to time, I wondered if I could run it on my personal computer and access it from my laptop. This way, I can dedicate at least six physical cores and 24 GB of RAM to the VM (even more, but that was a nice number and more than enough).

The setup of Minikube itself is simple.

minikube start \
    --vm-driver=virtualbox \
    --cpus 6 --memory 24576 \
    --kubernetes-version 1.19.6 \
    --apiserver-ips=192.168.178.26

All options except the last are self-explanatory. apiserver-ips is necessary because of the certificates that Minikube creates. Otherwise, you will receive a certificate-ip-mismatch error when you try to connect to it from another computer. This option defines the IP address of the computer that Minikube is running on.

From the Minikube documentation:

--apiserver-ips ipSlice    A set of apiserver IP Addresses which are used 
                           in the generated certificate for kubernetes. 
                           This can be used if you want to make the apiserver 
                           available from outside the machine (default [])

Next, you need to forward at least port 8443 to access Minikube from a remote computer. If you want to access your application, e.g. through HTTP, then you must add more port forwardings. Open the virtual machine’s "Network" settings, expand the "Advanced" section, and from there, you can open the "Port Forwarding" editor.

The SSH port forwarding was already there. I added 51080 to 80 (HTTP) and 51928 to 8443 (kubectl). You can choose any free, user-accessible port you want to expose 8443 on the host. I picked this one up from somewhere else and just stuck with it. Ultimately, it does not matter. Although these instructions are for VirtualBox, the same concept should apply to any other Hypervisor.

Lastly, you must configure the client to use the remote Minikube cluster. Minikube, by default, configures the local kubectl to access it automatically. You can take this configuration and apply it to the client computer.

You need to edit /home/<user>/.kube/config and add the remote Minikube cluster. You can copy the configuration file’s values on the Minikube server and apply 1:1 on the client. The one thing you need to adjust is the IP address and the port. Logically, Minikube configures itself for local access. In my case it is server: https://192.168.178.26:51928, the IP address of the Minikube machine and the port as configured in VirtualBox’ port forwardings.

apiVersion: v1
clusters:
- cluster:
    ...other clusters...
- cluster:
    certificate-authority: C:\Users\<User>\.minikube-priv\ca.crt
    extensions:
    - extension:
        last-update: Wed, 03 Feb 2021 07:30:44 CET
        provider: minikube.sigs.k8s.io
        version: v1.17.1
      name: cluster_info
    server: https://192.168.178.26:51928
  name: mkube-priv
contexts:
- context:
    ...other contexts...
- context:
    cluster: mkube-priv
    extensions:
    - extension:
        last-update: Wed, 03 Feb 2021 07:30:44 CET
        provider: minikube.sigs.k8s.io
        version: v1.17.1
      name: context_info
    namespace: default
    user: mkube-priv
  name: mkube-priv
current-context: mkube-priv
kind: Config
preferences: {}
users:
- name: ...other users...
- name: mkube-priv
  user:
    client-certificate: C:\Users\<User>\.minikube-priv\profiles\minikube\client.crt
    client-key: C:\Users\<User>\.minikube-priv\profiles\minikube\client.key

Notice how my client is a Windows machine, not Linux. If you run WSL, you must then copy it all to the WSL user’s configuration. I did it on both the Windows environment and the Linux environment on Windows. There are three sections to copy a configuration to:

  • clusters
  • contexts
  • users

Additionally, I decided to also copy Minikube’s certificates from the /home/<user>/.minikube folder to the client and store that data in a separate folder. It is easy to determine what is required. Look at the kubectl configuration and what files it references.

I was lazy and copied the complete profiles folder. You only need a few files, but why bother?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.