Microsoft Azure provides a robust platform for creating and managing Kubernetes clusters. In this blog post, we will walk you through the process of setting up a minimal Kubernetes cluster in Azure and then demonstrate how to create a simple C# service and deploy it to the Azure Kubernetes Service (AKS). Additionally, we’ll write a C# program that calls this service within the AKS cluster.
Prerequisites
Before you begin, make sure you have the following prerequisites in place:
- An active Microsoft Azure account.
- Azure CLI installed on your local machine.
kubectl
command-line tool installed on your local machine.- A code editor and a basic understanding of C#.
Step 1: Create an Azure Kubernetes Service (AKS) Cluster
- Login to Azure: Open a terminal and log in to your Azure account using the Azure CLI.
az login
- Create a Resource Group: If you haven’t already, create a resource group for your AKS cluster. Replace
<resource-group-name>
with your preferred name and<location>
with your desired Azure region.
az group create --name <resource-group-name> --location <location>
- Create the AKS Cluster: Use the following command to create a basic AKS cluster. Replace
<cluster-name>
,<node-count>
, and<node-vm-size>
with appropriate values.
az aks create --resource-group <resource-group-name> --name <cluster-name> --node-count <node-count> --node-vm-size <node-vm-size> --enable-addons monitoring --generate-ssh-keys
- Get AKS Credentials: After the cluster is created, get the credentials for
kubectl
to access the AKS cluster:
az aks get-credentials --resource-group <resource-group-name> --name <cluster-name>
Step 2: Create a C# Service
In this example, we’ll create a simple C# web service using ASP.NET Core.
- Initialize a New ASP.NET Core Web API Project: Open a terminal and run the following commands to create a new ASP.NET Core web API project:
dotnet new webapi -n SampleApi
cd SampleApi
- Add a Sample Controller: Modify the
ValuesController.cs
file to include a simple controller with an endpoint. Here’s an example:
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok(new { Message = "Hello from Azure AKS!" });
}
}
- Build and Run the Application: Build and run your application locally:
dotnet build
dotnet run
Your C# service should be running locally at http://localhost:5000
.
Step 3: Containerize the Service
To deploy the C# service to AKS, you need to containerize it.
- Create a Dockerfile: Create a Dockerfile in the project directory with the following content:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["SampleApi.csproj", "./"]
RUN dotnet restore "SampleApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SampleApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SampleApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SampleApi.dll"]
- Build the Docker Image: Build the Docker image by running the following commands:
docker build -t sample-api .
- Run the Docker Container Locally: Run the Docker container locally to ensure it’s working as expected:
docker run -d -p 8080:80 --name sample-api sample-api
Your C# service should be running inside a Docker container at http://localhost:8080
.
Step 4: Deploy the C# Service to AKS
- Push the Docker Image to Azure Container Registry (ACR): You need to have an Azure Container Registry (ACR) set up for this step. If you haven’t already, create an ACR in your Azure subscription.
- Log in to your ACR:
az acr login --name <acr-name>
- Tag the Docker image:
docker tag sample-api <acr-name>.azurecr.io/sample-api:v1
- Push the Docker image to ACR:
docker push <acr-name>.azurecr.io/sample-api:v1
- Deploy the Service to AKS: Create a Kubernetes deployment and service to deploy your C# service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-api-deployment
spec:
replicas: 2
selector:
matchLabels:
app: sample-api
template:
metadata:
labels:
app: sample-api
spec:
containers:
- name: sample-api
image: <acr-name>.azurecr.io/sample-api:v1
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: sample-api-service
spec:
selector:
app: sample-api
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Save this YAML configuration to a file, for example, sample-api-deployment.yaml
. Then, apply it to your AKS cluster:
kubectl apply -f sample-api-deployment.yaml
- Access the Deployed Service: Find the external IP address of your service using the following command:
kubectl get svc sample-api-service
Your C# service should be accessible at the provided external IP.
Step 5: Write a C# Program to Call the Service
Now, you can create a C# program to call the deployed service within the AKS cluster. You can use the HttpClient
class to make HTTP requests to the service. Here’s an example program:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new HttpClient();
var response = await client.GetStringAsync("http://<service-external-ip>/api/values");
Console.WriteLine("Response from the deployed service:");
Console.WriteLine(response);
}
}
Replace <service-external-ip>
with the actual external IP of your service.
Compile and run this program to make a request to your C# service in the AKS cluster.
Conclusion
You’ve successfully set up a minimal Kubernetes cluster in Microsoft Azure, created a simple C# service, containerized it, and deployed it to the Azure Kubernetes Service (AKS). You also wrote a C# program to call this service within the AKS cluster.
This demonstration provides a foundation for building and deploying more complex applications in Azure using Kubernetes. With AKS, you can easily scale and manage your containerized applications, making it a powerful platform for modern application development. Happy coding!