Kubernetes for ASP.NET Core

• By OmerZ Solutions

Modern applications require more than just reliable code. Businesses need applications that can scale automatically, recover from failures, and run efficiently across different cloud environments.

Kubernetes has become one of the most popular platforms for managing containerized applications. Combined with ASP.NET Core, Kubernetes provides a powerful foundation for building scalable and cloud-native enterprise systems.

Kubernetes for ASP.NET Core
Kubernetes is an open-source container orchestration platform that automates deployment, scaling, networking, and management of containerized applications.

What is Kubernetes?

Kubernetes, often called K8s, is a platform designed to manage applications running inside containers. It provides automated deployment, load balancing, scaling, and self-healing capabilities.

Instead of manually managing application servers, Kubernetes handles the complex operational tasks required to run applications reliably.

Why Use Kubernetes with ASP.NET Core?

ASP.NET Core applications are lightweight, cross-platform, and designed for modern cloud environments. Kubernetes extends these capabilities by providing automated infrastructure management.

Benefits of Kubernetes for ASP.NET Core

  • Automatic application scaling
  • High availability
  • Self-healing after failures
  • Efficient resource utilization
  • Easy deployment automation
  • Cloud provider flexibility
  • Improved DevOps workflows

Containers vs Virtual Machines

Before understanding Kubernetes, it is important to understand the difference between containers and traditional virtual machines.

Containers Virtual Machines
Share the host operating system kernel Include a complete guest operating system
Lightweight and fast Require more resources
Ideal for microservices Commonly used for traditional applications

Docker and Kubernetes Relationship

Docker is used to package ASP.NET Core applications into containers, while Kubernetes manages and operates those containers at scale.

Kubernetes Architecture Overview

A Kubernetes environment consists of several components that work together to run applications efficiently.

Main Kubernetes Components

  • Cluster
  • Nodes
  • Pods
  • Containers
  • Control Plane
  • Services
  • Deployments

Kubernetes Cluster

A Kubernetes cluster is a group of machines that work together to run containerized applications.

A cluster contains a control plane and worker nodes.

Kubernetes Nodes

Nodes are physical or virtual machines that run application workloads.

Each node contains components required to run containers and communicate with the Kubernetes control plane.

Kubernetes Pods

A pod is the smallest deployable unit in Kubernetes. It represents one or more containers running together.

An ASP.NET Core application usually runs inside its own pod.

Kubernetes Control Plane

The control plane manages the overall Kubernetes cluster. It schedules applications, monitors resources, and maintains the desired state.

Control Plane Responsibilities

  • Application scheduling
  • Cluster management
  • Health monitoring
  • Configuration management

Containerizing ASP.NET Core Application with Docker

Before deploying an ASP.NET Core application to Kubernetes, the application must be packaged as a container image. Docker is commonly used to create and manage these application containers.

A Docker container includes the application, runtime dependencies, and required configuration so that it can run consistently across different environments.

Creating Dockerfile for ASP.NET Core

A Dockerfile contains instructions used to build a Docker image for an ASP.NET Core application.

ASP.NET Core Dockerfile Example



FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/publish .

ENTRYPOINT [
"dotnet",
"SampleApi.dll"
]

This multi-stage Docker build creates a smaller production image by separating the build environment from the runtime environment.

Building ASP.NET Core Docker Image

After creating the Dockerfile, an image can be generated using the Docker build command.



docker build -t sample-api:1.0 .

Running Container Locally



docker run -p 8080:8080 sample-api:1.0

The application is now running inside a Docker container and can be deployed to Kubernetes.

Container Registry

Kubernetes pulls application images from container registries. A registry stores and distributes Docker images.

Common container registries include:

  • Docker Hub
  • Azure Container Registry
  • Amazon Elastic Container Registry
  • Google Container Registry

Push Docker Image Example



docker tag sample-api:1.0 myregistry/sample-api:1.0
docker push myregistry/sample-api:1.0

Kubernetes Deployment Object

A Deployment describes how an application should run inside Kubernetes. It defines the container image, number of replicas, and update strategy.

Deployments provide:

  • Automatic pod creation
  • Application updates
  • Rollback support
  • Replica management

Creating ASP.NET Core Kubernetes Deployment

A Kubernetes deployment is usually defined using YAML configuration files.

deployment.yaml Example



apiVersion:apps/v1
kind:Deployment
metadata:
  name: sample-api
spec:
  replicas:3
  selector:
    matchLabels:
      app:sample-api
  template:
    metadata:
      labels:
        app:sample-api
    spec:
      containers:
      - name:sample-api
        image:myregistry/sample-api:1.0
        ports:
        - containerPort:8080

Applying Kubernetes Deployment



kubectl apply
-f deployment.yaml

Kubernetes creates the required pods according to the deployment configuration.

Checking Running Pods



kubectl get pods

The command displays the current status of application pods running inside the cluster.

Kubernetes Replica Management

Replicas define how many copies of an application should run at the same time.

Running multiple replicas improves availability because another pod can handle requests if one instance fails.

Example



replicas:5

The above configuration instructs Kubernetes to maintain five running instances of the ASP.NET Core application.

Kubernetes Service

Pods have dynamic IP addresses. Kubernetes Services provide stable networking and allow applications to communicate with pods.

Types of Kubernetes Services

  • ClusterIP
  • NodePort
  • LoadBalancer

Creating Kubernetes Service for ASP.NET Core



apiVersion:v1
kind:Service
metadata:
  name:sample-api-service
spec:
  selector:
    app:sample-api
  ports:
  - protocol:TCP
    port:80
    targetPort:8080
  type:LoadBalancer

Exposing ASP.NET Core Application

The Service object exposes the application so that users and other services can communicate with it.



kubectl apply -f service.yaml

Viewing Service Information



kubectl get services

Kubernetes Configuration Management

ASP.NET Core applications usually require environment-specific configuration values such as connection strings, API URLs, and feature settings.

Kubernetes provides ConfigMaps and Secrets to manage application configuration.

Using ConfigMap with ASP.NET Core

configmap.yaml Example



apiVersion:v1
kind:ConfigMap
metadata:
 name:sample-config
data:
 ASPNETCORE_ENVIRONMENT:Production

Using ConfigMap in Deployment



envFrom:
- configMapRef:
    name:sample-config

Managing Secrets in Kubernetes

ASP.NET Core applications commonly require sensitive information such as database connection strings, API keys, and authentication secrets. Kubernetes Secrets provide a safer way to store and manage confidential configuration values.

Creating Kubernetes Secret



apiVersion:v1
kind:Secret
metadata:
  name:database-secret
type:Opaque
data:
  username:YWRtaW4=
  password:cGFzc3dvcmQ=

Secret values are stored as encoded data and can be injected into application containers during deployment.

Using Secret in ASP.NET Core Deployment



env:
- name:DatabasePassword
  valueFrom:
    secretKeyRef:
      name:database-secret
      key:password


Environment Configuration in ASP.NET Core

ASP.NET Core applications support multiple environments such as Development, Testing, and Production.

Kubernetes can configure the environment using variables.

Setting ASP.NET Core Environment



env:
- name:ASPNETCORE_ENVIRONMENT
  value:Production


The application automatically loads the correct configuration file such as appsettings.Production.json.

Kubernetes Ingress for ASP.NET Core Applications

Ingress provides external HTTP and HTTPS access to applications running inside a Kubernetes cluster.

Instead of exposing every service individually, Ingress acts as a reverse proxy and routes traffic to the correct application.

Creating Ingress Configuration



apiVersion:networking.k8s.io/v1
kind:Ingress
metadata:
 name:sample-api-ingress
spec:
 rules:
 - host:api.example.com
   http:
    paths:
    - path:/
      pathType:Prefix
      backend:
       service:
        name:sample-api-service
        port:
         number:80

HTTPS Configuration with Kubernetes

Production applications should always use HTTPS to protect user data and API communication.

Kubernetes supports HTTPS through Ingress TLS configuration.



tls:
- hosts:
  - api.example.com
  secretName:api-tls-secret


Scaling ASP.NET Core Applications in Kubernetes

One of the major advantages of Kubernetes is automatic application scaling. Applications can increase or decrease instances based on workload.

Scaling helps maintain performance during high traffic periods.

Manual Scaling with Kubernetes



kubectl scale deployment sample-api --replicas=5

This command increases the number of application instances to five replicas.

Horizontal Pod Autoscaler (HPA)

Horizontal Pod Autoscaler automatically adjusts the number of pods according to resource usage such as CPU or memory consumption.

Creating HPA Example



apiVersion:autoscaling/v2
kind:HorizontalPodAutoscaler
metadata:
 name:sample-api-hpa
spec:
 scaleTargetRef:
  apiVersion:apps/v1
  kind:Deployment
  name:sample-api
 minReplicas:2
 maxReplicas:10
 metrics:
 - type:Resource
   resource:
    name:cpu
    target:
     type:Utilization
     averageUtilization:70

In this example, Kubernetes keeps CPU utilization around the configured target and automatically adjusts pod count.

ASP.NET Core Health Checks with Kubernetes

Health checks allow Kubernetes to understand whether an application is running correctly.

ASP.NET Core provides built-in support for health monitoring endpoints.

Adding Health Checks



builder.Services.AddHealthChecks();
app.MapHealthChecks("/health");

Liveness Probe

A liveness probe determines whether an application is still running. If the probe fails, Kubernetes restarts the container.

Liveness Configuration



livenessProbe:
 httpGet:
  path:/health
  port:8080
 initialDelaySeconds:30
 periodSeconds:10


Readiness Probe

A readiness probe checks whether an application is ready to receive traffic.

If a pod is not ready, Kubernetes temporarily removes it from service routing.

Readiness Configuration



readinessProbe:
 httpGet:
  path:/health
  port:8080
 initialDelaySeconds:10
 periodSeconds:5

Logging in Kubernetes

Logging is essential for monitoring production ASP.NET Core applications. Kubernetes collects container logs that can be viewed using command-line tools or external monitoring platforms.

Viewing Application Logs



kubectl logs
pod-name

Monitoring ASP.NET Core Applications

Production Kubernetes environments usually use monitoring solutions to track:

  • Application performance
  • CPU and memory usage
  • Request failures
  • Response times
  • Application availability

Kubernetes with Entity Framework Core

ASP.NET Core applications commonly use Entity Framework Core for database operations. When deploying these applications on Kubernetes, database connectivity and configuration should be planned carefully.

The application container should communicate with the database using secure configuration values provided through Kubernetes Secrets and environment variables.

Database Connection Configuration



{
  
"ConnectionStrings":
{
	"DefaultConnection": "Server=db-service;Database=AppDb;User Id=appuser;Password=password;"
}

}

In Kubernetes environments, database connection information is usually injected at runtime instead of being stored directly inside application files.

Database Deployment Considerations

Although Kubernetes can run databases, many enterprise organizations prefer managed database services because databases require persistent storage, backup strategies, and maintenance operations.

Common Database Approaches

  • Managed cloud databases
  • Dedicated database servers
  • Database containers with persistent volumes
  • Hybrid database architectures

Running Entity Framework Core Migrations

Database migrations should be handled carefully in Kubernetes environments.

A common approach is running migrations as a separate deployment step during application release.



dotnet ef database update

Many enterprise teams execute migrations through CI/CD pipelines before deploying new application versions.

CI/CD Pipeline for ASP.NET Core Kubernetes Deployment

Continuous Integration and Continuous Deployment automate the process of building, testing, and releasing ASP.NET Core applications.

Kubernetes Deployment Strategies

Kubernetes supports different strategies for releasing new application versions safely.

Rolling Update

Rolling updates gradually replace old application pods with new versions without causing downtime.



strategy:
 type:RollingUpdate

Blue-Green Deployment

Blue-green deployment maintains two application versions and switches traffic between them after validation.

Canary Deployment

Canary deployment releases a new version to a small percentage of users before full deployment.

Enterprise Kubernetes Architecture for ASP.NET Core

Large organizations usually design Kubernetes environments with multiple services, security controls, monitoring, and automated deployment pipelines.

Kubernetes Best Practices for ASP.NET Core

  • Use lightweight Docker images
  • Configure proper resource limits
  • Enable health checks
  • Use Kubernetes Secrets for sensitive data
  • Avoid storing configuration inside containers
  • Implement centralized logging
  • Monitor application performance
  • Use automated CI/CD deployment
  • Keep containers stateless whenever possible
  • Regularly update container images

Resource Requests and Limits

Kubernetes allows developers to define CPU and memory requirements for applications.



resources:
 requests:
  memory:"512Mi"
  cpu:"500m"
 limits:
  memory:"1Gi"
  cpu:"1000m"

Proper resource configuration prevents applications from consuming excessive cluster resources.

Common Kubernetes Mistakes

  • Running containers without resource limits
  • Storing passwords in source code
  • Ignoring application health checks
  • Deploying databases without backup planning
  • Using incorrect container configurations
  • Skipping monitoring and logging
  • Manually managing deployments instead of automation

Frequently Asked Questions About Kubernetes and ASP.NET Core

Can ASP.NET Core applications run on Kubernetes?

Yes. ASP.NET Core applications are well suited for Kubernetes because they are cross-platform, lightweight, and support container-based deployment.

Is Docker required for Kubernetes?

Applications deployed to Kubernetes need container images. Docker is one of the most common tools used to create those images.

Does Kubernetes replace ASP.NET Core hosting?

No. Kubernetes manages application deployment and infrastructure. ASP.NET Core remains responsible for running the web application itself.

Can Kubernetes automatically scale ASP.NET Core applications?

Yes. Kubernetes Horizontal Pod Autoscaler can automatically increase or decrease application instances based on resource usage.

Is Kubernetes suitable for enterprise applications?

Yes. Kubernetes is widely used for enterprise systems because it provides scalability, reliability, automation, and efficient infrastructure management.

Conclusion

Kubernetes provides a powerful platform for deploying and managing modern ASP.NET Core applications. By combining containerization, automated scaling, service management, and self-healing capabilities, Kubernetes helps development teams build reliable cloud-native solutions.

When ASP.NET Core applications are properly containerized and deployed using Kubernetes best practices, organizations can achieve improved scalability, availability, and operational efficiency.

Whether building microservices, enterprise APIs, or large-scale web platforms, Kubernetes provides the foundation required for modern software delivery.

Need Help Deploying Scalable ASP.NET Core Applications?

OmerZ Solutions helps businesses build, containerize, and deploy secure enterprise .NET applications using modern cloud and DevOps technologies.

Contact Us