Azure : Create AKS Cluster and deploy an application

Reading Time: 5 mins

Overview

Azure Kubernetes Service (AKS) is Microsoft Azure’s managed Kubernetes solution. It competes with both Google Kubernetes Engine (GKE) and Amazon Elastic Kubernetes Service (EKS). In this post, I will walk you through different ways of creating a cluster.

Install AZ CLI, Kubectl

In this section, Install the required CLI’s in your local machine which will be used to connect to AKS clusters once deployed.

  • Download and install the AZ command line tool at its install page. It will help you create and communicate with  AKS cluster. Once cli is successfully installed, configure the same using below steps:
# Authenticate to Azure 

$ az login
  • kubectl is the command-line client for directly interacting with a Kubernetes cluster. The Azure CLI provides a sub-command that will install kubectl for you or can be installed by following the steps here.
az aks install-cli

Create AKS cluster through console

  • Login to Azure portal > Search for resources > Kubernetes services > Create > Create a Kubernetes cluster

  • Use existing resource group or create a new one as shown below:

  • Cluster details:
      • Cluster present configuration – select Standard or Dev etc .. from drop down, I choose standard for this demo purpose
      • Kubernetes cluster name: Give a name # Ex: captain-aks-cluster-1
      • Region: Select a region # (Asia Pacific) South India
      • Kubernetes Version: select default or any other version
  • Primary node pool:
      • Node size: Leave to default or select a diff size
      • Scale method: leave to default – Autoscale
      • Node count range: provide min and max num of nodes and click on Next: Node pools

  • Node pools: You can add node pools, for this demo I didn’t add any pools.
  • Authentication: select Service principal
      • Open a new tab > Login to Azure portal > search for Azure Active Directory > App registrations > select the principal (follow the steps here to create a new service principal)
      • Click on service principal > Overview > Application (client) ID : This is the Service principal client ID
      • In the same page > Manage > Certificates & Secrets > New client secret > Add > copy the value and save it in a safe location for later use. This is the Service principal client secret
  • Review + Create 
  • Create

Once the cluster is successfully created, cluster status should turn Succeeded (Running)

 

Create AKS cluster through CLI

Note: This section is optional if you also want to deploy AKS cluster using terminal. If you have already deployed a cluster through console then jump to next section to deploy a test application.

# Create a resource group

$ az group create -l southindia -n captain-demo-RG

{
"id": "/subscriptions/8272-4e6b-aa9b-45d6455244/resourceGroups/captain-demo-RG",
"location": "southindia",
"managedBy": null,
"name": "captain-demo-RG",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}

#Syntax to create AKS cluster:
az aks create -g <resource-group> -n <k8s-cluster-name> --generate-ssh-keys

# Example
$ az aks create -g captain-demo-RG -n captain-aks-cluster-2 --node-count 2 --generate-ssh-keys

# Syntax to get fetch credentials
az aks get-credentials -g <resource-group> -n <k8s-cluster-name>

# Example:
$ az aks get-credentials -g captain-demo-RG -n captain-aks-cluster-1

# get the nodes in AKS cluster:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-19011979-vmss000000 Ready agent 5m49s v1.21.7
aks-nodepool1-19011979-vmss000001 Ready agent 5m54s v1.21.7

Create ACR (Optional):

In this demo, I will be using ACR (Azure Container Registries) to store the images. You can deploy the application with images located in any registry of your choice.

  • Login to Azure portal > Container registries > Create container registry
  • In Resource Group, create new as shown below and give it a name

  • Give a registry name and select the Location from drop down.

  • Review + Create 
  • Create
# Authenticate to repo, syntax: az acr login --name myregistry

$ az acr login --name captainrepo
Login Succeeded

# Tag Image, syntax: docker tag <local image>:<version> <registryname>.azurecr.io/<reponame>:<version>

$ docker tag 778018584600.dkr.ecr.ap-south-1.amazonaws.com/tbs-spring-image captainrepo.azurecr.io/tbs-spring-image:latest

# Push the image to ACR repo:

$ docker push captainrepo.azurecr.io/tbs-spring-image:latest
  • Login to Azure portal > Container registries > click on registry > Repositories
  • Image pushed in previous step can be seen here

Deploy an Application

  • As the cluster is deployed in previous step, now let’s understand how to connect.
  • Click on deployed AKS cluster > Connect

  • Run the commands that is displayed on right pane of the page once Connect option is clicked. I have also shown examples below:
# Set the subscription, Syntax: 

az account set --subscription <Subscription ID>

# Fetch the credentials

az aks get-credentials --resource-group captain-aks-demo --name captain-aks-cluster-1
Merged "captain-aks-cluster-1" as current context in /Users/reddye/.kube/config

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-agentpool-51633727-vmss000000 Ready agent 5m27s v1.21.7
# Create a deployment

$ kubectl create deployment spring-deploy --port=8080 --image=captainrepo.azurecr.io/tbs-spring-image:latest --replicas=2

deployment.apps/spring-deploy created

# Expose the deployment

$ kubectl expose deployment spring-deploy --port=8080 --type=LoadBalancer

service/spring-deploy exposed

# Collect the External IP of service spring-deploy

$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 41m
spring-deploy LoadBalancer 10.0.32.224 20.41.216.209 8080:30684/TCP 94s