Automating container deployment on AKS using Azure DevOps – Part 1

Introduction

In this series of posts, I’ll demonstrate the deployment of Azure Container Registry and Azure Kubernetes Service. I’ll also push a Web API container to ACR and pull that container and deploy it on an AKS pod. To keep things simple, AKS will consist of only one pod. Managed identity will be enabled on AKS.

I’m automating the whole deployment and release on Azure DevOps using build and release pipelines. Azure Repos will hold the source code.

Pre-requisites

  • Docker Desktop
  • Visual Studio code
  • Docker and Kubernetes extensions for Visual Studio code


Part 1

In Part 1, the following steps will be covered:

  • Create a Web API.
  • Containerize the Web API.
  • Create a build pipeline on Azure DevOps that will build and push the container to ACR. I’m assuming ACR already exists.

Create a Web API

Create a .Net 5.0 API using the following command:

dotnet new webapi -o weatherinfoapi --no-https

This will generate a very simple API that uses an in-memory data store to return weather forecasts. You can test the generated API using a tool like Postman. Try this GET method in a browser or Postman – localhost:5000/weatherforecast. Change the port to whatever is configured for your API. You should be able to see a list of weather forecasts. Now that you have an API to deploy, let’s create a container for it.

Containerize the Web API

Open the Web API folder in Visual Studio. Go to Command Palette. Type Docker and you should be able to see Docker:Add Docker files to Workspace… command. Choose .Net Core as the framework, Linux as the operating system, and enter the port number of your API. A file named Dockerfile will be added to your project. It contains instructions on how to build a docker container. The .dockerignore file lists all files and directories that need to be excluded from the image.

Run the following command in the VS Code terminal to generate the docker image. The Docker desktop needs to be running before you can execute this command.

docker build -t weatherinfoapi:v1 .

After the build is complete, run docker images to get a list of images in your system. The newly created api image should be listed there. You can test it locally by running the below command. Replace 5000 with the port number of your API.

docker run -it --rm -p 8080:5000 weatherinfoapi:v1

Create a build pipeline on Azure DevOps

Create a new project for the API in Azure DevOps. Push your code to the remote repository. Now you are ready to create a build pipeline that will automatically build and push the image to Azure Container Registry.

Under Pipelines->Pipelines click “Create Pipeline”. I’m using the classic editor this time. Make sure that the agent pool is set as shown below:

Agent pool

Choose the repository and the branch. Next, for the template, type “Docker” in the search box. Choose Docker container and hit apply. It contains two steps – one for building the container and another for pushing the container to ACR.

Let’s see how we can configure each step.

Build an image

Choose the Azure subscription and Authorize the pipeline to use the connection. You need to enter your credentials for the first time. Select the Azure Container Registry.

Push an image

Select the Azure service connection and the Azure Container Registry. Save and queue the pipeline.


Once the build is complete you should be able to see the container on the Azure portal. Browse to your container registry and look under Repositories.

References

Leave a comment