# Connect to a Kubernetes Cluster from the Azure CLI

In 
Published 2022-12-03

This tutorial explains how we can connect to the Kubernetes Cluster from the Azure CLI.

Prerequisites:

  • Install Azure CLI on Windows

Here are the steps for connecting to an Azure Kubernetes Cluster from the Azure CLI

1. Connect to Azure using the Azure CLI

We need to run the following command:

az login

2. Set the subscription if we have more than one

We need to run the following command:

az account set --subscription 69045ea7-6500-45f7-adc6-b4c86ce7233b

3. Get the credential for accessing the K8s cluster

We need to run the following command:

az aks get-credentials --resource-group K8s-rg1 --name K8s-Cluster1

Once you have run the command above to connect to the cluster, you can run any kubectl commands. Here are a few examples of useful commands you can try.

# List all deployments in all namespaces
kubectl get deployments --all-namespaces=true
# List all deployments in a specific namespace
# Format :kubectl get deployments --namespace <namespace-name>
kubectl get deployments --namespace kube-system
# List details about a specific deployment
# Format :kubectl describe deployment <deployment-name> --namespace <namespace-name>
kubectl describe deployment my-dep --namespace kube-system
# List pods using a specific label
# Format :kubectl get pods -l <label-key>=<label-value> --all-namespaces=true
kubectl get pods -l app=nginx --all-namespaces=true

If all these commands run successfully, that means that you are able to connect to a Kubernetes cluster in Azure.