Monday, May 6, 2019

Deploying ASP.NET Core app to Kubernetes Cluster hosted on Azure’s AKS



How to leverage Microsoft Azure’s Kubernetes Service to host a containerized ASP.NET Core app




Introduction

In previous articles I have discussed how we create a containerized Entity Framework Core enabled ASP.NET Core application developed on Mac and hosted on Docker Hub’s repository. In this article, we will take a look at how to deploy that docker hub hosted docker image into AKS (Azure’s Kubernetes Service). This is the final step which mostly covers the deployment on AKS's Kubernetes cluster. Once the deployment is done, it would become apparent that how powerful Kubernestes 

Prerequisites

1.     AZ CLI 
2.     Containerized ASP.NET Core application deployed on Docker Hub. For details on how to create a containerized docker ASP.NET Core application, please refer to my previous article here: http://mnabeelkhan.blogspot.com/2019/04/developing-containerized-entity.html


Flow overview


 

The flow is pretty simple. It is a three-step process of first creating a containerized ASP.NET Core application in form of a docker image, then pushing that docker image to Docker Hub, and the last step is to deploy the docker image to Kubernetes cluster. The first two steps (1 . Creating a docker image for a containerized docker application and 2. Pushing that image to Docker Hub) has already been covered in the previous article. In this article we will focus on how to deploy a docker image to a Kubernetes cluster using Azure’s Azure Kubernetes Service (AKS).

Before delving into the steps, it is important to mention that the proceeding steps work for both GCP and Azure seamlessly. The only different is the step 1 where we are going to create a Kubernetes cluster. The rest of the steps are exactly the same for both GCP and Azure implementations of Kubernetes clusters. This shows the power of Kubernetes and how widely it is supported by cloud platforms.

Steps:

As our target deployment Kubernetes cluster is on Azure, we have to carry out a setup step of creating a cluster first. That would be our step 1.


1.     Create a Kubernetes Cluster
 “az aks create --resource-group [[YOUR_RESOURCE_GROUP]] --name [[YOUR_CLUSTER_NAME]] --node-count 2 --service-principal [[YOUR_APP_ID]] --client-secret [[Your_Password]]"

Once the above command completes, get the credentials of the newly created cluster by using the following command:

"az aks get-credentials --resource-group [[YOUR_RESOURCE_GROUP]] --name [[YOUR_CLUSTER_NAME]]"

For this blog post, I created "DockerHubAKSCluster" Kubernetes cluster on Azure with "DockerHubAKS" as resource group name. Here is the screenshot of what Azure portal shows me once I create a cluster.






2.     Create a Kubernetes Service
This can be done by deploying docker image into a newly created service.
Run the following command:
“kubectl run mac-api --image=[[YOUR-DOCKER_HUB_REPO_ID]]/mac-api --port 8080”


3.     Expose the deployed service on a port
Run the following command:

“kubectl expose deployment mac-api --type=LoadBalancer --port 80 --target-port 80”
The result of the above command should be:
“service/mac-api exposed”

Once the service has been deployed and exposed, you can run the following command that will give you the cluster IP and the external IP information:
“kubetctl get service”
           


 Once you deploy and expose the cluster, you will notice that Azure creates a separate resource group with "MC_" prefix to the original resourced group that you have used in step 1. Following screen shots shows the contents:


As you can see from the above screenshot that Microsoft Azure creates a separate resources for the infrastructure of Kubernetes cluster. As a user of Kubernetes cluster, we mostly work with the Kubernetes cluster that we have created and Azure automatically configures the other resource group to ensure the infrastructure is in place.



4.     Test
Since the mac-api is an API application, we are going to use “Postman” to test the application.

Following screenshots show how Kubernetes cluster hosted containerized ASP.NET Core application is responding to “Get” and “Post” commands:






Conclusion

In this article we have seen how simple and straight forward is to take a docker hub hosted docker image and deploy that on Azure’s Kubernetes service AKS.
 


1 comment: