DEV Community

Ghassan Karwchan
Ghassan Karwchan

Posted on

1

How to fix common error using Azure Kubernetes AKS from WSL2

When you use Kubernetes CLI tool: kubectl that is installed while you installed Docker Desktop for Windows, there is a small glitch that it will fail the kubectl commands, because of Kubeconfig file.

Let's supposed you want to create a new cluster on AKS, as follows:

az aks create --resource-group MyResourceGropu --name MyMicroserviceCluster --node-count 1 --enable-addons http_application_routing --generate-ssh-keys
Enter fullscreen mode Exit fullscreen mode

And then you need to set that new cluster as your default cluster to work on, so you use

az aks get-credentials --resource-group MyMicroserviceResources --name MyMicroserviceCluster
Enter fullscreen mode Exit fullscreen mode

The previous command will retrieve the credentials of the newly created cluster and store it in the kubeconfig file, and make the newly cluster as the current default cluster, so any subsequent kubectl command will run against that cluster.

If you run the previous command from WSL2 terminal, you will get the following message:

Merged "MyMicroserviceCluster" as current context in c:\Users\<username>\.kube\config
Enter fullscreen mode Exit fullscreen mode

Because Kubernetes CLI tool is installed with Docker Desktop for windows, so it default the config location to windows location.

But later any subsequent kubectl command from WSL2 terminal, will use the linux path for kubeconfig which is ~/.kube/config.

So, if you run the following command:

kubectl config current-context
Enter fullscreen mode Exit fullscreen mode

you will see that the current context still the current context in linux config file, which most probably is the default docker-desktop.

To fix the problem, you can specify the config file as environment variable KUBECONFIG.

So modify the profile file ~/.profile by adding the following:

export KUBECONFIG=/mnt/c/Users/<windows user>/.kube/config
Enter fullscreen mode Exit fullscreen mode

This will point the config file to the windows config file.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay