DEV Community

Cover image for Building Scalable Microservices with .NET 8.0 and Kubernetes
Syed Nadeem Hussain
Syed Nadeem Hussain

Posted on • Originally published at geek-of-javascript.blogspot.com

Building Scalable Microservices with .NET 8.0 and Kubernetes

Microservices architecture has revolutionized software development, enabling scalable and maintainable applications. With the latest .NET 8.0, building microservices is more efficient and powerful than ever. Here’s a quick guide to get you started:

Setting Up Your Environment

  1. Install .NET 8.0 SDK
  2. Install Docker
  3. Set Up Kubernetes

Developing Your Microservices

Create your microservices with ASP.NET Core Web API. Here’s a simple example:

OrderService/OrderController.cs:

using Microsoft.AspNetCore.Mvc;

namespace OrderService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OrderController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetOrders()
        {
            var orders = new List<string> { "Order1", "Order2", "Order3" };
            return Ok(orders);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Containerizing with Docker

Create Dockerfiles to containerize your microservices:

OrderService/Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["OrderService/OrderService.csproj", "OrderService/"]
RUN dotnet restore "OrderService/OrderService.csproj"
COPY . .
WORKDIR "/src/OrderService"
RUN dotnet build "OrderService.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "OrderService.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "OrderService.dll"]
Enter fullscreen mode Exit fullscreen mode

Deploying to Kubernetes

Deploy your Dockerized microservices to Kubernetes with simple manifests:

OrderService/k8s-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: orderservice-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: orderservice
  template:
    metadata:
      labels:
        app: orderservice
    spec:
      containers:
      - name: orderservice
        image: orderservice:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: orderservice
spec:
  selector:
    app: orderservice
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
Enter fullscreen mode Exit fullscreen mode

Learn More

To dive deeper into building and scaling microservices with .NET 8.0 and Kubernetes, read the full blog post here.

Top comments (0)