Using Lens to inspect a Kubernetes minikube cluster on WSL
1. A bit of context
1.1 Lens
Lens is like a Kubernetes (K8s) IDE, with the app you can monitor and interact with K8s clusters using a graphical user interface (GUI). See Video 1 for an intro on that desktop app.
Video 1 - Introduction to Lens.
In the context of this tutorial, we installed Lens on Windows 11.
1.2 Minikube
Minikube is an easy way to have a local K8s cluster running on your computer, all you need is Docker container or some similar tool, and kubectl. For a quick intro to this tool check Video 2.
Video 2 - Minikube and Kubectl explained.
Note: TechWorld with Nana is an amazing channel! Check it out if you are interested in DevOps.
In the context of this tutorial we installed minikube on WSL, so we followed the Linux installation guide from the minikube website.
After running the minikube start
command you can check if you are using Docker, the containers that represent your K8s with:
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1536bce200c gcr.io/k8s-minikube/kicbase:v0.0.33 "/usr/local/bin/entr…" 2 weeks ago Up 2 minutes 127.0.0.1:49157->22/tcp, 127.0.0.1:49156->2376/tcp, 127.0.0.1:49155->5000/tcp, 127.0.0.1:49154->8443/tcp, 127.0.0.1:49153->32443/tcp minikube
And with kubectl you can check the nodes too:
➜ ~ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 20d v1.24.3
You may want to change your kubectl context if you have multiple clusters configured, for example:
➜ ~ kubectl config get-contexts # get available contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
minikube minikube minikube default
* other-cluster other-cluster clusterUser_k8s_other-cluster
➜ ~ kubectl config use-context minikube # in this example, changes the context from 'other-cluster' to 'minikube'
Switched to context "minikube".
Also, you may simulate a multi-node K8s instance with minikube if you want to test a multi-node deployment (when we talk about databases, it is a good practice to have at least 3 instances in different nodes for robustness to node failure). You can create a new multi-node cluster/profile by running:
minikube start --nodes <number-of-nodes> --profile <name-of-new-profile>
(click to expand) Example run
You may check your multiple profiles with minikube profile list
, change profile with minikube profile <profile-name>
, and run specific actions to profiles by adding -p <profile-name>
to the end of the command. (click to expand) Example run
And you may see your kubectl
available contexts with the command kubectl config get-contexts
and take a look at the new nodes with kubectl get nodes
. (click to expand) Example run
Note: Although is possible to run this multi-node test locally it is difficult to work on a modest machine now that we have 3 containers each expecting 2 CPUs and 2 Gb of RAM .
1.3 WSL
I’ve made a quick intro about it on an older post, but you can go straight to this quick video intro. Basically, WSL is a tool to have a Linux developer environment on your Windows PC.
2. Using Lens to inspect a Kubernetes minikube cluster on WSL
Wait! Couldn’t I just use minikube dashboard
instead?
Yes, minikube comes with a dashboard (see Figure 1) so you can look up your local K8s cluster and interact with it, but the idea of using Lens is to have the same tool for interacting with the local cluster and with a production cluster in a cloud provider.
First, check if your cluster is running with minikube status
command, if no cluster is running you can spin one up with the minikube start
command. (click to expand) Example run
If you still don’t have it, download and install Lens. Start it and go the the “Cluster” tab as shown by the left arrow in Figure 2 and then click on the plus sign pointed by the right arrow. Figure 3 shows the possible ways to add clusters to Lens.
Here comes the problem, you may want to sync the kubeconfig file that you can find in a path like this: \\wsl.localhost\Ubuntu-20.04\home\romulo\.kube\config
when using the first option shown in Figure 3. You shall end up with a list of clusters similar to Figure 4, where in my case I can easily access the second one, a K8s cluster on Azure. The problem lies when trying to access the minikube one, the error in Figure 5 is raised.
(click to expand) Full error
From the error log we can easily see that part of the path is being repeated, e.g. \home\romulo
in \\wsl.localhost\Ubuntu-20.04\home\romulo\.kube\home\romulo\.minikube\profiles\multinode3\client.crt
.
So, the best approach to deal with this mismatch I’ve found is to make a copy of the ~/.kube/config
file to, for example, ~/kubeconfig
and delete on this new file all instances of /home/<username>/
(in my case /home/romulo/
). This way you don’t mess around with the config file necessary for minikube
and kubectl
to work properly while you use Lens.
After syncing this new ~/kubeconfig
file with Lens, you will still need to update it if your ~/.kube/config
changes (possibly by adding/deleting clusters connections). Nevertheless minikube has an annoying behavior: it changes the server port every time the cluster restarts. For example, you may find at the first minikube start a line in the kubeconfig file with server: https://127.0.0.1:49174
and then, after a cluster restart you may see server: https://127.0.0.1:49189
. Fortunately, if you are using Docker or Podman to virtualize the cluster, you can set up the port forwarding on the creation of the minikube cluster/profile. You shall run:
minikube delete # to delete old docker containers for the active profile
minikube start --ports=127.0.0.1:55555:8443 # forwarding <server-port-you-want-fixed>:<K8s-default-apiserver-port>
(click to expand) Example run:
Note: The 55555
is an arbitrary port number I’ve chosen (it’s five fives ) and 8443
is the default API server port for K8s, if you change it you shall change it here too.
Unfortunately, this port fixing trick doesn’t work with multi-node minikube cluster (see PR#9404). If you want to see for yourself try to create a new cluster with minikube start --nodes 3 --profile multinode3 --ports=127.0.0.1:55555:8443
or add a new node to the single node cluster created before with minikube node add
. Both of them raise an error due to trying to use the same port forwarding (127.0.0.1:55555:8443
) for the worker nodes too. For the case of a multi-node minikube cluster you will need to keep updating the server port every time you want to use Lens and the cluster has been restarted .
I personally maintain a single node profile named minikube with the port forwarding trick and a multi-node one named multinode3. When I want to use Lens with the multi-node one I manually update my ~/kubeconfig
file with the new random port.
➜ ~ minikube profile list
|------------|-----------|---------|--------------|------|---------|---------|-------|--------|
| Profile | VM Driver | Runtime | IP | Port | Version | Status | Nodes | Active |
|------------|-----------|---------|--------------|------|---------|---------|-------|--------|
| minikube | docker | docker | 192.153.49.2 | 8443 | v1.25.0 | Stopped | 1 | * |
| multinode3 | docker | docker | 192.153.67.2 | 8443 | v1.25.0 | Stopped | 3 | |
|------------|-----------|---------|--------------|------|---------|---------|-------|--------|