Deploy Azure Kubernetes Service Using Powershell

If you would like to automate deployment of an Azure Kubernetes Service (AKS) and struggling with various commands to be used , this article will help you out. In this article, you’re going to learn how to build an AKS cluster from scratch using PowerShell and learn some best practices on how to approach set up.

Azure Kubernetes Service (AKS) is a Microsoft Azure-hosted fully managed container orchestration service, based on the open-source Kubernetes system. It provides flexibility, automation and reduced management overhead for administrators and developers.

Azure Kubernetes Service (AKS) provides below features:

  • Configuration of masters and nodes during the deployment process
  • Manages upgrades on cluster nodes
  • Integrates Azure Active Directory integration
  • Connections to monitoring services
  • Configuration of advanced networking features, such as HTTP application routing.
  • Auto scaling of resources when demand peaks
  • Integrates with the Azure Container Registry (ACR) for Docker image storage

Pre-requisites

SSH Key Generation

  • Open cmd prompt as Administrator
  • Run below command
ssh-keygen -t rsa -b 2048 -f "<Location>/myprivatekey"

Sample Application

apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-back
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-back
  template:
    metadata:
      labels:
        app: azure-vote-back
    spec:
      containers:
      - name: azure-vote-back
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-back
spec:
  ports:
  - port: 6379
  selector:
    app: azure-vote-back
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-front
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-front
  template:
    metadata:
      labels:
        app: azure-vote-front
    spec:
      containers:
      - name: azure-vote-front
        image: microsoft/azure-vote-front:v1
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        env:
        - name: REDIS
          value: "azure-vote-back"
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: azure-vote-front

Commands

Let’s use various commands to deploy AKS

Install Azure Kubernetes Module in Powershell

#Install Azure Kubernetes Service
Install-Module "az.aks"

Connect to Azure Account

#Connect to Azure Account
Connect-AzAccount
#$password = "<pwd>" | ConvertTo-SecureString -asPlainText -Force
#$username = "<username>" 
#$credential = New-Object System.Management.Automation.PSCredential($username,$password)
#Connect-AzAccount -Credential $credential -ServicePrincipal -Tenant "<TenantID>"

Get Azure Subscription

#Get Azure Subscription
Get-AzSubscription

Select Azure Subscription

#Select Azure Subscription
Select-AzSubscription -SubscriptionId "<Subscription ID>"

Create New Resource Group

#Create New resource group
$resourceGroupName = "AKSDemoRG";
$ResGrp = New-AzResourceGroup $resourceGroupName -location 'East US'

Create Azure Kubernetes Service

#Create Azure Kubernetes Service 
$KubernetesSvcName = "DemoKubernetesSvc";
$AKSvc = New-AzAks -KubernetesVersion 1.15.7 -ResourceGroupName $ResGrp.ResourceGroupName -Name $KubernetesSvcName -SshKeyValue "<Location>/myprivatekey.pub"
Write-Host "Azure Kubernetes Service: $AKSvc created."

Update Azure Kubernetes Service

#Update Azure Kubernetes Service
Set-AzAks -ResourceGroupName $ResGrp.ResourceGroupName -Name $KubernetesSvcName -NodeCount 5
Write-Host "Azure Kubernetes Service udpated."

Create Container Group

#Create Container Group
$containername = "demo-container"
New-AzContainerGroup -ResourceGroupName $resourceGroupName -Name $containername -Image mcr.microsoft.com/windows/servercore/iis:nanoserver -OsType Windows -DnsNameLabel aci-demo-win01

Get Container Group

#Get Container Group
Get-AzContainerGroup -ResourceGroupName $resourceGroupName -Name $containername

Install kubectl to deploy sample Application to AKS Cluster

#Install kubectl to deploy sample application to AKS Cluster
Install-Script -Name install-kubectl -Scope CurrentUser -Force 
Import-AzAksCredential -ResourceGroupName $resourceGroupName -name $KubernetesSvcName
install-kubectl.ps1

Deploy Sample Application to AKS Cluster

kubectl.exe apply -f "<Location>\azure-vote.yaml"

Remove Kubernetes Service

#Remove Kubernetes Service
Remove-AzAks -Name $KubernetesSvcName -ResourceGroupName $resourceGroupName -Force

Remove Container Group

#Remove Container Group
Remove-AzContainerGroup -ResourceGroupName $resourceGroupName -Name $containername -Force

Remove Resource Group

#Remove Resource Group
Remove-AzResourceGroup -Name $resourceGroupName -Force

In this project, you successfully deployed an AKS cluster and ran an application on top of that cluster using Powershell. There are a plethora of additional features to dig into when it comes to AKS and you’ve only just scratched the surface.



Categories: Automation, Azure, Azure Kubernetes Service, Containers, Powershell

Leave a Reply