Deploy And Run .NET Core 7 Web API On Azure Kubernetes Service (AKS)

We will learn about the Containerization of .NET Core Web API using Docker and then deploy it on Azure Container Registry and run that image into Azure Kubernetes Service (AKS).

Agenda

  • Overview
  • Implementation of .NET Core Web API
  • Dockerization of application
  • Deploy application image on Azure Container Registry
  • Run application image inside Azure Kubernetes Service (AKS)

Prerequisites

  • Visual Studio 2022
  • Docker Desktop
  • .NET Core 7 SDK and Runtime
  • Azure Portal Subscription

Overview

In the software world, we all want to automate things and reduce manual work related to the deployment and management of different application components, in that case, azure provides many services for that like azure container registry and container instance as I have shown below diagram.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

  • Azure container registry stores private docker images and manages the same.
  • Azure container instance is a managed service that helps us to run a container directly on the azure cloud without requiring any other infrastructure like a virtual machine (VM) and like that.
  • Also, it helps to automate our application deployment by using CI/CD tools in which we configure some automation steps which build and deploy our application docker image inside the container registry and after that, we can easily run that image inside the azure container instance in an isolated environment very efficiently. 
  • Azure Kubernetes Service (AKS) is an open-source and fully managed container orchestration service available on the Azure public cloud and used to deploy and manage different container images.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

  • AKS has provisioning to auto-scale resources without any downtime as per the requirement.

Implementation of .NET Core Web API

Step 1

Create a new .NET Core 7 Web API

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 2

Configure your application

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 3

Provide additional information

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 4

Add docker support inside the application using Visual Studio

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 5

Run the application using the docker (Note: Make sure Docker Desktop is installed and running on your machine)

Deploy application image on Azure Container Registry

Step 1

Login on Azure Portal

Step 2

Create Resource Group

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 3

After that, Create a new Azure Container Registry

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 4

Next, right-click on the application solution and click on publish

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 5

Click on Docker Container Registry and press Next

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 6

Select Azure Container Registry and Click Next

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 7

After that, select the subscription and container registry which we created earlier and click on finish.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 8

Next, click on Publish and it will upload a docker image on the azure container registry.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 9

Here you can see application image details inside the repository section of our container registry on the Azure portal.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Run application image inside Azure Container Instance

Step 1

Crete the AKS Service on Azure

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Next, configure container registry which we created earlier

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 2

Open AKS and there we can find details about AKS

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 3

Click on connect button and the credentials will get populated

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Here you can see two commands, that you need to execute on your local CMD but for that, you need Azure CLI because without that AZ command will not work. So, download Azure CLI from the following URL

https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli

Step 4

Open CMD and enter the az login command it will open a window inside the browser and you need to provide Azure credentials

az login

Also, enter the two aks commands after login as I showed in the above image.

Step 5

Create a pod.yml file and apply

apiVersion: v1
kind: Pod
metadata:
 name: weather-forecast-app
 labels:
 app: weatherforecast-aks
 component: weatherforecast-app
spec:
 containers:
 - image: jdleaningregistry.azurecr.io/weatherforecastapplication:latest
 name: weatherforecast-webapi
 ports:
 - containerPort: 80

Step 6

Next, create a service.yml file and apply

apiVersion: v1
kind: Service
metadata:
 labels:
 app: weatherforecast-aks
 name: weatherforecast-aks
spec:
 ports:
 - port: 8080
 protocol: TCP
 targetPort: 80
 selector:
 app: weatherforecast-aks
 component: weatherforecast-app
 type: LoadBalancer

Step 7

You can see the status and details about pods and services using a few commands as I showed below

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 8

If you want more details related to pods and services then execute describe commands related to Kubernetes

kubectl describe pod <podname>
kubectl describe svc <service name>

Step 9

Open the AKS on Azure and click on Services and ingresses inside that we can see weather forecast details like external IP addresses and ports.

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Step 10

Finally, hit the public IP URL to fetch data

http://20.204.233.158:8080/weatherforecast

Deploy and Run .NET Core 7 Web API on Azure Kubernetes Service (AKS)

Conclusion

Here we discuss the Containerization of applications using Azure container registry and instance. Also, the step-by-step like how to create the resource and run it on Azure.

Happy Learning!