Deploying a .NET Application on Azure Kubernetes Service (AKS) - Step-by-Step Guide

Deploying a .NET application on Azure Kubernetes Service (AKS) involves several steps to ensure that your application is containerized, deployed, and running in the AKS cluster. Here's a guide to help you through the process:

Prerequisites:

  1. Azure Subscription: You need an Azure subscription. If you don’t have one, create it here.
  2. Azure CLI: Install the Azure CLI if you haven’t already. Install Azure CLI.
  3. Kubernetes CLI (kubectl): Install kubectl to interact with your AKS cluster. Install kubectl.
  4. Docker: Ensure Docker is installed for building the container image. Install Docker.
  5. .NET SDK: Install the .NET SDK to build your .NET application. Install .NET SDK.

Steps for Deployment:

1. Prepare the .NET Application

  • First, ensure your .NET application is ready for deployment. If you don't have a .NET app yet, you can create a new one:
    dotnet new webapi -n MyDotNetApp
    cd MyDotNetApp

2. Create a Dockerfile

  • In the root of your project directory, create a Dockerfile to containerize your .NET application. Here’s an example Dockerfile for a .NET Core Web API:
    # Use the official image as the base
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80
    
    # Use the SDK to build the application
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    COPY ["MyDotNetApp/MyDotNetApp.csproj", "MyDotNetApp/"]
    RUN dotnet restore "MyDotNetApp/MyDotNetApp.csproj"
    COPY . .
    WORKDIR "/src/MyDotNetApp"
    RUN dotnet build "MyDotNetApp.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "MyDotNetApp.csproj" -c Release -o /app/publish
    
    # Copy the build to the final container image
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "MyDotNetApp.dll"]

3. Build the Docker Image

  • Build the Docker image using the following command:
    docker build -t mydotnetapp .

4. Push the Docker Image to Azure Container Registry (ACR)

  • Create an Azure Container Registry (ACR) if you don’t have one:
    az acr create --resource-group <resource-group-name> --name <acr-name> --sku Basic
    
  • Log in to your Azure Container Registry:
    az acr login --name <acr-name>
  • Tag and push the Docker image:
    docker tag mydotnetapp <acr-name>.azurecr.io/mysampleapp:v1
    docker push <acr-name>.azurecr.io/mysampleapp:v1

5. Create AKS Cluster

  • Create an AKS cluster:
    az aks create --resource-group <resource-group-name> --name <aks-cluster-name> --node-count 3 --enable-addons monitoring --generate-ssh-keys
    
  • Get the credentials to access the AKS cluster:
    az aks get-credentials --resource-group <resource-group-name> --name <aks-cluster-name>

6. Create Kubernetes Deployment YAML File

  • Create a deployment.yaml file for your application. Here’s an example:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mydotnetapp-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: mydotnetapp
      template:
        metadata:
          labels:
            app: mydotnetapp
        spec:
          containers:
          - name: mydotnetapp
            image: <acr-name>.azurecr.io/mysampleapp:v1
            ports:
            - containerPort: 80

7. Create Kubernetes Service YAML File

  • Create a service.yaml file to expose your application:
    apiVersion: v1
    kind: Service
    metadata:
      name: mydotnetapp-service
    spec:
      selector:
        app: mydotnetapp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer

8. Deploy the Application to AKS

  • Apply the Kubernetes configuration files to deploy the application and service:
    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml

9. Verify the Deployment

  • Check if the pods are running:
    kubectl get pods
  • Check the service to get the external IP:
    kubectl get service mydotnetapp-service

10. Access Your Application

  • Once the external IP is assigned, you can access your application through the browser or API client by visiting the external IP on port 80.

11. Scale and Manage the Application

  • You can scale the number of replicas:
    kubectl scale deployment mydotnetapp-deployment --replicas=5
  • To view logs for troubleshooting:
    kubectl logs <pod-name>

Optional: Set Up CI/CD for AKS Deployment

You can automate this process by setting up a CI/CD pipeline in Azure DevOps, GitHub Actions, or other CI/CD tools. This way, you can build and deploy your .NET application automatically every time there’s a new commit or a change to the Docker image.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete