Deploying a multi-container application to Azure Kubernetes Services

Deploying a multi-container application to Azure Kubernetes Services
Photo by Luca Bravo / Unsplash


Azure Kubernetes Service (AKS) is the quickest way to use Kubernetes on Azure. Azure Kubernetes Service (AKS) manages your hosted Kubernetes environment, making it quick and easy to deploy and manage containerized applications without container orchestration expertise. It also eliminates the burden of ongoing operations and maintenance by provisioning, upgrading, and scaling resources on demand, without taking your applications offline. Azure DevOps helps in creating Docker images for faster deployments and reliability using the continuous build option.

Lab Scenario

This lab uses a Dockerized ASP.NET Core web application - MyHealthClinic (MHC) and is deployed to a Kubernetes cluster running on Azure Kubernetes Service (AKS) using Azure DevOps.

mhc-aks.yaml  Load Balancer  Redis Cache

Click the Azure DevOps Demo Generator link and follow the instructions in Getting Started page to provision the project to your Azure DevOps.

For this lab the Azure Kubernetes Service template is used which is already selected when you click on the link above. There are some additional extensions required for this lab and can be automatically installed during the process.

  1. Launch the Azure Cloud Shell from the Azure portal and choose Bash.

Deploy Kubernetes to Azure, using CLI:

i. Get the latest available Kubernetes version in your preferred region into a bash variable. Replace <region> with the region of your choosing, "eastus".

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

ii. Create a Resource Group

  az group create --name akshandsonlab --location eastus

iii. Create AKS using the latest version available

 az aks create --resource-group akshandsonlab --name akspwnresist --enable-addons monitoring --kubernetes-version $version --generate-ssh-keys --location eastus

Deploy Azure Container Registry(ACR):

 az acr create --resource-group akshandsonlab --name pwnresistmhc --sku Standard --location eastus

Authenticate with Azure Container Registry from Azure Kubernetes Service

az aks update -n akspwnresist -g akshansonlab --attach-acr pwnresistmhc

Create Azure SQL server and Database:

 az sql server create -l eastus -g akshandsonlab -n sqlsvrpwnresist -u sqladmin -p P2ssw0rd1234

Create a database

 az sql db create -g akshandsonlab -s sqlsvrpwnresist -n mhcdb --service-objective S0

The following components - Container Registry, Kubernetes Service, SQL Server along with SQL Database are deployed. Access each of these components individually and make a note of the details which will be used in Exercise 1.

Select the mhcdb SQL database and make a note of the Server name.

Click on “Set server Firewall” and enable “Allow Azure services …” option.

Navigate to the resource group, select the created container registry and make a note of the Login server name.

Configure Build pipeline

  1. Navigate to Pipelines –> Pipelines.
  2. Select MyHealth.AKS.Build pipeline and click Edit.

In Run services task, select your Azure subscription from Azure subscription dropdown. Click Authorize.

Following the successful authentication, select appropriate values from the dropdown - Azure subscription and Azure Container Registry as shown.

Repeat this for the Build services, Push services and Lock services tasks in the pipeline.

applicationsettings.json file contains details of the database connection string used to connect to Azure database which was created in the beginning of this lab.

mhc-aks.yaml  deployments services  pods

Click on the Variables tab.

Update ACR and SQLserver values for Pipeline Variables with the details noted earlier while configuring the environment.

Save the changes.

Configure Build pipeline (YAML) -Optional

We also have a YAML build pipeline if that’s something you’re interested in. To proceed through the YAML pipeline, choose MyHealth.AKS.Build-YAML and click Edit. If you utilize the YAML pipeline, make sure to update the MyHealth.AKS.Release release definition’s artifact link.

  1. Navigate to Pipelines –> Pipelines.

2. Select MyHealth.AKS.Build - YAML pipeline and click Edit.

3. In Run Services task, select settings. Select your Azure subscription from Azure subscription dropdown. Click Authorize.

4. Following the successful authentication, select appropriate values from the dropdown - Azure subscription and Azure Container Registry as shown and click Add.

Repeat this for the Build services, Push services and Lock services tasks in the pipeline.

5. Click on the Variables tab.

6. Update ACR and SQLserver values for Pipeline Variables with the details noted earlier while configuring the environment.

Configure Release pipeline

  1. Navigate to Pipelines | Releases. Select MyHealth.AKS.Release pipeline and click Edit.

2. Select Dev stage and click View stage tasks to view the pipeline tasks.

3. In the Dev environment, under the DB deployment phase, select Azure Resource Manager from the drop down for Azure Service Connection Type, update the Azure Subscription value from the dropdown for Execute Azure SQL: DacpacTask task.

4. In the AKS deployment phase, select Create Deployments & Services in AKS task.

Update the Azure Subscription, Resource Group and Kubernetes cluster from the dropdown. Expand the Secrets section and update the parameters for Azure subscription and Azure container registry from the dropdown.

Repeat similar steps for Update image in AKS task.

Create Deployments & Services in AKS will create the deployments and services in AKS as per the configuration specified in mhc-aks.yaml file. The Pod, for the first time will pull up the latest docker image.

Update image in AKS will pull up the appropriate image corresponding to the BuildID from the repository specified, and deploys the docker image to the mhc-front pod running in AKS.

A secret called mysecretkey is created in AKS cluster through Azure DevOps by using command kubectl create secret in the background. This secret will be used for authorization while pulling myhealth.web image from the Azure Container Registry.

5. Select the Variables section under the release definition, update ACR and SQLserver values for Pipeline Variables with the details noted earlier while configuring the environment. Select the Save button.

Trigger a Build and deploy application

Let us trigger a build manually and upon completion, an automatic deployment of the application will be triggered. Our application is designed to be deployed in the pod with the load balancer in the front-end and Redis cache in the back-end.

  1. Select MyHealth.AKS.build pipeline. Click on Run pipeline

2. Once the build process starts, select the build job to see the build in progress.

3. Switch back to the Azure DevOps portal. Select the Releases tab in the Pipelines section and double-click on the latest release. Select In progress link to see the live logs and release summary.

Once the release is complete, launch the Azure Cloud Shell and run the below commands to see the pods running in AKS

a). Type az aks get-credentials --resource-group yourResourceGroup --name yourAKSname in the command prompt to get the access credentials for the Kubernetes cluster. Replace the variables yourResourceGroup and yourAKSname with the actual values.

b). kubectl get pods

c). To access the application, run the below command. If you see that External-IP is pending, wait for sometime until an IP is assigned.

kubectl get service mhc-front --watch

Copy the External-IP and paste it in the browser and press the Enter button to launch the application.

Summary

Azure Kubernetes Service (AKS) reduces the complexity and operational overhead of managing a Kubernetes cluster by offloading much of that responsibility to the Azure. With Azure DevOps and Azure Container Services (AKS), we can build DevOps for dockerized applications by leveraging docker capabilities enabled on Azure DevOps Hosted Agents.