Kubectl and K9s

1 minute read


After downloading your context file for an Azure Kubernetes Services (AKS) cluster, you have many ways of interacting with the cluster. I will show the two I’m most familiar with here.

To Resolve:

  1. If you haven’t already, install kubectl and k9s on your machine locally

  2. First we will cover k9s since it is a console program for interacting with clusters:

    • main commands:
    • ctrl+a / :alias => shows all reasources for a cluster
    • :q / Ctrl+c => quit
    • :ctx => shows context
    • :node => navigates to a resource like all nodes
    • d describe
    • v view
    • e edit
    • l logs
    • Reference all cli options here
  3. From the k9s site:

    # List all available CLI options
    k9s help
    # Get info about K9s runtime (logs, configs, etc..)
    k9s info
    # Run K9s in a given namespace.
    k9s -n mycoolns
    # Run K9s and launch in pod view via the pod command.
    k9s -c pod
    # Start K9s in a non default KubeConfig context
    k9s --context coolCtx
    # Start K9s in readonly mode - with all modification commands disabled
    k9s --readonly
  4. So that is a tool to get familiar with, but more important is to get familiar with kubectl

    • First, let’s look at context commands
    kubectl config view # Show Merged kubeconfig settings.
    kubectl config get-contexts                          # display list of contexts
    kubectl config current-context                       # display the current-context
    kubectl config use-context my-cluster-name           # set the default context to my-cluster-name
    • Get all deployments: kubectl get deployments --all-namespaces=true
    • Get all pods: kubectl get pods -A
    • Follow the link above and this link for a full list of commands. Only way to learn is to practice!
  5. To Do: Use kubelogin for authentication without the --admin credentials. See this post for details.

  6. List IAM roles at Resource level: az role assignment list --scope /subscriptions/<subscriptionID>/resourceGroups/<resourcegroupname>/providers/Microsoft.ContainerRegistry/registries/<acrname> -o table

  7. Attach Azure Container Registry (ACR) to AKS: az aks update -n <myAKSCluster> -g <myResourceGroup> --attach-acr <acr-resource-id>

  8. Service principle used for AKS: az aks show --resource-group <myResourceGroup> --name <myAKSCluster> --query servicePrincipalProfile.clientId -o tsv

  9. If in a test environment, stop the cluster after testing to save resources: az aks stop --resource-group my-cluster-rg --name my-cluster

  10. Full List of AKS commands for reference.