Azure DevOps with Azure Kubernetes Service (AKS)

Azure Kubernetes Service is a managed service offering provided by Azure for customers to run microservices applications. AKS is the easiest way to create a fully functional Kubernetes cluster in a few minutes. The difference with AKS and on-premises or Kubernetes deployed on top of VMs are, with AKS Azure will manage the master nodes and as a customer, we have to take care of the worker nodes. More details can be found here.

Azure DevOps use to deploy the application through CI/CD (Continuous Integration /Continues Deployment). With the use of Azure, DevOps developers can create different pipelines for production and development and with the code commit to each brunch with Azure DevOps the application is deployed to new version. Azure DevOps helps cloud administrators, developers to automate the process of application deployments.

Prerequisites

Create Sample project for Azure DevOps

First we need an application to work with and deploy to AKS service. Azure DevOps team has provide a website that we can use to generate sample demo applications for different scenarios and technologies.

AZURE DEVOPS DEMO GENERATOR

Lets go to above link and create sample application to work with demo. Click Choose Template to select a template

Create a Project

Under DevOps select Azure Kubernetes Service

Click Create Project to create sample project

After creating the project this project visible under your DevOps organization.

Create Azure Kubernates Service cluster

we need to create a AKS cluster to deploy Kubernetes cluster to deploy the application. For this we are going to use Azure CLI.

As a first step we need to create few variables.

#!/bin/sh

#Variables

REGION="southeastasia"
RG="akslabdemo"
CLUSTER_NAME="aksdevops"
ACR_NAME="aksdemoacr1990"
SQL_SVR_NAME="aksappdbsvr"
DB_NAME="aksdb"

Get latest Kubernetes version avilable.

VERSION=$(az aks get-versions -l $REGION --query 'orchestrators[-1].orchestratorVersion' -o tsv)

Create Resource Group

az group create --name $RG --location $REGION

Create Kubernetes cluster using VM scale set and cluster autoscaler

az aks create --resource-group $RG --name $CLUSTER_NAME  --enable-addons monitoring --kubernetes-version $VERSION --generate-ssh-keys --vm-set-type VirtualMachineScaleSets --node-vm-size Standard_B2s --enable-cluster-autoscaler --node-count 1 --max-count 5 --min-count 1 --location $REGION

Create Azure Container Registry to store images build by Azure DevOps

az acr create --resource-group $RG --name $ACR_NAME --sku Standard --location $REGION

Connect ACR with AKS cluster

#Connect ACR with AKS
# Set RBACs for the service principle of AKS
 CLIENT_ID=$(az aks show --resource-group $RG --name $CLUSTER_NAME --query "servicePrincipalProfile.clientId" --output tsv)

 # Get the ACR registry resource id
 ACR_ID=$(az acr show --name $ACR_NAME --resource-group $RG --query "id" --output tsv)

# Create role assignment
az role assignment create --assignee $CLIENT_ID --role acrpull --scope $ACR_ID

Create SQL Server and Database need for the Application

#Create SQL Server
az sql server create -l $REGION -g $RG -n $SQL_SVR_NAME -u sqladmin -p [email protected]

az sql db create -g $RG -s $SQL_SVR_NAME -n $DB_NAME --service-objective S0

After the DB created login to Azure DevOps and select the project created through Azure DevOps demo generator.

Edit variables and parameters for Build Pipeline

Under pipelines in left menu select pipeline and click edit to do modifications to pipeline as below.

This will show you all the tasks are associated with build process. We need to do few modifications to build task. As the first step we need to replace the build variables as below with our ACR,DB etc.

Next, we need to change the subscriptions in each build step and select relevant resources such as ACR settings as below.

After build variables are changed next we need to modify the release pipeline.

Edit Relese Pipeline

To deploy the build application to Kubernetes service under release pipeline we has to do few modifications. First we need to add release variables as below.

To edit the release pipeline follow below steps.

Change the hosted agent for the SQL backpack task. Make sure to select windows server agent pool for this task.

For AKS deployment task changed the subscription and cluster details.

Run the Build Pipeline

When running the pipeline as below it starts to build the application and if the build it a success then trigger the release process.

Build process status

If the build completed successfully, it start with the release pipeline.

Release is completed as below if there no errors prompt.

Next step is to verify the app is deployed to Kubernetes cluster. First download the credential as below.

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

Check the frontend and backend pods are created with the load balancer service.

#View the pods are deployed
kubectl get pods

#View Load Balance service 
kubectl get svc

kubectl get svc will list down the services as below copy the public IP and check the application running on that IP.

Before check the application, we need to enable SQL server firewall to access the Azure services as below.

Application running on the LB IP

Additional Links