Deepak Singh
3 min readAug 15, 2023

Implementing Kubernetes in Azure with AKS: A starters Guide

Kubernetes has become the de facto standard for container orchestration, allowing developers to manage, scale, and deploy containerized applications seamlessly. Microsoft Azure offers Azure Kubernetes Service (AKS) as a managed Kubernetes service, simplifying the process of deploying and managing Kubernetes clusters. In this article, we’ll dive into the concepts of creating and managing AKS using Terraform and Azure Pipelines, along with deploying a sample application on top of the AKS cluster.

Table of Contents

1. Introduction to AKS
2. Creating AKS using Terraform
3. Setting Up Azure Pipelines for AKS Deployment
4. Inspecting AKS Cluster Attributes
5. Deploying and Running a “Hello World” Application on AKS

1. Introduction to AKS

Azure Kubernetes Service (AKS) is a managed Kubernetes offering from Microsoft Azure. It provides a simplified way to deploy, manage, and scale containerized applications using Kubernetes. AKS abstracts away the complexities of cluster management, such as server maintenance, updates, and scaling, allowing developers to focus on application development.

2. Creating AKS using Terraform

Terraform is an infrastructure-as-code tool that allows you to define and manage your infrastructure in a declarative manner. Let’s walk through the process of creating an AKS cluster using Terraform.

Prerequisites

- Azure subscription
- Azure CLI installed
- Terraform installed

Terraform Configuration

Create a file named `main.tf` and add the following code to define the AKS cluster:


# Provider block for Azure Resource Manager
provider “azurerm” {
features {}
}
# Define the Azure Resource Group
resource "azurerm_resource_group" "aks_rg" {
name = "my-aks-rg"
location = "East US"
}
# Define the AKS Cluster
resource "azurerm_kubernetes_cluster" "aks_cluster" {
name = "my-aks-cluster"
location = azurerm_resource_group.aks_rg.location
resource_group_name = azurerm_resource_group.aks_rg.name
dns_prefix = "myaks"
# Define Linux admin profile for the AKS nodes
linux_profile {
admin_username = "aksuser"
ssh_key {
key_data = file("~/.ssh/id_rsa.pub")
}
}
# Define agent pool profile for the AKS nodes
agent_pool_profile {
name = "default"
count = 2
vm_size = "Standard_D2_v2"
os_disk_size_gb = 30
}
# Define service principal for the AKS cluster
service_principal {
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
}
# Define tags for the AKS resources
tags = {
environment = "dev"
}
}

In the above configuration, replace `YOUR_CLIENT_ID` and `YOUR_CLIENT_SECRET` with your Azure AD service principal credentials.

Creating AKS Cluster

Initialize Terraform:

terraform init

Provision of the AKS cluster:

 terraform apply

Setting Up Azure Pipelines for AKS Deployment

Azure Pipelines is a continuous integration and continuous deployment (CI/CD) service that can automate the deployment of applications to AKS clusters.

— -Prerequisites

- Azure DevOps account
- Azure Pipelines configured

— -Azure Pipelines Configuration

1. Create a new pipeline in Azure Pipelines.

2. Choose your source repository (e.g., GitHub, Azure Repos).

3. Configure your pipeline YAML file (`azure-pipelines.yml`) with the following stages:

trigger:
- main
stages:
- stage: TerraformDeploy
jobs:
- job: TerraformJob
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
addToPath: true
- script: |
cd terraform-folder
terraform init
terraform apply -auto-approve
displayName: 'Terraform Apply'


- stage: DeployToAKS
jobs:
- job: DeployJob
steps:
- task: UseKubectl@1
inputs:
kubectlVersion: '1.21.0'
- script: |
kubectl apply -f app.yaml
displayName: 'Deploy Sample App'

4. Inspecting AKS Cluster Attributes

You can use the Azure CLI to inspect various attributes of your AKS cluster.

  1. Get AKS credentials:
az aks get-credentials — resource-group my-aks-rg — name my-aks-cluster

2. List AKS nodes:

 kubectl get nodes

3. Get AKS cluster information:

az aks show — resource-group my-aks-rg — name my-aks-cluster — output table

5. Deploying and Running a “Hello World” Application on AKS

Let’s deploy a simple “Hello World” application to your AKS cluster.

  1. Create a file named `app.yaml` with the following content:
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec:
containers:
— name: hello
image: busybox:1.28
command: [‘echo’, ‘Hello, World!’]

2. Deploy the application:

kubectl apply -f app.yaml

3. Verify the deployment:

 kubectl get pods
kubectl logs hello-world

Conclusion

In this article, we explored the concepts of creating and managing an Azure Kubernetes Service (AKS) cluster using Terraform and deploying a simple “Hello World” application on top of the AKS cluster. We covered the process of setting up Azure Pipelines for CI/CD, inspecting AKS cluster attributes, and deploying and verifying a Kubernetes application. By following these steps, you can efficiently manage your containerized applications on Azure using AKS.

References: Microsoft Azure Kubernetes Service for Image : https://learn.microsoft.com/en-us/azure/architecture/microservices/logging-monitoring