Introduction
In modern cloud-native development, deploying applications in a scalable, resilient, and maintainable way is essential. One of the most widely adopted solutions for this is Kubernetes, especially when combined with ASP.NET Core applications.
Kubernetes provides powerful orchestration capabilities that help manage containerized applications efficiently. When used with ASP.NET Core, it enables developers to build highly available, scalable, and production-ready systems.
In this guide, we will walk through how to deploy an ASP.NET Core application using Kubernetes step by step, with clear explanations and practical examples.
Understanding Kubernetes in the Context of .NET Applications
Kubernetes is an open-source container orchestration platform designed to automate the deployment, scaling, and management of applications.
It plays a crucial role in:
Managing container lifecycles
Scaling applications based on demand
Ensuring application availability
For ASP.NET Core applications, Kubernetes acts as a runtime environment that ensures your application runs reliably across different environments.
Understanding ASP.NET Core for Containerized Deployment
ASP.NET Core is a cross-platform framework that is well-suited for container-based deployments. Its lightweight nature and built-in support for dependency injection, configuration, and logging make it ideal for microservices and cloud-native applications.
When packaged using Docker, ASP.NET Core applications can be easily deployed to Kubernetes clusters.
Prerequisites for Kubernetes Deployment
Before starting, ensure the following tools are installed and configured:
.NET SDK
Docker
Kubernetes (Minikube or cloud cluster)
kubectl CLI
These tools are required to build, containerize, and deploy your application.
Step 1: Create an ASP.NET Core Application
dotnet new webapi -n MyK8sApp
cd MyK8sAppCode Explanation
dotnet new webapicreates a new ASP.NET Core Web API project-n MyK8sAppassigns a project namecd MyK8sAppnavigates into the project directory
Run the application to verify it works correctly:
dotnet runCode Explanation
dotnet runbuilds and runs the application locallyThis step ensures the project is functioning before containerization
Step 2: Create a Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyK8sApp.dll"]Code Explanation
FROM aspnet:7.0defines the runtime imageWORKDIR /appsets the working directoryEXPOSE 80exposes port 80 for the containerFROM sdk:7.0is used for building the applicationdotnet restoreinstalls dependenciesdotnet publishcompiles and prepares the app for deploymentENTRYPOINTdefines how the container starts
Step 3: Build Docker Image
docker build -t myk8sapp:latest .Code Explanation
docker buildcreates an image from the Dockerfile-t myk8sapp:latestassigns a name and tag.specifies the current directory as build context
Verify the image:
docker imagesCode Explanation
Lists all locally available Docker images
Confirms that the image was built successfully
Step 4: Run Docker Container (Validation Step)
docker run -d -p 8080:80 myk8sappCode Explanation
-druns the container in detached mode-p 8080:80maps host port 8080 to container port 80myk8sappis the image name
Access the application: http://localhost:8080
This step validates that the containerized application is functioning correctly.
Step 5: Push Image to Docker Hub
docker tag myk8sapp yourusername/myk8sapp
docker push yourusername/myk8sappCode Explanation
docker tagrenames the image for Docker Hubdocker pushuploads the image to a remote repositoryKubernetes pulls images from such repositories
Step 6: Create Kubernetes Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: myk8sapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: myk8sapp
template:
metadata:
labels:
app: myk8sapp
spec:
containers:
- name: myk8sapp
image: yourusername/myk8sapp
ports:
- containerPort: 80Code Explanation
kind: Deploymentdefines a deployment resourcereplicas: 2ensures two instances of the applicationmatchLabelsconnects deployment with podscontainersdefines container configurationimagespecifies which Docker image to use
Step 7: Create Kubernetes Service Configuration
apiVersion: v1
kind: Service
metadata:
name: myk8sapp-service
spec:
type: NodePort
selector:
app: myk8sapp
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007Code Explanation
kind: Serviceexposes the applicationtype: NodePortallows external accessselectorconnects service to podsnodePortdefines the external access port
Step 8: Deploy to Kubernetes Cluster
kubectl apply -f deployment.yaml
kubectl apply -f service.yamlCode Explanation
kubectl applycreates resources in the cluster-fspecifies the configuration file
Step 9: Verify Deployment
kubectl get pods
kubectl get servicesCode Explanation
get podsshows running containersget servicesshows exposed endpoints
Step 10: Access the Application
minikube service myk8sapp-serviceCode Explanation
Opens the application in a browser using Minikube
Automatically resolves service URL
Alternatively:
http://:30007
Benefits of Using Kubernetes with ASP.NET Core
Scalability
Kubernetes automatically scales applications based on traffic.
High Availability
Multiple replicas ensure the application remains available.
Self-Healing
Failed containers are automatically restarted.
Load Balancing
Traffic is distributed across multiple instances.
Best Practices for Production Deployment
Use environment-specific configurations
Implement health checks (liveness and readiness probes)
Define CPU and memory limits
Use logging and monitoring tools
Avoid using latest tag in production images
Common Mistakes to Avoid
Skipping container validation before deployment
Incorrect port configurations
Not versioning Docker images
Ignoring resource constraints
Summary
Deploying an ASP.NET Core application using Kubernetes provides a robust and scalable solution for modern application hosting. By containerizing the application with Docker and orchestrating it using Kubernetes, developers can achieve high availability, fault tolerance, and seamless scalability. This approach aligns with cloud-native development practices and is essential for building production-ready .NET applications.


Join the conversation! Your thoughts help the community grow.