Getting started with Azure Kubernetes Service (AKS)

Reading Time: 7 minutes


In this article, we will go through how to create an Azure Kubernetes Service (AKS) cluster. In this article, you’re going to learn how to build an AKS cluster from scratch and learn how to deploy a sample application on Kubernetes.

Azure Kubernetes Service (AKS) is a managed container orchestration service, based on the open-source Kubernetes system, which is available on the Microsoft Azure public cloud. An organization can use AKS to deploy, scale and manage Docker containers and container-based applications across a cluster of container hosts

There are a few ways to create an AKS cluster but in this tutorial, you’re going to get your hands dirty by creating the cluster with the Azure CLI in Azure Cloud Shell.


Prerequisites

To get your hands dirty, all you need is a Microsoft Azure subscription. When you signup for Azure subscription, Microsoft offers you a $200 credit, which you can use to try Azure Kubernetes Service (AKS).

Once you have your subscription ready, let’s move to next step for cloud shell.


Launching Azure Cloud Shell

In this tutorial, you will be using the Azure Cloud Shell to simplify the setup requirements. Cloud Shell comes with many common tools already installed, including the Azure CLI and the Kubernetes command-line tool, kubectl.

First, launch Azure Cloud Shell by clicking on the Cloud Shell icon from the Azure Portal. You can also open a Cloud Shell terminal directly by going to shell.azure.com.

If this is your first time loading Cloud Shell, you will be prompted to select Bash or PowerShell for your experience, as you can see below. For this Project, select Bash since many kubectl-related commands and tools expect a Linux type shell for proper execution.

The next prompt will ask you to create a storage account to use for persistent storage of files stored in Cloud Shell. Simply click Create storage to continue, as shown below.

After a brief moment, the Cloud Shell terminal will connect and a Bash prompt will appear, as shown below.


Configuring the Azure CLI

Now that you are logged into Cloud Shell, it’s time to prepare your environment to build an AKS cluster.

Select the Correct Subscription

The first step is to ensure you’re using the correct subscription. After all, you may have more than one subscription associated with your user account. The example below uses the az account show the command to show which subscription is currently selected.

ronak@Azure:~$ az account show
{
  "environmentName": "AzureCloud",
  "homeTenantId": "000000-0000-0000-0000-000000000000",
  "id": "000000-0000-0000-0000-000000000000",
  "isDefault": true,
  "managedByTenants": [],
  "name": "Azure subscription 1",
  "state": "Enabled",
  "tenantId": "000000-0000-0000-0000-000000000000",
  "user": {
    "cloudShellID": true,
    "name": "live.com#someemail@gmail.com",
    "type": "user"
  }
}


Creating a Resource Group for Your Azure Kubernetes Cluster

Next, you have to create a resource group or use an existing one. To easily manage and remove this resource group when done with this Project, let’s create your own. All resources created in Microsoft Azure must reside in a resource group.

In this step, you will create a resource group to contain the AKS cluster, and specify which region the resource group will be created in.

you can get your Region list using below command

az account list-locations --query "sort_by([].{DisplayName:displayName, Name:name}, &DisplayName)" --output table

Once you have your region, the example below shows the creation of a resource group named k8s in the southeastasia region.

az group create --name k8s --location southeastasia
{
  "id": "/subscriptions/000000-0000-0000-0000-000000000000/resourceGroups/k8s",
  "location": "southeastasia",
  "managedBy": null,
  "name": "k8s",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}


Configuring the AKS Cluster

In the previous section, you created a resource group to store the AKS cluster. Now it’s time to create the AKS cluster itself.


Picking a Kubernetes API Version

There are a number of configuration options that can be submitted when creating an AKS cluster, the available Kubernetes API versions may differ depending on the region that the AKS cluster will be residing in.

To learn what Kubernetes API versions are available in each region, run the following AZ CLI command:

az aks get-versions --location southeastasia -o table

Output will look something like:

KubernetesVersion    Upgrades
-------------------  -------------------------------------------------
1.18.2(preview)      None available
1.18.1(preview)      1.18.2(preview)
1.17.5(preview)      1.18.1(preview), 1.18.2(preview)
1.17.4(preview)      1.17.5(preview), 1.18.1(preview), 1.18.2(preview)
1.16.9               1.17.4(preview), 1.17.5(preview)
1.16.8               1.16.9, 1.17.4(preview), 1.17.5(preview)
1.15.11              1.16.8, 1.16.9
1.15.10              1.15.11, 1.16.8, 1.16.9
1.14.8               1.15.10, 1.15.11
1.14.7               1.14.8, 1.15.10, 1.15.11

Creating the AKS Cluster

Now that you have all the prerequisite work complete, it is time to create your AKS cluster!

If you take a look at the AZ CLI documentation for creating an AKS cluster, you’ll see a ton of parameters, most of which are optional. After looking at the documentation, go back to the terminal.

The command below will show the most important parameters to get an AKS cluster up and running with Kubernetes API version 1.17.5. The AZ CLI resource that will be used is az aks. Enter the correct resource group name and AKS cluster name, then run the command line below to create an AKS cluster with one worker node:

az aks create --resource-group k8s --name name_of_aks_cluster --node-count 1 --kubernetes-version 1.17.5 --generate-ssh-keys

The parameters used are:

--resource-group = Resource group that you created in the previous section.

--name = The name of the AKS cluster you want to create.

name-count 1 = The number of worker nodes that will be running. In a dev environment, one node is fine. For redundancy and clustering purposes, all production environments should have at least two nodes.

--kubernetes-version = The Kubernetes API version that will be used in the AKS cluster.

As shown in the screenshot below, multiple JSON blocks will be printed to the terminal with all of the AKS information.


Using AZ CLI to Confirm that the AKS Cluster Exists

Now that the AKS cluster is created, you can see if the AKS cluster is up and running by using AZ CLI. Run the following command to show the existing AKS cluster table output on a terminal

az aks show --name name_of_aks_cluster --resource-group name_of_resource_group -o table

Output should look something like :

Name        Location       ResourceGroup    KubernetesVersion    ProvisioningState    Fqdn
----------  -------------  ---------------  -------------------  -------------------  ----------------------------------------------------------
rbanka-aks  southeastasia  k8s              1.17.5               Succeeded            rbanka-aks-k8s-f9fa58-bbc849f9.hcp.southeastasia.azmk8s.io


Configuring the Kubeconfig

In the previous section, you learned about the Kubernetes API version offerings from Azure, how to create an AKS cluster, and how to confirm that the AKS cluster exists. In this section, you will take that setup a step further and begin configuring it.


Retrieving your Credentials

Once the AKS cluster is built, the next step is to retrieve the credentials that will be used with kubectl. Kubectl is the command-line program that manages the cluster.

To download the credentials, query the AKS cluster by using the az aks get-credentials command specifying the resource group and name of the cluster as shown in the example below.

az aks get-credentials --name name_of_aks_cluster --resource-group name_of_resource_group
ronak@Azure:~$ az aks get-credentials --resource-group k8s --name rbanka-aks
Merged "rbanka-aks" as current context in /home/ronak/.kube/config

Validating the Cluster

The final step in creating the cluster is to validate it is running and that your credentials have been retrieved successfully. You can do this by running commands with kubectl to retrieve the current nodes. The output below shows the three worker nodes you specified during creation in the Ready state.

Kubectl get nodes

If everything is working as expected, output will be something like

ronak@Azure:~$ kubectl get nodes
NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-15007391-vmss000000   Ready    agent   7m47s   v1.17.5


If you would like to view more information about the cluster, you can run kubectl cluster-info as shown below.

ronak@Azure:~$ kubectl cluster-info
Kubernetes master is running at https://rbanka-aks-k8s-f9fa58-8390d1b8.hcp.southeastasia.azmk8s.io:443
CoreDNS is running at https://rbanka-aks-k8s-f9fa58-8390d1b8.hcp.southeastasia.azmk8s.io:443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://rbanka-aks-k8s-f9fa58-8390d1b8.hcp.southeastasia.azmk8s.io:443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.


Deploying a Demo Application

Once the AKS cluster is built, it’s time to deploy an application to it. We will be using Microsoft provided an excellent example application hosted on GitHub called Azure Voting App.

This sample creates a multi-container application in an Azure Kubernetes Service (AKS) cluster. The application interface has been built using Python / Flask. The data component is using Redis.

Let’s clone the git repository for the sample app, This file is located in the Azure Voting App GitHub repository

git clone https://github.com/Azure-Samples/azure-voting-app-redis.git

cd azure-voting-app-redis

Let’s deploy our application using

kubectl apply -f azure-vote-all-in-one-redis.yaml

Checking the Deployment Status

Now that you have deployed the application to Kubernetes, the controller will take care of creating the necessary pods and services. Let’s now monitor how the deployment is going.

The example voting application creates two pods and two services. One pod provides a backend Redis cache for storing data. The other pod hosts a website where people can submit votes. The services expose each pod’s application, one internally to the front-end and the other externally through an Azure Load Balancer.

You can check on the status of the pods by running kubectl get pods as shown in the example below.

ronak@Azure:~/azure-voting-app-redis$ kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
azure-vote-back-7dbcd6c889-n282p   1/1     Running   0          71s
azure-vote-front-d9566c557-gq4b4   1/1     Running   0          71s

Next, check on the status of the services by running kubectl get svc as shown in the example below.

You can see from the output that the cluster has three services running; two services pertaining to the application just created and one default service.

  • The azure-vote-back service has been deployed with an internal cluster IP address of 10.0.24.92.
  • The azure-vote-front service has been deployed with a load balancer that exposes the service publicly on the public IP address 20.43.182.29 and port 80.
  • The kubernetes service which is created as part of the Kubernetes cluster creation, and is not part of the deployed application.
ronak@Azure:~/azure-voting-app-redis$ kubectl get svc
NAME               TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)        AGE
azure-vote-back    ClusterIP      10.0.24.92    <none>         6379/TCP       7m32s
azure-vote-front   LoadBalancer   10.0.245.66   20.43.182.29   80:30553/TCP   7m32s
kubernetes         ClusterIP      10.0.0.1      <none>         443/TCP        43m


Accessing the Application

Now that you have prepped and created the cluster, deployed the application and then monitoring it’s progress until completion, it’s time to check out your handiwork!

On your local machine, open a browser to the public IP address allocated to the azure-vote-front service. In my instance, that IP was 20.43.182.29 found by running kubectl get svc.

you can also get the external ip using below command

kubectl get svc azure-vote-front --output jsonpath='{.status.loadBalancer.ingress[0].ip}'

If all went well, you should be greeted with the page shown below.


Congratulations! You have successfully created an AKS cluster and deployed an application.


Cleanup

In previous step we have done deploying and running an AKS cluster will cost some money, even from credits, so let’s clean up our resources.

The az group delete subcommand will delete the entire AKS cluster and any data stored on it.

ronak@Azure:$ az group delete --name k8s
Are you sure you want to perform this operation? (y/n): y


You can further read about AKS and Kubernetes from below references


References:

Ronak Banka

Ronak works as a Senior Solution architect with VMware, based out of Singapore.

Leave a Reply

Your email address will not be published. Required fields are marked *