Skip to content

Instantly share code, notes, and snippets.

@lynsei
Created March 24, 2025 17:43
Show Gist options
  • Save lynsei/5ca21eb48948e3e06cc184a2cd476703 to your computer and use it in GitHub Desktop.
Save lynsei/5ca21eb48948e3e06cc184a2cd476703 to your computer and use it in GitHub Desktop.
Setup kubectl credentials for AKS

Configuring kubectl to Access AKS Clusters

This guide will help you configure kubectl to access Azure Kubernetes Service (AKS) clusters, including setting up contexts and testing local configurations.

Prerequisites

Make sure you have the following installed:

  • Azure CLI (az): To manage Azure resources.
  • kubectl: To interact with Kubernetes clusters.

Installation Commands

# Install Azure CLI
brew install azure-cli

# Install kubectl
brew install kubectl

Step 1: Authenticate with Azure

Log in to your Azure account:

az login

If you have multiple subscriptions, select the one you want to use:

az account set --subscription "YOUR_SUBSCRIPTION_NAME_OR_ID"

Step 2: Get Cluster Credentials

Use the following command to add the AKS cluster context to your kubectl configuration:

az aks get-credentials --resource-group YOUR_RESOURCE_GROUP --name YOUR_AKS_CLUSTER_NAME

Example

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

This will update your ~/.kube/config file with the cluster context.

Step 3: Verify Contexts

List available contexts:

kubectl config get-contexts

Switch between contexts as needed:

kubectl config use-context CONTEXT_NAME

Step 4: Test Your Configuration

Check the nodes to ensure connectivity:

kubectl get nodes

Verify all pods and services across all namespaces:

kubectl get pods -A
kubectl get services -A

Fetching All AKS Clusters and Resource Groups

To list all AKS clusters across all resource groups:

az aks list --query '[].{name:name, resourceGroup:resourceGroup}' -o table

Troubleshooting

Access Denied or Forbidden

You might not have the necessary permissions to access the cluster. Check your Azure role assignments:

az aks show --resource-group myResourceGroup --name myAKSCluster

Missing Permissions for Service Principal or Managed Identity

Ensure your service principal has the necessary roles:

  • Azure Kubernetes Service Cluster Admin
  • Azure Kubernetes Service Cluster User

Switching Between Clusters

If you have multiple clusters, you can switch contexts with:

kubectl config use-context CLUSTER_NAME

Let me know if you need further assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment