Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Kubernetes Deployment of ASP.NET Core Microservices - Full Walkthrough

Kubernetes Deployment of ASP.NET Core Microservices - Full Walkthrough

Date- May 22,2026 84
kubernetes aspnetcore

Overview

The rise of microservices architecture has transformed how applications are built and deployed. Microservices allow developers to create small, independent services that communicate over well-defined APIs, making the system more modular and easier to maintain. This approach solves several problems associated with monolithic applications, such as scalability issues, slower deployment cycles, and difficulty in managing large codebases.

ASP.NET Core is a powerful framework for building modern web applications and services. It is cross-platform, meaning you can run it on Windows, macOS, or Linux, which is ideal for microservices. When combined with Kubernetes, a container orchestration platform, it provides a robust solution for deploying, scaling, and managing microservices in production environments. Real-world use cases include e-commerce platforms, SaaS applications, and enterprise-grade solutions where multiple services need to work together seamlessly.

Prerequisites

  • ASP.NET Core Knowledge: Familiarity with building applications using ASP.NET Core framework.
  • Docker: Understanding of containerization and how to create Docker images.
  • Kubernetes: Basic knowledge of Kubernetes concepts like Pods, Services, and Deployments.
  • Development Environment: A local setup with Visual Studio, Docker, and kubectl installed.
  • Cloud Platform: An account with a cloud provider that supports Kubernetes (e.g., Google Cloud, Azure, AWS).

Setting Up ASP.NET Core Microservices

Before deploying to Kubernetes, you need to create a simple ASP.NET Core microservice. This service will serve as the foundation for our deployment. To start, create a new ASP.NET Core Web API project.

dotnet new webapi -n MyMicroservice

This command generates a new Web API project named MyMicroservice. The default template includes a sample controller. You can explore the structure of the generated project to understand how ASP.NET Core applications are organized.

Creating a Sample Controller

Next, let's create a sample controller that will return a simple JSON response. Open the Controllers folder and create a new file named SampleController.cs.

using Microsoft.AspNetCore.Mvc;

namespace MyMicroservice.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SampleController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok(new { message = "Hello from MyMicroservice!" });
        }
    }
}

This controller defines a GET endpoint that returns a JSON object with a greeting message. The Ok() method returns a 200 OK response along with the specified data.

Testing the Microservice Locally

To test your microservice, run the application using the following command:

dotnet run

Once the application is running, navigate to http://localhost:5000/api/sample in your browser or use Postman to send a GET request. You should see the following JSON response:

{
  "message": "Hello from MyMicroservice!"
}

Containerizing the ASP.NET Core Microservice

To deploy the microservice on Kubernetes, you first need to create a Docker image. Docker allows you to package your application and its dependencies into a single container, ensuring consistency across different environments.

Creating a Dockerfile

Create a new file named Dockerfile in the root of your project with the following content:

# Use the official ASP.NET Core runtime as a parent image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

# Use the SDK image to build the application
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyMicroservice/MyMicroservice.csproj", "MyMicroservice/"]
RUN dotnet restore "MyMicroservice/MyMicroservice.csproj"
COPY . .
WORKDIR "/src/MyMicroservice"
RUN dotnet build "MyMicroservice.csproj" -c Release -o /app/build

# Publish the application
FROM build AS publish
RUN dotnet publish "MyMicroservice.csproj" -c Release -o /app/publish

# Create the final image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyMicroservice.dll"]

This Dockerfile defines a multi-stage build process. The first stage uses the official .NET SDK to build the application, while the second stage creates a runtime image for deployment. The ENTRYPOINT specifies the command to run the application when the container starts.

Building the Docker Image

To build the Docker image, run the following command from the root of your project:

docker build -t mymicroservice:1.0 .

This command builds the Docker image and tags it as mymicroservice:1.0. You can verify that the image was created by running:

docker images

Deploying to Kubernetes

Now that you have a Docker image of your microservice, the next step is to deploy it to a Kubernetes cluster. For this example, we'll assume you have a Kubernetes cluster running (you can use Minikube for local testing or a cloud provider for production).

Creating a Kubernetes Deployment

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mymicroservice-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mymicroservice
  template:
    metadata:
      labels:
        app: mymicroservice
    spec:
      containers:
        - name: mymicroservice
          image: mymicroservice:1.0
          ports:
            - containerPort: 80

This deployment.yaml file defines a Kubernetes deployment for your microservice. It specifies two replicas for high availability and uses the Docker image built earlier. The containerPort indicates the port that the application listens on inside the container.

Applying the Deployment

To create the deployment in your Kubernetes cluster, run:

kubectl apply -f deployment.yaml

You can verify that your deployment is running by executing:

kubectl get deployments

Exposing the Microservice

Once the deployment is successful, you need to expose your microservice so that it can be accessed from outside the Kubernetes cluster. This is typically done using a Kubernetes Service.

Creating a Service

Create a file named service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: mymicroservice-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: mymicroservice

This service.yaml file defines a Kubernetes Service of type LoadBalancer, which will expose your microservice to the internet. It listens on port 80 and forwards traffic to the pods running your microservice.

Applying the Service

To create the service, run:

kubectl apply -f service.yaml

After applying the service, you can retrieve the external IP address using:

kubectl get services

Edge Cases & Gotchas

While deploying ASP.NET Core microservices on Kubernetes, there are several common pitfalls to be aware of.

Incorrect Port Configuration

One common mistake is misconfiguring the ports in your deployment and service definitions. Ensure that the containerPort in the deployment matches the targetPort in the service. For instance:

# Incorrect
          ports:
            - containerPort: 5000
# Correct
          ports:
            - containerPort: 80

Resource Limits

Neglecting to set resource limits can lead to pods consuming excessive resources and impacting other services. Always define CPU and memory limits in your deployment specifications:

resources:
  limits:
    memory: "128Mi"
    cpu: "500m"
  requests:
    memory: "64Mi"
    cpu: "250m"

Performance & Best Practices

To achieve optimal performance when deploying ASP.NET Core microservices on Kubernetes, consider the following best practices.

Using Readiness and Liveness Probes

Implementing readiness and liveness probes helps Kubernetes manage the lifecycle of your application more effectively. These probes ensure that traffic is only routed to healthy pods. For example:

livenessProbe:
  httpGet:
    path: "/health"
    port: 80
  initialDelaySeconds: 30
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: "/health"
    port: 80
  initialDelaySeconds: 5
  periodSeconds: 10

Scaling Deployments

Take advantage of Kubernetes' scaling capabilities by adjusting the number of replicas based on demand. You can manually scale deployments or use the Horizontal Pod Autoscaler for automated scaling based on CPU or memory usage.

Real-World Scenario

Let’s tie everything together in a realistic mini-project. Imagine you are building a simple e-commerce application with an ASP.NET Core microservice that handles product information.

Creating the Product Microservice

Create a new ASP.NET Core Web API project named ProductService. Define a controller that manages product data:

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace ProductService.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductController : ControllerBase
    {
        private static List products = new List
        {
            "Product 1",
            "Product 2"
        };

        [HttpGet]
        public IActionResult GetProducts()
        {
            return Ok(products);
        }
    }
}

This controller defines a GET endpoint that returns a list of products. You can then build and containerize this microservice as previously described.

Deploying the Product Microservice

Follow the same steps to create the Docker image, deployment, and service for the ProductService. Ensure you have unique names for your deployment and service files.

Conclusion

  • Understanding microservices and the benefits they offer in modern application development.
  • How to create an ASP.NET Core microservice and containerize it using Docker.
  • Steps to deploy and expose your microservice on Kubernetes.
  • Common pitfalls and best practices to ensure a successful deployment.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating HashiCorp Vault for Effective Secrets Management in ASP.NET Core Applications
May 22, 2026
Docker Containerization of ASP.NET Core Apps: Mastering Dockerfile and Compose
May 22, 2026
Integrating AWS SQS and SNS in ASP.NET Core for Decoupled Microservices
May 10, 2026
Integrating RabbitMQ with ASP.NET Core Using MassTransit: A Complete Guide
May 10, 2026
Previous in ASP.NET Core
Docker Containerization of ASP.NET Core Apps: Mastering Dockerfil…
Next in ASP.NET Core
Integrating HashiCorp Vault for Effective Secrets Management in A…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 632 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 907 views
  • 3
    How to get fcm server key 5,050 views
  • 4
    Responsive Slick Slider 23,598 views
  • 5
    Understanding CWE-312: Best Practices for Secure Data Stor… 298 views
  • 6
    Mastering Functions in C++: A Complete Guide with Examples 3,745 views
  • 7
    Integrating Cloudflare Turnstile in ASP.NET Core: A Privac… 142 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26293 views
  • Exception Handling Asp.Net Core 21063 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20449 views
  • How to implement Paypal in Asp.Net Core 19809 views
  • Task Scheduler in Asp.Net core 17816 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor