Azure Kubernetes Services – Day One: Containerize ASP.NET Core Applications with Docker

Posted by

Introduction

There was an era of monolithic software applications where the whole application was packaged and deployed on a single server running as a single process. The architecture and the software development life cycle for monolithic applications were no doubt error prone and a single point of failure could bring the complete application down irrespective of the module causing root cause not associated with other application modules. Worse than this could be a situation of hardware failure that eventually brings the application down and the IT team indulges in the setup of new infrastructure or migration.

To overcome the situation with monolithic applications, evolved the microservices architecture.

The term microservices portrays a software development style that has grown from contemporary trends to set up practices that are meant to increase the speed and efficiency of developing and managing software solutions at scale. Microservices is more about applying a certain number of principles and architectural patterns than it is about architecture. Each microservice lives independently, but on the other hand, also all rely on each other. All microservices in a project get deployed in production at their own pace, on-premise, on the cloud, independently, living side by side.

The components could be easily managed if there are less in the count but with the increasing number of components in a single application, the deployment-related risks also increase.

With the changing application development and deployment paradigm from monolithic to microservices to more advanced one, a need of automation arises, which includes the automatic scheduling of the micro-components to our servers, automatic configuration, supervision, and failure handling. Not only deployment combination count increases but the interdependencies of the components also rises. This calls for the automation which takes responsibility for automatic scheduling, configuration, monitoring, error handling, and scaling. Hosting the microservices in containers solves the purpose but then comes the question of orchestration. If a microservice needs to be hosted in a container it also needs the orchestration of the container.

This is exactly where Kubernetes comes into the picture.

Kubernetes not only benefits development team in application deployment but also is a boon for the DevOps team that helps in monitoring and error handling. Kubernetes offers a layer of abstraction where the DevOps do not have to worry about keeping an eye upon the individual applications rather they manage and supervise Kubernetes and other related infrastructure.

Thesis

In this article, we’ll demonstrate how to orchestrate the deployment and scaling of an entire ASP.NET Core application using Docker and Azure Kubernetes Services. The entire article will be divided into three parts as follows.

  1. Learning Azure Kubernetes Services – Day One: Containerize ASP.NET Core Applications with Docker.
  2. Learning Azure Kubernetes Services – Day Two: Deploy ASP.NET Core Applications on local Kubernetes Cluster.
  3. Learning Azure Kubernetes Services – Day Three: Deploying ASP.NET Core Application to Azure Kubernetes Services.

Containers

Containers basically provide the isolation of the operating system

Containers offer the lightweight process and the isolation of an operating system. With virtual machines, the hardware could be virtualized creating multiple virtual machines running on a single physical machine whereas containers help in virtualizing the operating system and get isolated copies of the operating system that can offer compute services to the applications.

Docker

Docker made the containers portable and eased the process of packaging the application, their related libraries dependencies and the operating system itself. That portable package could be easily run over any other machine that has the docker running. Learn more about Docker here. Open this URL and click on Download from Docker hub. Once downloaded, login to the Docker and follow instructions to install Docker for Windows.

Kubernetes

Kubernetes serves as a system that facilitates the deployment and management of the containerized applications. Kubernetes works independently without being concerned about the underlying infrastructure being a physical or virtual machine. While deploying an application having a lot of components via Kubernetes, it takes responsibility for choosing a server for each component and deploys it and helps components to communicate with all other components of the application. It provides a layer of abstraction from the underlying hardware infrastructure and manages the components on its own.

Azure Kubernetes Services (AKS)

Microsoft Azure offers Azure Kubernetes Service that eases deployment, management, monitoring and operations of Kubernetes. AKS manages the hosted Kubernetes environment and makes it very easy to deploy and manage containerized applications without any expertise in container orchestration. AKS removes the overhead of running operations and maintenance by provisioning, upgrading, and scaling resources on demand without taking down the applications.

Application Overview

Time to get hands-on with Docker, Containers, Kubernetes and AKS. We’ll create an ASP.NET Core application and first containerize it with Docker and the deploy to AKS cluster. The logic of the application doesn’t matter here as we’ll just use application for the sake of having an application to deploy. The focus would be on Dockers, Containers, Kubernetes and AKS cluster. When the application is dockerized, we’ll create a local image of the application to test locally. Then we’ll deploy the application on local Kubernetes cluster and then later we’ll push the same application to AKS. Once docker for windows is installed and running, go to the settings and enable the checkbox for Kubernetes as shown below.

Pre-requisites

Before we start there are some pre-requisites to be taken care of. Along with an Azure account and make sure the following stuff is installed on the development machine.

  1. Git for Windows.
  2. .Net Core 2.1 or above.
  3. Azure CLI
  4. Azure PowerShell

After Azure CLI installation, run the following command (az –version) to make sure the CLI is up and running.

ASP.NET Core Application

Create any sample ASP.NET core application, or clone/download the already created application from https://github.com/akhilmittal/Azure-Kubernetes-Service.

Open the command prompt and navigate to the folder where the application needs to be cloned then run the command git clone https://github.com/akhilmittal/Azure-Kubernetes-Service. This will clone the code on the local machine.

Execute dotnet run as shown below. This will run the server and tell on what port the server is listening for the application i.e. http://localhost:5000.

Navigate through the provided URL i.e. http://localhost:5000. It is a simple application with just showing the host or container in which the application is running. Currently, it runs on a development windows machine without a container and so it shows the name of the host machine i.e. 3593BH2.

Following is what application looks like in Visual Studio with a single cshtml page containing the code of what is shown on the UI.

Containerize the ASP.NET Core Application

Navigate to the root of the application directory to see the already created Docker file.

This Docker file will be used to build the image.

Runtime image is build using docker build command as shown below.

Make sure that we are in the directory that contains a Docker file and then run the docker build command followed by a dot to indicate that the Docker file is located in the current directory and use the -t switch to specify a tag of aks:local.

This will kickstart the build process.

Once the build completes, run the docker image list command, it lists a brand-new image called aks:local.

To test this image, run a container based on this image. So, run the docker run command with the -d switch to tell Docker to run this as a daemon process and use a “-p” switch to map the port 5000 on the host to the local port 80 on the container, and then, specify the name of the image i.e. aks:local.

Execute command docker ps to see the container up and running.

Now go to the browser and connect to http://localhost:5000.

We see the application now running under docker container as the hostname now shows the id of the container here.

Moving further, let’s delete the newly created container as shown below.

Conclusion

This article was an end to end article to understand how to containerize an ASP.NET application. In the next article we’ll see how we can deploy a containerized ASP.NET core application to local AKS Cluster.

Next >> Deploy the ASP.NET Core Application to local Kubernetes Cluster

Advertisements

2 comments

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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.