I’ve installed Docker Desktop, Kubernetes, and Minikube on Windows. Now I’d like to use kubectrl on WSL2 (Ubuntu), but I don’t know how.
If I run kubectl version
and kubectl cluster-info
on Windows (PowerShell):
PS C:UsersUserName> kubectl version
Client Version: v1.29.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.28.3
PS C:UsersUserName> kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:49366
CoreDNS is running at https://127.0.0.1:49366/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
If I run them on WSL2:
user@LAPTOP:~/app$ kubectl version
Client Version: v1.29.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
The connection to the server localhost:8080 was refused - did you specify the right host or port?
user@LAPTOP:~/app$ kubectl cluster-info
E0207 06:34:05.061042 29276 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0207 06:34:05.061283 29276 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0207 06:34:05.062630 29276 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0207 06:34:05.062815 29276 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
E0207 06:34:05.064124 29276 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
The connection to the server localhost:8080 was refused - did you specify the right host or port?
It seems to me that Kubectrl uses port 49366
on Windows but it uses port 8080
. Is it the cause? If so, how do I fix it?
EDIT:
I added the following line in ~/.bashrc
on WSL2 and then ran source ~/.bashrc
:
export KUBECONFIG=/mnt/c/users/Hiroki/.kube/config
Now I see the following:
user@LAPTOP:~/app$ kubectl cluster-info
Error in configuration:
* unable to read client-cert /mnt/c/users/UserName/.kube/C:UsersUserName.minikubeprofilesminikubeclient.crt for minikube due to open /mnt/c/users/UserName/.kube/C:UsersUserName.minikubeprofilesminikubeclient.crt: no such file or directory
It seems that kubectl on WSL2 can’t access minikube on Windows
2
Answers
I think your issue is connected with the certificate that minikube uses to connect. It prob stored on windows.
Did you tehy to symbolic link it?
example:
ln -s /mnt/c/users/Hiroki/.kube/C:UsersTakahashi.minikube/ /home/user/.kube/minikube
and try the connection.
Or try the firewall of the wsl2 if it blocks some ports.
To resolve your issue ensure that the kubectl configuration file on WSL2 is pointing to the correct location of the Minikube configuration. It happens due to the KUBECONFIG environment variable not set. Either the relevant user is not configured to use kubectl, or the.kube/config file is missing.
Also verify that WSL2 has the KUBECONFIG environment variable set. If not, direct it to the appropriate configuration file that has cluster information. Generally under your Windows user directory in.kube/config.
As per the official kubernetes doc on Creating a cluster with kubeadm, Try the below commands to resolve your issue :
Also follow this blog by K21 Academy to resolve the error you’re getting.
Follow below troubleshooting steps to resolve the error :
1. First make sure that the windows and WSL2 firewall rules permit port communication. Check portfording is properly set up or different ports are used by kubectl on Windows and WSL2. Use Proxy server which helps to resolve any network-related issues that may be preventing the connection.
2. Try using minikube start embed-certs to launch the minikube.
3. Logs will make this quite evident no such file or directory. Please see Unable to read client-cert/key for additional information and solutions.
4. Verify the client’s file location and check. The crt file is present in the designated location in your WSL2 distribution. To view the directory’s contents in WSL2, use the 1s command.
5. Check kubectl versions compatibility on both Windows and WSL2.