Container Orchestration with Kubernetes in .NET Microservices: Product and Order Services

DotNet Full Stack Dev
3 min readJul 31, 2024

--

Container orchestration has become a crucial component in managing microservices architecture, especially in environments where scalability, availability, and efficient resource utilization are key. Kubernetes (K8s) is one of the most popular container orchestration platforms, offering powerful features for automating deployment, scaling, and management of containerized applications.

In this blog, we’ll explore how to use Kubernetes to orchestrate two .NET microservices: Product and Order services.

Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. Uncover more by visiting our https://dotnet-fullstack-dev.blogspot.com reach out for further information.

Kubernetes

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating containerized applications. It groups containers that make up an application into logical units for easy management and discovery. Kubernetes provides features such as:

  • Automated Rollouts and Rollbacks: Manage updates to applications without downtime.
  • Service Discovery and Load Balancing: Automatically assign containers with IP addresses and a DNS name.
  • Storage Orchestration: Automatically mount the storage system of your choice.
  • Self-healing: Restarts containers that fail, replaces, and reschedules containers when nodes die.
  • Horizontal Scaling: Scale applications up or down based on load.

Setting Up the Environment

For this demonstration, assume we have two microservices:

  1. Product Service: Manages product data.
  2. Order Service: Manages customer orders.

Both services are containerized using Docker and need to be deployed in a Kubernetes cluster.

Dockerizing the .NET Microservices

Before deploying to Kubernetes, we need to containerize the .NET applications. Here’s an example Dockerfile for the Product service:

Dockerfile for Product Service:

# Use the official .NET image as a build stage
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

# Copy the csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o /out

# Use the official .NET runtime image for a smaller final image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /out .
ENTRYPOINT ["dotnet", "ProductService.dll"]

Dockerfile for Order Service: Similar structure to the Product service, adjusted for the Order service.

Creating Kubernetes Manifests

To deploy the microservices in Kubernetes, we need to create Kubernetes manifests for each service, including Deployments and Services.

Deployment for Product Service:

apiVersion: apps/v1
kind: Deployment
metadata:
name: product-service
spec:
replicas: 3
selector:
matchLabels:
app: product-service
template:
metadata:
labels:
app: product-service
spec:
containers:
- name: product-service
image: product-service:latest
ports:
- containerPort: 80

Service for Product Service:

apiVersion: v1
kind: Service
metadata:
name: product-service
spec:
selector:
app: product-service
ports:
- protocol: TCP
port: 80
targetPort: 80

Deployment and Service for Order Service: Similar to the Product service, with appropriate names and labels.

Deploying to Kubernetes

With the manifests defined, deploy the microservices to your Kubernetes cluster.

kubectl apply -f product-deployment.yaml
kubectl apply -f product-service.yaml
kubectl apply -f order-deployment.yaml
kubectl apply -f order-service.yaml

Service Discovery and Load Balancing

Kubernetes provides built-in service discovery and load balancing. The Services defined in the manifests expose the microservices within the cluster, allowing other services to communicate with them via DNS names, such as product-service and order-service.

Scaling and Auto-scaling

Kubernetes makes it easy to scale applications. For example, you can scale the Product service up to 5 replicas with a simple command:

kubectl scale deployment/product-service --replicas=5

Auto-scaling can also be configured based on CPU usage or custom metrics using the Horizontal Pod Autoscaler (HPA).

Example HPA:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: product-service
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: product-service
minReplicas: 3
maxReplicas: 10
targetCPUUtilizationPercentage: 50

Monitoring and Logging

Monitoring and logging are critical in a microservices architecture. Kubernetes integrates with monitoring tools like Prometheus and Grafana, and logging solutions like Fluentd and Elasticsearch.

Prometheus and Grafana can be used to collect and visualize metrics from the Product and Order services, providing insights into performance and health.

9. Conclusion

Kubernetes provides a robust platform for managing containerized applications at scale. By leveraging Kubernetes, we can efficiently manage the deployment, scaling, and monitoring of our .NET microservices, ensuring high availability and resilience.

In our example, the Product and Order services are containerized, deployed, and managed using Kubernetes. The setup provides seamless service discovery, load balancing, scaling, and monitoring capabilities, making it an ideal solution for microservices architecture.

Kubernetes’ capabilities, combined with containerized microservices, provide a powerful infrastructure that can handle complex and dynamic application requirements, making it a cornerstone technology in modern software development.

You may also like : https://medium.com/@siva.veeravarapu/aws-step-functions-for-workflow-orchestration-streamlining-order-processing-in-net-7b24ac28f017

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

Responses (1)