Deploying C# on Kubernetes: A Beginner's Basic Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why C# and Kubernetes are a Powerful Combination

\n\n

Modern development increasingly requires not just writing code, but also properly packaging and running it in a cloud environment. Kubernetes (or K8s) has become the de facto standard for container orchestration, and C# (.NET) is one of the most popular languages for creating high-load web applications and microservices.

\n\n

In this guide, we will walk through the path from a ready-made C# application to its full deployment in a Kubernetes cluster. You will learn how to create a Docker image, write manifests for deployment and service, and then run everything using kubectl. No magic — just practice.

\n\n

Step 1: Preparing the .NET Application and Containerization

\n\n

1.1 Creating a Simple Web API in C#

\n\n

For demonstration, we will create a minimal ASP.NET Core Web API. Open a terminal and run:

\n\n
dotnet new webapi -n MyK8sApp\ncd MyK8sApp
\n\n

By default, the project already contains the WeatherForecastController. Let's make sure the application runs locally:

\n\n
dotnet run
\n\n

Navigate to http://localhost:5000/weatherforecast — you should see a JSON response. Now our application is ready for packaging.

\n\n

1.2 Creating a Dockerfile

\n\n

Deployment to Kubernetes is impossible without containerization. Let's create a Dockerfile in the project root:

\n\n
# Use the official image for building\nFROM mcr.microsoft.com/dotnet/sdk:8.0 AS build\nWORKDIR /app\n\n# Copy project files and restore dependencies\nCOPY *.csproj .\nRUN dotnet restore\n\n# Copy the remaining files and build the application\nCOPY . .\nRUN dotnet publish -c Release -o out\n\n# Final image — runtime only\nFROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime\nWORKDIR /app\nCOPY --from=build /app/out .\nENTRYPOINT ["dotnet", "MyK8sApp.dll"]
\n\n

Note: we use a multi-stage build — this reduces the final image size and improves security.

\n\n

1.3 Building and Checking the Docker Image

\n\n

Build the image and run it locally:

\n\n
docker build -t myk8sapp:latest .\ndocker run -d -p 8080:80 --name myapp myk8sapp:latest
\n\n

Check that it works: http://localhost:8080/weatherforecast. If everything works — great. Stop the container:

\n\n
docker stop myapp && docker rm myapp
\n\n

Step 2: Kubernetes Basics — What You Need to Know for Deployment

\n\n

Before writing manifests, let's understand the key Kubernetes concepts:

\n\n
    \n
  • Pod — the smallest unit of deployment. Contains one or more containers.
  • \n
  • Deployment — manages the state of Pods: scaling, updates, recovery from failures.
  • \n
  • Service — an abstraction that provides network access to Pods. Typically uses ClusterIP (internal) or LoadBalancer (external) types.
  • \n
  • ConfigMap / Secret — for storing configuration and sensitive data.
  • \n
\n\n

We will use minikube for local testing. Install it following the official instructions.

\n\n

Start the cluster:

\n\n
minikube start
\n\n

Step 3: Creating Kubernetes Manifests for the C# Application

\n\n

3.1 Deployment Manifest

\n\n

Create a file named deployment.yaml:

\n\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: myk8sapp-deployment\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: myk8sapp\n  template:\n    metadata:\n      labels:\n        app: myk8sapp\n    spec:\n      containers:\n      - name: myk8sapp\n        image: myk8sapp:latest\n        imagePullPol                

Blogs

Book Recommendations