Azure Kubernetes Services - Day One - Containerize ASP.NET Core Applications With Docker

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 cycles for monolithic applications were no doubt error-prone and a single point of failure could bring the complete application down irrespective of the module 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 engages in the setup of new infrastructure or migration.
 
Microservices architecture evolved to overcome the situation with monolithic applications. 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 fewer  of them,  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 ones, a need for automation arises, which includes the automatic scheduling of the micro-components to our servers, automatic configuration, supervision, and failure handling. Not only does the deployment combination count increase but the interdependencies of the components also rise. 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 teams 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 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.
 
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 getting 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 library 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 then deploy to AKS cluster. The logic of the application doesn’t matter here as we’ll just use an 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.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
Pre-requisites
 
Before we start there are some pre-requisites to be taken care of. Along with an Azure account, 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.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 

ASP.NET Core Application

 
Create any sample ASP.NET core application, or clone/download the already created application from here.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker 
  1. Open the command prompt and navigate to the folder where the application needs to be cloned then run the command git clone. This will clone the code on the local machine.

    Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
  1. 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.

    Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker

  2. Navigate through the provided URL i.e. http://localhost:5000. It is a simple application 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.

    Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
Following is what the application looks like in Visual Studio with a single cshtml page containing the code of what is shown on the UI.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker 
 

Containerize the ASP.NET Core Application

 
Navigate to the root of the application directory to see the already created Docker file.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
This Docker file will be used to build the image.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
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.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
This will kickstart the build process.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
Once the build completes, run the docker image list command, it lists a brand-new image called aks:local.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
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.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
Execute command docker ps to see the container up and running.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
Now go to the browser and connect to http://localhost:5000.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 
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.
 
Azure Kubernetes Services - Containerize ASP.NET Core Applications With Docker
 

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.


Similar Articles