Deploying C# on Kubernetes: A Basic Practical 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 Run C# in Kubernetes?

Modern C# (.NET) development increasingly requires a scalable and fault-tolerant infrastructure. Kubernetes (K8s) has become the standard for container orchestration, allowing you to automate the deployment, scaling, and management of applications. In this guide, we will show you how to turn your C# application into a container and run it in a Kubernetes cluster in practice.

You will learn how to prepare a Docker image, write Kubernetes manifests, and perform a deployment using kubectl. The article is aimed at developers who are already familiar with the basics of C# and want to master modern DevOps practices.



1. Preparing the C# Application for Containerization

Before deploying an application to Kubernetes, it must be packaged into a Docker image. For this example, we'll take a simple ASP.NET Core Web API application.



1.1. Creating the Project

Create a new project using the .NET CLI:

dotnet new webapi -n MyK8sAppcd MyK8sApp

Make sure the application runs locally:

dotnet run

By default, the application will be available at http://localhost:5000.



1.2. Adding a Dockerfile

In the project root, create a file named Dockerfile (no extension). Use a multi-stage build to optimize the image size:

# Build stageFROM mcr.microsoft.com/dotnet/sdk:8.0 AS buildWORKDIR /appCOPY *.csproj ./RUN dotnet restoreCOPY . ./RUN dotnet publish -c Release -o out

# Runtime stageFROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtimeWORKDIR /appCOPY --from=build /app/out ./ENV ASPNETCORE_URLS=http://+:80EXPOSE 80ENTRYPOINT ["dotnet", "MyK8sApp.dll"]


1.3. Building and Testing the Docker Image

Build the image and run it locally:

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

Check that the application responds: curl http://localhost:8080/weatherforecast (or open it in a browser).



2. Writing Kubernetes Manifests

To deploy to Kubernetes, you need YAML files describing the desired state of the application. The minimum set includes a Deployment and a Service.



2.1. Creating a Deployment

File deployment.yaml:

apiVersion: apps/v1kind: Deploymentmetadata:  name: myk8sapp-deployment  labels:    app: myk8sappspec:  replicas: 2  selector:    matchLabels:      app: myk8sapp  template:    metadata:      labels:        app: myk8sapp    spec:      containers:      - name: myk8sapp        image: myk8sapp:latest        ports:        - containerPort: 80        env:        - name: ASPNETCORE_ENVIRONMENT          value: "Production"        resources:          requests:            memory: "128Mi"            cpu: "250m"          limits:            memory: "256Mi"            cpu: "500m"

Important: If you are using a local cluster (e.g., Minikube or Kind), the image must be accessible inside the cluster. Either set imagePullPolicy: Never for local images.



2.2. Creating a Service

File service.yaml:

apiVersion: v1kind: Servicemetadata:  name: myk8sapp-servicespec:  selector:    app: myk8sapp  ports:    - protocol: TCP      port: 80      targetPort: 80  type: LoadBalancer

The LoadBalancer type is suitable for cloud providers. For local clusters, use NodePort or ClusterIP with port forwarding via kubectl port-forward.



3. Deploying and Testing the Application

Now that the manifests are ready, perform the deployment to the Kubernetes cluster.



3.1. Applying the Manifests

Blogs

Book Recommendations