Introduction

Azure Kubernetes Service (AKS) simplifies the deployment, management, and scaling of containerized applications using Kubernetes. To interact with an AKS cluster, you need to establish a connection using kubectl, the Kubernetes command-line tool. This guide provides a step-by-step process to explain How To Connect Azure Kubernetes Cluster Using Kubectl

Why Kubectl needed?

Connecting to an AKS cluster is an essential step for managing workloads, monitoring performance, and deploying applications. This process is especially critical for:

  • Monitoring Cluster Health: Using kubectl commands to retrieve performance metrics and check node status.
  • Application Deployment: Deploying and managing containerized applications in the AKS cluster.
  • Cluster Administration: Performing administrative tasks like scaling, updating, or debugging resources within the cluster.

Whether you’re a developer or administrator, establishing this connection ensures you can effectively manage your Kubernetes environment.

To connect to an Azure Kubernetes Service (AKS) cluster using kubectl, you will need to perform the following steps:

Prerequisites ( install both Azure CLI and Kubectl)

STEP: 1 Install the Azure CLI

If you haven’t installed the Azure CLI on your local machine, you can download and install it from the official Microsoft documentation here.

STEP: 2 Install Kubectl

Install the Kubernetes command-line (click here) tool “Kubectl” , if you haven’t installed it already.

Steps to connect Azure account:

STEP: 3 Authenticate with Azure

Open command prompt, run az login command to authenticate your CLI with your Azure account. Once you run this command, you will be prompted to enter your Azure account credentials.

Steps to connect Azure AKS Cluster:

Go to Azure Portal -> Kubernetes Services -> Select the required Cluster -> Overview -> Connect -> to find the entire command for the specific cluster itself or follow the below commands one by one by replacing with subscription Id, cluster name and resource group name.

connect AKS cluster using Kubectl

STEP: 4 Set the subscription

To set subscription, run

STEP: 5 Generate kubeconfig file

Open command prompt, run the below command and run az aks get-credentials command to connect to your AKS cluster. The get-credentials command downloads credentials and configures the Kubernetes CLI to use them.

Replace <resource-group-name> with the name of the resource group that contains your AKS cluster, and <cluster-name> with the name of your AKS cluster.

Above command will create the kubeconfig file in the user root directory. To get kubeconfig file in the specific location

Connect Azure Kubernetes Cluster Using Kubectl

STEP: 6 Verify the connection

To verify that Kubectl is connected to your AKS cluster, run the Kubectl get nodes command. This command should display a list of the nodes in your AKS cluster. If the command returns the list of nodes, then you have successfully connected to your AKS cluster using Kubectl.

Connect Azure Kubernetes Cluster Using Kubectl

Points to Remember

  • Go to Azure Portal -> Kubernetes Services -> Select the required Cluster -> Overview -> Connect -> to find the entire command for the specific cluster itself or follow the below commands one by one by replacing with subscription Id, cluster name and resource group name.
  • First we need to login to the Azure account by configuring Azure subscription and login details. And then we need to connect to Kubernetes Cluster only then we can able to run Kubectl commands.

Conclusion

Connecting to an AKS cluster using kubectl is a fundamental skill for managing Kubernetes workloads in Azure. By following this guide, you can authenticate, configure, and verify your connection to the AKS cluster seamlessly. This enables you to monitor cluster performance, deploy applications, and manage resources effectively.

As Kubernetes continues to be a vital platform for container orchestration, mastering tools like kubectl and Azure CLI is essential for efficient cluster management.