Introduction: Why C# and Kubernetes are a Powerful Combination
\n\nModern 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\nIn 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.
Step 1: Preparing the .NET Application and Containerization
\n\n1.1 Creating a Simple Web API in C#
\n\nFor demonstration, we will create a minimal ASP.NET Core Web API. Open a terminal and run:
\n\ndotnet new webapi -n MyK8sApp\ncd MyK8sApp\n\nBy default, the project already contains the WeatherForecastController. Let's make sure the application runs locally:
dotnet run\n\nNavigate to http://localhost:5000/weatherforecast — you should see a JSON response. Now our application is ready for packaging.
1.2 Creating a Dockerfile
\n\nDeployment to Kubernetes is impossible without containerization. Let's create a Dockerfile in the project root:
# 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\nNote: we use a multi-stage build — this reduces the final image size and improves security.
\n\n1.3 Building and Checking the Docker Image
\n\nBuild the image and run it locally:
\n\ndocker build -t myk8sapp:latest .\ndocker run -d -p 8080:80 --name myapp myk8sapp:latest\n\nCheck that it works: http://localhost:8080/weatherforecast. If everything works — great. Stop the container:
docker stop myapp && docker rm myapp\n\nStep 2: Kubernetes Basics — What You Need to Know for Deployment
\n\nBefore 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) orLoadBalancer(external) types. \n - ConfigMap / Secret — for storing configuration and sensitive data. \n
We will use minikube for local testing. Install it following the official instructions.
\n\nStart the cluster:
\n\nminikube start\n\nStep 3: Creating Kubernetes Manifests for the C# Application
\n\n3.1 Deployment Manifest
\n\nCreate a file named deployment.yaml:
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