Cyberithub

8 Easy Steps to Create an AKS Cluster using Azure CLI

Advertisements

In this article, we will go through the steps to create an AKS Cluster using Azure CLI. Azure Kubernetes Service(AKS) is a managed Kubernetes Service offered by Microsoft on Azure Cloud. It completely removes the overhead of creating and managing complex Kubernetes environment. Moreover, AKS fully integrates with other Azure services which means you can deploy almost any kind of applications on AKS Cluster. Apart from this, AKS will have all the Kubernetes features available which further simplifies the Cluster Management. More about Azure Kubernetes Service(AKS).

8 Easy Steps to Create an AKS Cluster using Azure CLI

Steps to Create an AKS Cluster using Azure CLI

Also Read: Deploy Application war file to Tomcat Using Jenkins in 9 Simple Steps 

Step 1: Login to Azure Account

First you need to login to your Microsoft Azure account using az login command as shown below. This command will open a web browser and will ask to provide your valid Azure account and password. Once you logged in, it will show your subscription details as below.

C:\>az login
The default web browser has been opened at https://login.microsoftonline.com/common/oauth2/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "372cb21a-688a-4613-b110-9fb34d646be3",
    "id": "8d73fb68-2c34-4f8c-8014-c077617e7e32",
     "isDefault": true,
     "managedByTenants": [],
     "name": "Free Trial",
     "state": "Enabled",
     "tenantId": "372cb21a-688a-4613-b110-9fb34d646be3",
      "user": { 
            "name": "admin@cyberithub.com",
            "type": "user"
            }
       }
]

 

Step 2: Create Resource Group

Before creating a Kubernetes Cluster, you need to create a Resource Group. Here we are creating a Resource Group name TestResourceGroup using az group create --name TestResourceGroup --location eastus command as shown below.

C:\>az group create --name TestResourceGroup --location eastus
{
 "id": "/subscriptions/8d73fb68-2c34-4f8c-8014-c077617e7e32/resourceGroups/TestResourceGroup",
 "location": "eastus",
 "managedBy": null,
 "name": "TestResourceGroup",
 "properties": {
 "provisioningState": "Succeeded"
   },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

 

Step 3: Create an AKS Cluster

Next step is to create a single node AKS Cluster under Resource Group TestResourceGroup by using az aks create --resource-group TestResourceGroup --name TestAKSCluster --node-count 1 --generate-ssh-keys command as shown below. As you can see, this command will create an AKS cluster and display the status on JSON format.

C:\>az aks create --resource-group TestResourceGroup --name TestAKSCluster --node-count 1 --generate-ssh-keys
SSH key files 'C:\Users\cyberithub\.ssh\id_rsa' and 'C:\Users\cyberithub\.ssh\id_rsa.pub' have been generated under ~/.ssh to allow SSH access to the VM. If using machines without permanent storage like Azure Cloud Shell without an attached file share, back up your keys to a safe location
Resource provider 'Microsoft.ContainerService' used by this operation is not registered. We are registering for you.
Registration succeeded.
{
  "aadProfile": null,
  "addonProfiles": null,
  "agentPoolProfiles": [
  {
    "availabilityZones": null,
    "count": 1,
    "enableAutoScaling": null,
    "enableEncryptionAtHost": false,
    "enableFips": false,
    "enableNodePublicIp": false,
    "enableUltraSsd": false,
    "gpuInstanceProfile": null,
    "kubeletConfig": null,
    "kubeletDiskType": "OS",
    "linuxOsConfig": null,
    "maxCount": null,
    "maxPods": 110,
    "minCount": null,
    "mode": "System",
    "name": "nodepool1",
    "nodeImageVersion": "AKSUbuntu-1804gen2containerd-2021.07.25",
    "nodeLabels": {},
.......................................................................

 

Step 4: Install Kubectl Utility

To connect with AKS cluster, you will need to have kubectl utility installed in your System. You can install this utility by simply using az aks install-cli command as shown below.

C:\>az aks install-cli
Downloading client to "C:\Users\cyberithub\.azure-kubectl\kubectl.exe" from "https://storage.googleapis.com/kubernetes-release/release/v1.22.0/bin/windows/amd64/kubectl.exe"
Please add "C:\Users\cyberithub\.azure-kubectl" to your search PATH so the `kubectl.exe` can be found. 2 options:
1. Run "set PATH=%PATH%;C:\Users\cyberithub\.azure-kubectl" or "$env:path += 'C:\Users\cyberithub\.azure-kubectl'" for PowerShell. This is good for the current command session.
2. Update system PATH environment variable by following "Control Panel->System->Advanced->Environment Variables", and re-open the command window. You only need to do it once
Downloading client to "C:\Users\cyberithub\AppData\Local\Temp\tmpe8x6vodw\kubelogin.zip" from "https://github.com/Azure/kubelogin/releases/download/v0.0.10/kubelogin.zip"
Please add "C:\Users\cyberithub\.azure-kubelogin" to your search PATH so the `kubelogin.exe` can be found. 2 options:
1. Run "set PATH=%PATH%;C:\Users\cyberithub\.azure-kubelogin" or "$env:path += 'C:\Users\cyberithub\.azure-kubelogin'" for PowerShell. This is good for the current command session.
2. Update system PATH environment variable by following "Control Panel->System->Advanced->Environment Variables", and re-open the command window. You only need to do it once

 

Step 5: Set Variables Path

You can either set the PATH variable from Environment Variables section or set it for just this session by using below set PATH command. But it is worth to remember that below setting is just a temporary set. In case you think your current session can be disconnected then it is better to set your path using $PATH environment variable from "Control Panel->System->Advanced->Environment Variables" .

C:\>set PATH=%PATH%;C:\Users\cyberithub\.azure-kubectl
C:\>set PATH=%PATH%;C:\Users\cyberithub\.azure-kubelogin

 

Step 6: Connect to AKS Cluster

You will be needing the Kube config file to connect your AKS Cluster. So to get the config file, you need to use az aks get-credentials --resource-group TestResourceGroup --name TestAKSCluster command as shown below. This command will pull kube config file in the local user home directory as you can see from below output.

C:\>az aks get-credentials --resource-group TestResourceGroup --name TestAKSCluster
Merged "TestAKSCluster" as current context in C:\Users\cyberithub\.kube\config

 

Step 7: Verify Your Connection

You can verify connection to your AKS Cluster by using kubectl get nodes command. This command will show all the nodes in the Cluster. Since we had created a single node cluster so we will only see one node on the below output.

C:\>kubectl get nodes
NAME                              STATUS ROLES AGE   VERSION
aks-nodepool1-35495379-vmss000000 Ready  agent 5m42s v1.20.7

 

Step 8: Deploy Sample Application

Now that our AKS cluster is ready and available, we can quickly deploy a sample application. Here we are creating a Nginx deployment and deploying it on our Cluster using kubectl create command as shown below.

C:\>kubectl create -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/controllers/nginx-deployment.yaml
deployment.apps/nginx-deployment created

Once deployment is created, you can check its status by using kubectl get deployment command as shown below.

C:\>kubectl get deployment
NAME             READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3        3         3      9s

Leave a Comment