Building a Cloud-Native Application with Azure and .NET
Obinna Isiwekpeni
Posted on February 6, 2023
Cloud-native applications are applications designed and built to use cloud-computing infrastructure. These applications are run and hosted in the cloud and use the characteristics of a cloud-computing software delivery model.
There are different cloud-computing providers, such as Amazon Web Services (AWS), Google Cloud Platform (GCP), Microsoft Azure, and many more. In this tutorial, you will use Microsoft Azure.
Prerequisites
To follow in this tutorial, you need the following:
You also need to do some customizations to Visual Studio Code and Docker.
On Docker, Kubernetes should be enabled.
On Visual Studio Code you need to install the following extensions:
- YAML 1.11.10112022
- C# 1.25.0
- dotnet 1.3.0
- NuGet Package Manager 1.1.6
- Azure Account 0.11.2
- Azure CLI Tools 0.5.0
- Docker 1.22.1
- Kubernetes 1.3.10
- Kubernetes Support 0.1.9
Connecting Visual Studio Code to your Azure Subscription
To connect your Visual Studio Code to your Azure subscription, do the following:
- Open your terminal and enter the command below to login to Azure.
az login
On Visual Studio Code, use the Ctrl+Shift+P shortcut for Windows or Cmd+Shift+P for Mac to open the Command Palette. Then, search for Azure and sign in to link your VS Code instance with your Azure login.
After clicking on it, a browser tab or window will open for you to choose the Microsoft account with your Azure subscription.
Use the Command Palette in Visual Studio Code to select the active subscription.
Containerize a .NET Core Worker Service
Use the Visual Studio Code Command Palette to create a new .NET Core application.
Create a folder and name the folder the project name. In this case, WorkerService and press enter. This creates a WorkerService project.
Open the terminal window in Visual Studio Code and type the command below to run the created Worker Service.
dotnet run
- Open the Worker.cs file and the
ExecuteAsync
method is simply logging some information. Modify the log message to look like this:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation($"Worker Service running on {Environment.MachineName}");
await Task.Delay(3000, stoppingToken);
}
}
Run the code above, and it logs your machine name to the terminal every 3 seconds.
Create a Docker container from this code by opening the Command Palette in Visual Studio Code using the command that creates a Docker file.
Select the .NET Core Console Application for Linux. Do not include Docker Compose files.
Open the Dockerfile created and right click on it to build a docker image.
The Docker image should be available after building it. Verify this by either using the Docker pane in Visual Studio Code or typing the command
docker image ls
in your terminal.
Test the Docker image by running it as a Container. Right-click on the latest tag and click on run. The list of running containers can be found in the Container pane. Right-click on the running container to view the logs.
The log messages show the container on Docker where the application is running.
Lastly, stop the container to free up some resources.
Create a Resource Group in your Azure Subscription
To create a Resource Group (RG), do the following:
Open the Azure Portal and login. Then, create a new resource group and name it
rg-workerservice
. Then click on the Review+create button.
The Resource Group is created at this point and ready for use.
Create the Azure Kubernetes Service (AKS) resource in your Azure Subscription
Use the Azure Portal to create a new Azure Kubernetes Service (AKS). The steps are similar to that of creating a Resource Group.
On the Home page of Azure Portal, search for Azure Kubernetes Service and click on the Create button.
Enter the information needed to create the Kubernetes cluster. Use the information provided in the image below. Then click on the
Review+create
button.
The deployment process takes some time.
Create the Azure Container Registry (ACR) resource in your Azure Subscription
- To create a new Azure Container Registry, use the Azure Portal
The default settings in the wizard are enough for this tutorial, so you can go ahead and click on the Review + create button. The container registry will be used to push the Docker images to Azure Kubernetes Service and consumed from there. Therefore, the Kubernetes cluster needs access to the container registry.
- Run the following command to grant the cluster access to the registry:
az aks update -g <resource-group> -n <aks-name> --attach-acr <acr-name>
In Visual Studio Code, open Kubernetes in the Activity Bar and select your Kubernetes cluster from the Azure Cloud section.
Right click on your Kubernetes cluster to merge it into your local Kubeconfig.
You can view the different Kubernetes clusters in the Clusters section. Right click on your cluster and set it as the current cluster.
Deploy the Worker Service Inside the Kubernetes Cluster
To deploy the WorkserService inside the Kubernetes cluster, do the following:
- Open the Docker section in Visual Studio Code and right-click on the latest tag of the docker image built in the previous step to push it to the container registry.
If the push was successful, a copy of the Docker image should be present in the Registries section
Open the Kubernetes pane in Visual Studio Code and open your cluster’s namespaces list
To create a new namespace, double-click on the default namespace and modify the YAML script using the content below
apiVersion: v1
kind: Namespace
metadata:
name: workerservice
spec:
finalizers:
- kubernetes
status:
phase: Active
Open the command palette and type in
Kubernetes Apply
to apply the namespace.
Confirm the creation of the namespace. It should appear in the Namespaces section.
Right-click on the newly created namespace and click on Use Namespace.
Create a file deployment-workerservice.yaml by double-clicking on the workerservice namespace ****and add the following content to it.
apiVersion: apps/v1
kind: Deployment
metadata:
name: workerservice
spec:
replicas: 1
selector:
matchLabels:
app: workerservice
template:
metadata:
labels:
app: workerservice
spec:
containers:
- name: workerservice
image: obinna.azurecr.io/workerservice:latest
resources:
limits:
memory: "128Mi"
cpu: "500m"
The file includes configuration steps for the deployment of the container into the Kubernetes cluster. It defines the number of replicas of the container and the resources in terms of memory and CPU to be assigned to the container.
Use the
Kubernetes Apply
command to create the deployment configuration file. Then, you can find the running Kubernetes Pod by looking in the Kubernetes activity pane and expanding your Workloads, Deployments and Pods
Right-click on the running Pod and select Logs to see the console output from the WorkerService
Here we can see the logs of our WorkerService application running in a Kubernetes cluster.
It is worth noting that these steps can be automated by integrating them in a CI/CD pipeline.
Conclusion
This tutorial provides a step by step process on how to build and deploy a cloud native application in Azure and .NET.
Posted on February 6, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.