Azure Kubernetes Service with .NET Core

Introduction

In today's rapidly evolving software landscape, developers are increasingly turning to containerization and orchestration platforms to deploy and manage their applications efficiently. Azure Kubernetes Service (AKS) offers a managed Kubernetes service that simplifies the deployment, management, and scaling of containerized applications in Azure. When combined with the versatility and performance of .NET Core, developers can build and deploy modern, scalable applications with ease. In this guide, we'll explore how to leverage Azure Kubernetes Service with .NET Core, providing step-by-step instructions and real-world examples.

What is Azure Kubernetes Service (AKS)?

Azure Kubernetes Service (AKS) is a fully managed Kubernetes service provided by Microsoft Azure. Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. AKS abstracts away the complexities of managing Kubernetes clusters, allowing developers to focus on building and deploying their applications without worrying about the underlying infrastructure.

What is .NET Core?

.NET Core is a cross-platform, open-source framework for building modern, high-performance applications. It allows developers to build applications using C# or F# and run them on Windows, macOS, or Linux. With its lightweight runtime and support for containerization, .NET Core is well-suited for building microservices and cloud-native applications.

Getting Started with Azure Kubernetes Service and .NET Core

Prerequisites

Before we begin, make sure you have the following prerequisites installed:

  • Azure CLI: Install the Azure CLI
  • Docker: Install Docker Desktop for your operating system
  • .NET Core SDK: Install the .NET Core SDK

Step 1. Create an Azure Kubernetes Service Cluster

  1. Open a terminal or command prompt and login to your Azure account using the Azure CLI:

    az login
  2. Create a resource group for your AKS cluster:

    az group create --name myResourceGroup --location eastus
  3. Create an AKS cluster:

    az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys

Step 2. Install and Configure the Kubernetes CLI (kubectl)

  1. Install the Kubernetes CLI (kubectl) using the Azure CLI:

    az aks install-cli
  2. Get the credentials for your AKS cluster:

    az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

Step 3. Build and Containerize a .NET Core Application

  1. Create a new .NET Core web application:

    dotnet new webapi -n MyApp
  2. Navigate to the project directory:

    cd MyApp
  3. Build the .NET Core application:

    dotnet build
  4. Create a Dockerfile in the project directory:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
    WORKDIR /src
    COPY ["MyApp.csproj", "."]
    RUN dotnet restore "./MyApp.csproj"
    COPY . .
    WORKDIR "/src/."
    RUN dotnet build "MyApp.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "MyApp.dll"]
    
  5. Build and tag the Docker image:

    docker build -t myapp:v1 .
  6. Test the Docker image locally:

    docker run -d -p 8080:80 myapp:v1
  7. Open a web browser and navigate to http://localhost:8080 to verify that the application is running.

Step 4. Deploy the Application to Azure Kubernetes Service (AKS)

  1. Push the Docker image to a container registry (e.g., Azure Container Registry):

    az acr login --name <registry_name>
    docker tag myapp:v1 <registry_name>.azurecr.io/myapp:v1
    docker push <registry_name>.azurecr.io/myapp:v1
  2. Deploy the application to AKS using a Kubernetes deployment manifest (deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: myapp
      template:
        metadata:
          labels:
            app: myapp
        spec:
          containers:
          - name: myapp
            image: <registry_name>.azurecr.io/myapp:v1
            ports:
            - containerPort: 80
    
  3. Apply the deployment manifest to create the deployment:

    kubectl apply -f deployment.yaml
  4. Expose the deployment as a Kubernetes service:

    kubectl expose deployment myapp-deployment --type=LoadBalancer --port=80 --target-port=80
  5. Get the external IP address of the service:

    kubectl get service myapp-deployment
  6. Open a web browser and navigate to the external IP address to access the application running in AKS.

Conclusion

In this article, we've explored how to leverage Azure Kubernetes Service (AKS) with .NET Core to build and deploy containerized applications in Azure. By following the steps outlined above, you can create an AKS cluster, containerize a .NET Core application, and deploy it to AKS with ease. Whether you're building microservices, web applications, or APIs, AKS and .NET Core provide a powerful combination for modern application development in the cloud. Happy learning!


Similar Articles