Setup K8s cluster on Azure

Kubernetes (K8s) has become the industry-standard platform for container orchestration,  offering efficient management of containerized applications. Microsoft Azure offers a managed Kubernetes service called Azure Kubernetes Service (AKS), which simplifies the deployment, scaling, and management of Kubernetes clusters. 

Let’s roll up our sleeves and dive into the process of creating a Kubernetes (K8s) cluster on Azure Kubernetes Service (AKS) using the command line. In this tutorial, you’ll learn how to set up a fully functional AKS cluster, ready to deploy and manage containerized applications in the cloud.

Prerequisite:

  • Azure account login: Follow the steps to create a free account from https://portal.azure.com if you don’t have one.
  • kubectl installation: The Kubernetes command-line tool kubectl is used for interacting with Kubernetes clusters. Install it from here if you don’t have on your machine.

Steps to create AKS cluster

Follow the steps given below to create and set up the k8s cluster on Azure and deploy an application to verify the setup.

Step 1: Install Azure CLI

To install azure CLI on windows,

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -ArgumentList '/I AzureCLI.msi /quiet' -Wait; rm .\AzureCLI.msi

To install azure CLI on MacOs,

brew install azure-cli

To install azure CLI on Ubuntu,

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Verify the installation,

az --version

Step 2: Login to your Azure Account

Run the following command to login,

az login

A browser window will open, asking you to sign in. Give your credentials & come back to the terminal/shell. 

Step 3: Set the Default Azure Subscription

If you have multiple subscriptions, set the default subscription that you want to use for creating the Kubernetes cluster. 

To list all available subscriptions,

az account list --output table

You will get a subscription id from azure portal as well.

az account set --subscription <your-subscription-id>

Step 4: Create a Resource Group

In Azure, a resource group is a container that holds related resources for your Azure solution. Create a resource group using the following command. Choose your nearest physical location from the list.

az account list-locations --query “[*].name”

Give the resource group name as per your choice,

az group create --name $RESOURCE_GROUP --location $LOCATION

Step 5: Create a Kubernetes Cluster

List VM sizes in a region,

az vm list-sizes --location $LOCATION --output table

To check the usage and limits of VM sizes for your subscription, you can run the following command

az vm list-usage --location $LOCATION --output table

Next, you’ll want to choose the most suitable instance type for your cluster. For example, the Standard D2s_v3 instance offers a good balance of cost and performance for general workloads.

Run the following command to create a cluster. you can change the credentials as per your requirements.

az aks create \
  --resource-group $RESOURCE_GROUP  \
  --name $CLUSTER_NAME \
  --node-count 2 \ 
  --node-vm-size $INSTANCE_TYPE \
  --location $LOCATION \
  --enable-addons monitoring \
  --enable-managed-identity \
  --load-balancer-sku standard \
  --generate-ssh-keys

here, we have created a cluster with 2 worker nodes, enabled monitoring to get live logs, and added a load balancer service to expose our application.

Step 6: Connect to the cluster

Our next step is important as it allows us to connect with the cluster. Run the following command to get the config file.

az aks get-credentials --resource-group  $RESOURCE_GROUP --name $CLUSTER_NAME

This command downloads the cluster configuration and merges it with your local kubectl configuration. You can now interact with your Kubernetes cluster (AKS) using kubectl.

To verify that you are connected to the cluster, run:

kubectl get nodes

You should see the list of nodes in your cluster. Now you are ready to work with your AKS cluster.

Deploy an rsvp application on your cluster

You can deploy any application on AKS. We are deploying rsvp application in our cluster. Copy the source code for the backend in your current working directory.

Apply mongo-db as a backend for this rsvp application.

kubectl apply -f backend.yaml

Now, copy the source code for the frontend in your current working directory. Apply rsvp frontend to get the UI of the application.

kubectl apply -f frontend.yaml

List the pods,

kubectl get pods

Now, list the services:

kubectl get svc

Get Load Balancer External IP and expose your application. 

Figure 1: RSVP Application UI

Our first application is deployed. Now open the azure portal and verify all the resources we have created. 

Verify all your resources and setup on azure portal

Go to your azure portal and navigate to the resources. Click on kubernetes service and see all the available details of the cluster. View other resources like resource group, subscription etc.

Figure 2: Azure portal view

Select Kubernetes resources to view the details.

Conclusion

In this guide, we discussed how to create an Azure Kubernetes Service (AKS) cluster using the Azure CLI. We covered everything from creating a resource group, setting up the cluster, deploying an application. AKS provides a powerful and fully managed Kubernetes solution, simplifying container orchestration and management in Azure. Control plane will be taken care of by azure only.

Now that you have a working AKS cluster, you can explore more advanced topics like using Azure Container Registry (ACR) for managing container images, Azure key vault to manage secrets, monitoring your application using App-insights, configure Ingress to route traffic efficiently based on predefined rules, such as URL paths or hostnames etc. Let’s get your hands dirty with Azure Kubernetes Service and experience the power of Kubernetes in the cloud! 

Join Our Newsletter

Share this article:

Table of Contents