DEV Community

Obinna Isiwekpeni
Obinna Isiwekpeni

Posted on

Building a Cloud-Native Application with Azure and .NET

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.

Enabling Docker on Kubernetes

On Visual Studio Code you need to install the following extensions:

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
Enter fullscreen mode Exit fullscreen mode
  • 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.
    Connecting VS Code to Azure

  • 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.
    Selecting your Azure subscriptions

Containerize a .NET Core Worker Service

  • Use the Visual Studio Code Command Palette to create a new .NET Core application.
    Creating a new .NET Core application

  • Select the Worker Service template.
    Selecting a Worker Service template

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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);
      }
    }
Enter fullscreen mode Exit fullscreen mode

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.
    Creating a Dockerfile 1

  • Select the .NET Core Console Application for Linux. Do not include Docker Compose files.
    Creating a Dockerfile 2

Creating a Dockerfile 3

Creating a Dockerfile 4

  • Open the Dockerfile created and right click on it to build a docker image.
    Building the 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.
    Verifying Docker Image

  • 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.
    Viewing logs of application running in Docker

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.
    Creating Resource Group in Azure

  • 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.
    Creating Azure Kubernetes Service

  • Enter the information needed to create the Kubernetes cluster. Use the information provided in the image below. Then click on the Review+create button.
    Completing creation on Azure Kubernetes Service

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 Creating an Azure Container Registry

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>
Enter fullscreen mode Exit fullscreen mode
  • In Visual Studio Code, open Kubernetes in the Activity Bar and select your Kubernetes cluster from the Azure Cloud section.
    Selecting your Kubernetes cluster

  • Right click on your Kubernetes cluster to merge it into your local Kubeconfig.
    Merging your cluster 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.
    Setting your cluster 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. Pushing docker image to container registry

Successful push of docker image to the container registry

  • If the push was successful, a copy of the Docker image should be present in the Registries section
    Confirmation of successful push to container registry

  • Open the Kubernetes pane in Visual Studio Code and open your cluster’s namespaces list
    Viewing your cluster’s namespace 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
Enter fullscreen mode Exit fullscreen mode
  • Open the command palette and type in Kubernetes Apply to apply the namespace.
    Apply the namespace configuration

  • 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.
    Using your newly created 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"
Enter fullscreen mode Exit fullscreen mode

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
    Applying deployment config

  • Right-click on the running Pod and select Logs to see the console output from the WorkerService
    Application running in a Kubernetes cluster

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.

Top comments (0)