Saturday, 29 December 2018

Deploying ASP.NET Core App to Azure Kubernetes Services (AKS)–Setting Up Azure DevOps Pipeline Manually–Step By Step Guide–Part 1

In the post “Deploy ASP.NET Core App to AKS with Azure DevOps” a quick start guide has been given on setting up an ASP.NET Core App with Azure DevOps projects. The application code, Azure Container Registry, Azure Kubernetes Service (AKS) Cluster as well as build and deployment pipelines were auto generated, with few clicks in the Azure Portal. To do deployment to AKS, Helm packaging was used in the Azure DevOps Projects auto generated sample app. In this post, let’s look at much simpler implementation, to get the ASP.NET Core App build and deployed to AKS via Azure Pipelines.

As the first step create a ASP.NET Core App in Visual Studio. When you hit F5 in Visual Studio, a browser should launch and load the App. To deploy the ASP.NET Core app in AKS it needs to be containerized. For this purpose you should have Docker for Windows installed in your machine, if you are developing your ASP.NET Core App using windows. When setting up Docker for Windows make sure to set it up for Linux containers instead of Windows containers as we are going to deploy the app to AKS as a Linux container.
Right click your ASP.NET Core App project in the solution explorer and click Add –> Docker Support, in the context menu. Select option as Linux to add Docker Support to the ASP.NET Core app as we are going to run it as a Linux container in AKS.image
This will add a Dockerfile to your ASP.NET Core App. The Dockerfile specifies to obtain dotnet 2.1 sdk image and copy the source code of the ASP.NET Core App. Then it will build and publish the app. As the next step, will use the dotnet 2.1 aspnetcore runtime image and copy the published files and prepare the container image for app execution. This basic Dockerfile is clearly explained in the post here. image
When you run the Web App in the Visual Studio it builds the docker image as per instructions in the Docker file, deploys the container in Docker for Windows, and run the container. Now you should submit the code to Azure DevOps Git repo in an Azure DevOps Team Project.
The next step is to get the Docker images built with Azure build pipelines. Before doing that we need to setup the container registry where we are going to push the built Docker image. We are going to use an Azure Container Registry for this purpose. Creating Azure Container is explained in the post here.
Create a new build pipeline and set it to use the Git repo which  you have pushed the Docker supported ASP.NET Core app. Select the Hosted Ubuntu build agent as the build agent pool.image
Add a Docker Command task to the build pipeline and set the command to Build. For the Container Registry Type select Azure Container Registry. Click on Manage link near Azure Subscription and add a service connection (more information on service connections can be found in this article) to the Azure Resource group which you have created for the Azure Container Registry. This will list the container registry name in the drop down of Azure Container Registry in the Docker task. Select the container registry that you have created earlier.image
Set the Command as build to build the Docker image. Select the Dockerfile in the repo which have been created with Visual Studio. For the image name provide a name and use build ID as the tag. Image name should be in the following format. Make sure to Set the Build context to the Web App folder as it would be the path considered in the Dockerfile as the context.
Example – aksdemo:(Build.BuildId)image
Add another Docker task to the pipeline and set it to use Push as the command. Select the Azure subscription service connection used in previous step and the container registry.image
Set the Image name to exact image name setup in the Docker Build task. It is advisable to use a Build variable and store the image name with tag format to avoid any issues in accidental mistyping of names, and for improved maintainability of the build pipeline.image
With these two tasks the build pipeline is ready to build the Docker image and push it to the Azure Container Registry. You can further improve the build pipeline to use variables for common values used in multiple tasks and enable continuous integration to build on each commit and push an image to the registry. Once you queue a build the build will create the Docker image and push it to the Azure Container Registry.image
Notice the build id of the build and in Azure Container Registry,Repositories you can find the Docker image is created with the name you have provided and tagged with the build Id. Even though this post uses build Id as the image tag you can define your of tag for image my be based on repo tag, or build version or any other formula of your preference.SNAGHTML608056ba
This concludes the part one of getting an ASP.NET Core app build as a Docker image and deploying it to AKS via Azure DevOps. We have created the Docker image and now have it in the Azure Container Registry using Azure DevOps build pipeline created with two simple steps, Build Docker image and Push it to ACR. In the next post, we can understand how to setup deployment of the Docker image in Azure Container Registry (ACR) to an AKS cluster, using simple steps defined in an Azure release pipeline.

No comments:

Popular Posts