Autoscaling Using Kubernetes in .NET Microservices: Product and Order Services

DotNet Full Stack Dev
3 min readAug 14, 2024

--

In a microservices architecture, it is crucial to manage resources efficiently, especially when dealing with varying loads. Autoscaling is a powerful feature of Kubernetes that automatically adjusts the number of pod replicas based on the demand. This ensures that your services are always available and performant without over-provisioning resources. In this blog, we’ll explore how to implement autoscaling for 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.

Understanding Autoscaling in Kubernetes

Kubernetes offers different types of autoscaling mechanisms:

  • Horizontal Pod Autoscaler (HPA): Scales the number of pod replicas based on CPU utilization or custom metrics.
  • Vertical Pod Autoscaler (VPA): Adjusts the CPU and memory requests/limits of containers in a pod.
  • Cluster Autoscaler: Scales the number of nodes in a cluster based on the resource requirements of pods.

For this blog, we’ll focus on Horizontal Pod Autoscaler (HPA), which is the most commonly used autoscaling method in Kubernetes.

Setting Up the Environment

Before we start with autoscaling, ensure you have the following:

  • Kubernetes cluster (e.g., Minikube, AKS, EKS, GKE)
  • kubectl installed
  • Metrics Server installed in your Kubernetes cluster
  • Docker images for Product and Order services

Example .NET Microservices

For simplicity, let’s assume we have two .NET Core Web API projects: ProductService and OrderService. Each service has been containerized using Docker.

ProductService Dockerfile

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

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProductService.dll"]

OrderService Dockerfile

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

FROM mcr.microsoft.com/dotnet/sdk:6.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"]

Deploying the Microservices

First, let’s deploy the Product and Order services to the Kubernetes cluster.

ProductService Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
name: product-service
spec:
replicas: 2
selector:
matchLabels:
app: product-service
template:
metadata:
labels:
app: product-service
spec:
containers:
- name: product-service
image: your-docker-repo/product-service:latest
ports:
- containerPort: 80
resources:
requests:
cpu: "200m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"

OrderService Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
replicas: 2
selector:
matchLabels:
app: order-service
template:
metadata:
labels:
app: order-service
spec:
containers:
- name: order-service
image: your-docker-repo/order-service:latest
ports:
- containerPort: 80
resources:
requests:
cpu: "200m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"

Enabling Horizontal Pod Autoscaler (HPA)

To enable HPA, we need to define a YAML file that specifies the scaling policy for each service. The HPA will monitor the CPU usage and automatically scale the pods when the defined threshold is exceeded.

ProductService HPA YAML

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

OrderService HPA YAML

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

Applying the Deployments and HPA

Apply the deployments and HPA configurations using kubectl:

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

Testing Autoscaling

To test autoscaling, you can simulate high CPU usage. One common way is to use a load testing tool like hey or wrk to generate traffic to the services.

Example using hey:

hey -z 30s -c 50 http://<product-service-url>/api/products
hey -z 30s -c 50 http://<order-service-url>/api/orders

While the load test is running, you can monitor the number of pods using:

kubectl get hpa
kubectl get pods

You should see the number of pods increase as the CPU utilization threshold is exceeded.

Monitoring and Scaling Down

Kubernetes HPA will automatically scale down the number of pods when the load decreases. This ensures that resources are not wasted during periods of low demand.

Conclusion

Autoscaling in Kubernetes is a critical feature for managing microservices in a dynamic environment. By implementing autoscaling for the Product and Order services, we can ensure that our application remains responsive under varying loads, while also optimizing resource usage. Kubernetes’ HPA makes it easy to scale your .NET microservices automatically based on CPU utilization or custom metrics, providing both efficiency and resilience to your microservices architecture.

--

--

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

No responses yet