How to deploy multi-container application to Azure Kubernetes Services?

 Azure Kubernetes Services is the fastest way to use Kubernetes on Azure. Azure Kubernetes Service (AKS) manages the hosted Kubernetes environment, making it easy to deploy and manage containerized applications without requiring any container orchestration expertise. It also improves the agility, scalability, and availability of your containerized workloads. Azure DevOps streamlines AKS operations by providing continuous build and deployment capabilities.

In this blog, we will use Azure DevOps to deploy a containerized ASP.NET Core web application to an AKS cluster. The steps used in this blog can be used to deploy any application to AKS. The entire end-to-end demo is available in this video.

Deploy the AKS, ACR & Azure SQL Server

This is a three-tier application with the following components :

  • Azure AKS cluster hosting front-end pod and backend-pod. Front end pod is responsible to service all the web requests and hosts a web server.
  • Middle tier components are stored in Backend Pod and it is responsible to call the database to get the database.
  • All the application data is stored in the Azure SQL Database.
  • Apart from these three components, we will be using the container images and Azure Container Registry will be hosting these images.

We will use the following Azure CLI code to deploy these components:

az login
#--------------Variable Definition------------------

#--------------Variable Definition------------------

#Create Resource Group
az group create --name $RGNAME --location $LOCATION

#Get AKS Version 
VERSION=$(az aks get-versions --location $LOCATION --query 'orchestrators[-1].orchestratorVersion' --output tsv)
echo 'AKS version is' $VERSION

#Create AKS cluster
az aks create --location $LOCATION --resource-group $RGNAME --name $AKSNAME --enable-addons monitoring --kubernetes-version $VERSION --generate-ssh-keys

#create the logical server to host the Azure SQL database
az sql server create --location $LOCATION --resource-group $RGNAME --name $SQLNAME --admin-user sqladmin --admin-password P2ssw0rd1234

#Allow Access from Azure to Newly provisioned logical server
az sql server firewall-rule create --resource-group $RGNAME --server $SQLNAME --name allowAzure --start-ip-address --end-ip-address

#Create SQL db
az sql db create --resource-group $RGNAME --server $SQLNAME --name mhcdb --service-objective S0 --no-wait

#To View Logical server Hosting the DB
echo $(az sql server list --resource-group $RGNAME --query '[].name' --output tsv)''

#Create ACR
az acr create --location $LOCATION --resource-group $RGNAME --name $ACRNAME --sku Standard

#display the name of the login server of the Azure Container registry 
az acr show --name $ACRNAME --resource-group $RGNAME --query "loginServer" --output tsv

# Retrieve the id of the service principal configured for AKS
CLIENT_ID=$(az aks show --resource-group $RGNAME --name $AKSNAME --query "identityProfile.kubeletidentity.clientId" --output tsv)
echo 'Client id of Multicontainer app is ' $CLIENT_ID

# Retrieve the ACR registry resource id
ACR_ID=$(az acr show --name $ACRNAME --resource-group $RGNAME --query "id" --output tsv)
echo 'ACR Resource ID of Multicontaineracr is ' $ACR_ID

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

AKSNAME=$(az aks list --resource-group $RGNAME --query '[].name' --output tsv)
az aks get-credentials --resource-group $RGNAME --name $AKSNAME

# This verification will be done after Azure Pipeline is deployed
kubectl get pods
kubectl get service mhc-front --watch

Deploy Azure DevOps Teams project for .NET Core Application

We will use Azure DevOps Demo Generator to generate a new project based on Azure Kubernetes Services Project template. Here are the steps:

  1. Click the Azure DevOps Demo Generator and after signing into AzureDevOps select the Azure Kubernetes template.

2. Once you select the template it will ask if you have the Kubernetes and Replace token extensions. If you have it it will show it in the green. For example below screenshot

If you do not have these extensions please install these extensions by clicking the links.

Configure the Containerized application and Database with Azure DevOps

Now we already have the Azure DevOps Pipeline and a sample application. if you want to use some other application code please feel free to modify the code. For the purpose of simplicity, we are using the MyHealthClinic sample project.

This will create two sets of Pipelines:

  1. Build Pipeline
  2. Release Pipeline

The build pipeline is shown below.

Build Pipeline has these Tasks and their usage:

These pipelines use the variables and these variables are defined here in the Pipeline:

We need to populate these variables in order for Build Pipeline to work. Moreover, we need to authorize the Azure subscription and select Resource group, AKS, and ACR instances created earlier via Azure CLI.

The release pipeline has these tasks:

DB deployment task is used to populate the tables in the Database and AKS tasks deploy the deployment and services and finally update the image.

Understanding Docker build and Docker compose files

The application build process uses these files:

  1. file. The below image shows how it builds the application.

2. Docker-Compose.overide.yml file overrides the setting provided in the file. The below image shows how it overrides these settings.

3. Docker-compose file is responsible to build the image. It has reference to Dockerfile.

4. Dockerfile referred to above is used to build the image by using the image from the Docker hub and publishing the output in the output directory.

Finally, we need to create a deployment and services configuration file to deploy the images created with Dockerfile into AKS. Here are these configuration files. And the below images show each element and its use.

Cluster IP created below shows its purpose to communicate between PODs.

The load Balancer shown above is created with the below AKS config file. It is responsible to direct the traffic to each node and POD.

Once build and release pipelines are executed it will deploy the Web Application

kubectl get pods
kubectl get service mhc-front --watch

Running these commands will show the PODs and Loadbalancer URLs and once you go to the Load Balancer IP address you will see the below website. This shows our multi-container app is working fine.

I hope you enjoy this blog and it is helpful for you to understand the multi-container app deployment in AKS.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.