Learning Azure Kubernetes Services - Day Two - Deploy ASP.NET Core Applications On Local Kubernetes Cluster

Introduction

 
This article is the continuation of the three series article where we are trying to learn and get hands-on with Azure Kubernetes Services. In the last article, we created an ASP.NET Core application and containerized it with Docker. We created the local image of the application and tested it locally. In this article, we’ll deploy that image to local Kubernetes cluster.
 

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.
 
Before we start there are some pre-requisites to be taken care of. Along with an Azure account,  make sure the following things are 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.
 
The reader of this article is expected to have followed the last article as this is the continuation article.
 

Deploy on Local Kubernetes Cluster

 
There are two approaches to deploy the image on local Kubernetes cluster i.e. running on developers’ machine. Make sure Kubernetes is installed and running. You can check the version by kubectl version --short command.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 

Interactive approach

 
Run the kubectl run command. Run Kubectl followed by the name of a deployment<aks-deployment>, followed by the --image <ask:local>, then follow that with the --port switch and specify 80, which is the port our containers expose our application. The --replica switch to specify that 3 replicas are needed for the application.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
When command is executed, a deployment is created where the desired state is set to three replicas.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
Next, a replica set is created that ensures that there are always three replicas of the application running.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
The scheduler schedules the pod deployment on the worker node, which commands the Docker engine running on the worker node to pull the image and then run in pods.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
Once application has deployed, create a service to expose this deployment. To create a service, run the kubectl expose command and use the type as NodePort. A service of the type NodePort exposes the service on each node's IP at a static port. Run kubectl expose deployment aks-deployment --type=NodePort command.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
This creates the service. Verify it by running kubectl get service or svc.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
To connect to the service, launch the browser and connect to the localhost followed by the port number exposed on the node.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
The application is up and running. Now I have opened three browsers i.e. Chrome, IE, and Mozilla and hit the same URL. Let’s see what happens.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
We see that the container hostname changes. That's because behind the servers there are three pods, or containers, which the service is load balancing the request to.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 

Declarative Approach

 
There is another approach called declarative approach to deploy the application. Clean up the deployment and service by deleting them with the use of kubectl delete command. Execute kubectl delete deployment aks-deployment and likewise delete the service as well.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
A deployment manifest file i.e. a YAML file is needed now. There is already a YAML file at the root of the application(aksdeploy.yml). Open it.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
File has two divisions, the first creates the deployment and another exposes it as a service. Make the changes in the file as shown in the following image.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
The command kubectl create -f .\aksdeploy.yml creates the deployment and service.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
Check the status of the service using kubectl get svc and launch the browser to connect to the service.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 
So, the deployment is again successful with this approach as well.
 
Again, delete the deployment and the service, but this time by specifying the file name.
 
Azure Kubernetes Services - Deploy ASP.NET Core Applications On Local Kubernetes Cluster
 

Conclusion

 
This article was an end to end article to understand how to containerize and deploy an ASP.NET application to local Kubernetes. In the next article we will explore Azure Container Registry (ACR) and also how to deploy the image to Azure Kubernetes Services on Azure.Stay tuned!