Skip to content

Deploying a Simple .NET 8.0 Application to Azure Kubernetes Service (AKS)

With the release of .NET 8.0, developers are provided with a robust and modern framework to build web applications. Azure Kubernetes Service (AKS) is a powerful platform to deploy, manage, and scale containerized applications in the cloud. In this blog post, we will walk through the process of deploying a simple calculator application built with .NET 8.0 to AKS.

Prerequisites

Before we begin, ensure you have the following prerequisites:

  1. Azure Subscription: An active Azure account. If you don’t have one, you can create a free account.
  2. Azure CLI: Installed and configured to access your Azure account.
  3. Docker: Installed and running on your local machine for building container images.
  4. Kubectl: Installed and configured to interact with your Kubernetes cluster.
  5. .NET 8.0 SDK: Installed to develop and run the .NET application.

Step 1: Create a Simple Calculator Application in .NET 8.0

Let’s start by creating a simple calculator application using .NET 8.0. The application will provide basic arithmetic operations through a REST API.

1.1 Create the .NET Project

Open a terminal and run the following commands to create a new ASP.NET Core Web API project:

dotnet new webapi -n CalculatorApp
cd CalculatorApp

1.2 Implement the Calculator API

In the newly created project, open the Controllers folder and create a new file named CalculatorController.cs. Add the following code:

using Microsoft.AspNetCore.Mvc;

namespace CalculatorApp.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CalculatorController : ControllerBase
    {
        [HttpGet("add")]
        public ActionResult<double> Add(double a, double b) => Ok(a + b);

        [HttpGet("subtract")]
        public ActionResult<double> Subtract(double a, double b) => Ok(a - b);

        [HttpGet("multiply")]
        public ActionResult<double> Multiply(double a, double b) => Ok(a * b);

        [HttpGet("divide")]
        public ActionResult<double> Divide(double a, double b)
        {
            if (b == 0) return BadRequest("Cannot divide by zero.");
            return Ok(a / b);
        }
    }
}

This code defines four basic calculator operations: addition, subtraction, multiplication, and division.

1.3 Test the Application Locally

Run the application locally to ensure it’s working correctly:

dotnet run

Navigate to https://localhost:5001/api/calculator/add?a=5&b=3 to test the addition endpoint. You should see a result of 8.

Step 2: Containerize the Application with Docker

To deploy the application to AKS, we need to containerize it using Docker.

2.1 Create a Dockerfile

In the root directory of your project, create a Dockerfile with the following content:

# Use the official .NET 8.0 SDK image as a build environment
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app

# Copy the .csproj file and restore dependencies
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the application code and build it
COPY . ./
RUN dotnet publish -c Release -o out

# Use the official .NET runtime as the runtime environment
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build-env /app/out .

# Expose port 80
EXPOSE 80

# Run the application
ENTRYPOINT ["dotnet", "CalculatorApp.dll"]

2.2 Build the Docker Image

Build the Docker image using the Docker CLI:

docker build -t calculatorapp:latest .

2.3 Test the Docker Image Locally

Run the Docker container locally to ensure everything works as expected:

docker run -p 8080:80 calculatorapp:latest

Visit http://localhost:8080/api/calculator/add?a=5&b=3 to test the API.

Step 3: Push the Docker Image to Azure Container Registry (ACR)

Azure Container Registry (ACR) is a managed Docker registry service used to store and manage container images.

3.1 Create an Azure Container Registry

Run the following commands to create an ACR instance:

az group create --name myResourceGroup --location eastus
az acr create --resource-group myResourceGroup --name myacrname --sku Basic

3.2 Login to ACR and Push the Image

Log in to your Azure Container Registry and push the Docker image:

az acr login --name myacrname
docker tag calculatorapp:latest myacrname.azurecr.io/calculatorapp:latest
docker push myacrname.azurecr.io/calculatorapp:latest

Step 4: Create an Azure Kubernetes Service (AKS) Cluster

4.1 Create an AKS Cluster

Create an AKS cluster using the Azure CLI:

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

4.2 Connect to the AKS Cluster

Get the Kubernetes credentials to interact with your AKS cluster:

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

Step 5: Deploy the Application to AKS

5.1 Create a Kubernetes Deployment and Service YAML File

Create a file named calculator-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: calculator-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: calculator
  template:
    metadata:
      labels:
        app: calculator
    spec:
      containers:
      - name: calculator
        image: myacrname.azurecr.io/calculatorapp:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: calculator-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: calculator

5.2 Deploy to AKS

Apply the deployment and service configuration using kubectl:

kubectl apply -f calculator-deployment.yaml

5.3 Verify the Deployment

Check the status of the pods and service:

kubectl get pods
kubectl get svc

Once the calculator-service is in a Running state, note the external IP address assigned to it. This will be the endpoint for accessing your calculator application.

Step 6: Test the Deployed Application

Visit the external IP of the service and test the API endpoints. For example:

http://<EXTERNAL-IP>/api/calculator/add?a=5&b=3

You should see the expected result.

Conclusion

Deploying a simple .NET 8.0 application to Azure Kubernetes Service (AKS) involves several steps: creating the application, containerizing it with Docker, pushing it to Azure Container Registry, creating an AKS cluster, and deploying the application using Kubernetes manifests. By following these steps, you can efficiently deploy and manage your .NET applications in the cloud with AKS. This approach is scalable, reliable, and suitable for production environments. Happy deploying!

Published inAzure Kubernetes Service (AKS)
LinkedIn
Share
WhatsApp