8

Running this in WSL2 (current folder /home/my-linux-user/):

az aks get-credentials --resource-group my-resource-group --name cluster-name

Outputs:

Merged "cluster-name" as current context in C:\Users\my-windows-user\.kube\config

What do I need to do to get Azure CLI to put the kube config in the Linux home folder in WSL instead?

Joel
  • 8,502
  • 11
  • 66
  • 115
  • Why you want to get Azure CLI to put the kube config in the Linux home folder in WSL instead ? – Philippe Aug 23 '21 at 16:00
  • @Philippe I want to use `kubectl` in Linux (WSL). An easy way to get the kube config from an aks cluster is using azure cli. Got this working on an old machine, but I can't remember what I did then now that I'm setting things up on a new machine. – Joel Aug 23 '21 at 19:12
  • Ok, I understand now. Have you tried to make a symbolic link, something like `ln -fs /mnt/c/Users/my-windows-user/.kube /home/my-linux-user/.kube` ? The `kubectl` you use, is it WSL or Windows version ? – Philippe Aug 23 '21 at 19:24
  • @Philippe Sorry, should have made that clear. I'm using Ubuntu 20.04. I have installed `kubectl` in WSL using Brew (for Linux) – Joel Aug 24 '21 at 06:07

2 Answers2

11

I believe the root of the problem here is that the Azure CLI being run is actually the Windows version. I struggled with precisely this same issue until I discovered I was running the Windows version of AZ, thus it assumed Windows paths. This is one of those areas where Microsoft wanted to make things super easy for folks by just running Windows apps in WSL, yet it bites you in the arse.

Solution: install the Linux version of Azure CLI. Instructions found here: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux

On Ubuntu it's as easy as:

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Then all will "just work" as you expected. Super clean and easy, and also easy to overlook.

BrettRobi
  • 3,793
  • 7
  • 40
  • 64
3

What Linux distribution are you using in WSL? Another pertinent question is from @Philippe

The kubectl you use, is it WSL or Windows version ?

Are you running the kubectl client provided by az aks install-cli in the WSL Linux distribution? C: is going to be only available from the WSL Linux shell as /mnt/c/ so you are not supposed to get any message with a path like C:\Users\my-windows-user\.kube\config. Please ensure you are using the linux kubectl executable from a WSL shell.

In my case, I use Ubuntu 20.04 LTS [How-to install] with WSL. Following is a sample:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

Loading personal and system profiles took 1725ms.
❯ srbose@xxxxxxx ❯ ~ ❯
❯ bash
srbose@xxxxxxx:/mnt/c/Users/srbose$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
srbose@xxxxxxx:/mnt/c/Users/srbose$ az aks get-credentials -g $RG -n akstest
The behavior of this command has been altered by the following extension: aks-preview
/home/srbose/.kube/config has permissions "644".
It should be readable and writable only by its owner.
Merged "akstest" as current context in /home/srbose/.kube/config
srbose@xxxxxxx:/mnt/c/Users/srbose$

The default behaviour is as follows:

If the --kubeconfig flag is set, use only the specified file. Do not merge. Only one instance of this flag is allowed.

Otherwise, if the KUBECONFIG environment variable is set, use it as a list of files that should be merged. Merge the files listed in the KUBECONFIG environment variable according to these rules:

  • Ignore empty filenames.
  • Produce errors for files with content that cannot be deserialized.
  • The first file to set a particular value or map key wins.
  • Never change the value or map key. Example: Preserve the context of the first file to set current-context. Example: If two files specify a red-user, use only values from the first file's red-user. Even if the second file has non-conflicting entries under red-user, discard them.

For an example of setting the KUBECONFIG environment variable, see Setting the KUBECONFIG environment variable.

Otherwise, use the default kubeconfig file, $HOME/.kube/config, with no merging. For more information please check this article.

You can check if the KUBECONFIG environment variable is set using

echo $KUBECONFIG
Dharman
  • 30,962
  • 25
  • 85
  • 135
Srijit_Bose-MSFT
  • 1,010
  • 4
  • 13
  • 1
    `whereis az` returns `az: /usr/bin/az /opt/az/bin/az.bat /opt/az/bin/az /mnt/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/wbin/az /mnt/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/wbin/az.cmd` So I'm taking it that Linux "sees" the install of Azure CLI in both Windows and Linux. `which az` returns `/mnt/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/wbin/az` Which is the wrong one (I want the Linux one on Linux). – Joel Aug 24 '21 at 06:44
  • Oh! I figured it out. I'm stupid. I was sure I had restarted the terminal after installing, but apparently I hadn't. After a restart, `which az` returns the correct one. – Joel Aug 24 '21 at 06:45