Installing kubectl on WSL

There is no doubt that kubernetes is a powerful platform for running applications. And more and more we are seeing it become the default across the world, and even in regulated industries. Now as part of that, we need to have an IDE to support connecting to a kubernetes cluster. The key part to that IDE is kubectl.

For those who haven’t worked with it, kubectl is a tool for connecting to kubernetes clusters, and working with those cluster. Specifically it allows you to do things like:

  • Get pods, services, deployments, secrets, and other kubernetes objects.
  • Deploy and remove apps from the cluster.
  • Interact with the kubernetes API to support managing the cluster.

But the real question is “How do I install kubectl?”

If you need steps to installing WSL on an Azure Virtual Machine, you can see my post here.

Run the following steps:

# Run this to download using curl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# Run this command to install the utility
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
# Run this to validate the install
kubectl version --client

From there, you can use the following to connect to a cluster:

# Set your parameters
azure_subscription_id="" # The subscription of your aks cluster
aks_resource_group="" # The resource group of the AKS Service
aks_name="" # The name of the AKS Service.
az cloud set --name AzureUSGovernment
az login --use-device-code
az account set --subscription $azure_subscription_id
az aks get-credentials --resource-group $aks_resource_group --name $aks_name

Now when you do execute the following, you will get a prompt showing where on the windows file system it saved the “/.kube/config” file. The path is likely:

C:\users{Username}.kube\config

To set your linux context correctly, run the following:

USERNAME="" # The username you are logged in with.
export KUBECONFIG=/mnt/c/Users/$USERNAME/.kube/config
# To make this persist, use the following:
echo 'export KUBECONFIG=/mnt/c/Users/$USERNAME/.kube/config' >> ~/.bashrc
source ~/.bashrc

You can then validate the connection with the following command:

kubectl get pods -A

Leave a Reply

Your email address will not be published. Required fields are marked *