Kubernetes has become the de facto standard for deploying and managing containerized applications. Running a local Kubernetes cluster allows .NET developers to test their microservices architecture before deploying to a production environment. In this guide, we will set up a local Kubernetes cluster using Minikube, deploy .NET Core services, and configure ingress and networking for microservices.
By the end of this tutorial, you will have a fully functional local Kubernetes setup for .NET applications, enabling seamless development and debugging.
1. Install Minikube and Kubectl
1.1 Prerequisites
Before installing Minikube, ensure you have the following installed on your system:
- Docker (or VirtualBox/Hyper-V for virtualization)
- kubectl (Kubernetes CLI tool)
1.2 Installing Minikube
Minikube can be installed on Windows, macOS, or Linux. Run the following commands based on your OS:
Windows (Using Chocolatey)
choco install minikube
macOS (Using Homebrew)
brew install minikube
Linux (Using Curl)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
1.3 Starting Minikube
After installation, start Minikube with:
minikube start --driver=docker
Verify installation with:
kubectl get nodes
This should display a running Minikube node.
2. Deploy .NET Core Services in Local Kubernetes
2.1 Creating a .NET Core API
First, create a simple ASP.NET Core Web API:
dotnet new webapi -o MyK8sApi
cd MyK8sApi
2.2 Dockerizing the Application
Create a Dockerfile
in the project root:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyK8sApi.dll"]
Build and tag the Docker image:
docker build -t myk8sapi:latest .
2.3 Creating Kubernetes Deployment and Service
Create a Kubernetes deployment file deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myk8sapi
spec:
replicas: 1
selector:
matchLabels:
app: myk8sapi
template:
metadata:
labels:
app: myk8sapi
spec:
containers:
- name: myk8sapi
image: myk8sapi:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f deployment.yaml
Expose the service:
kubectl expose deployment myk8sapi --type=NodePort --port=80
Find the service URL:
minikube service myk8sapi --url
3. Configure Ingress and Networking for Microservices
3.1 Enabling Ingress Controller
To enable the ingress controller in Minikube, run:
minikube addons enable ingress
3.2 Defining an Ingress Resource
Create an ingress resource file ingress.yaml
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myk8sapi-ingress
spec:
rules:
- host: myk8sapi.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myk8sapi
port:
number: 80
Apply the ingress configuration:
kubectl apply -f ingress.yaml
Update your /etc/hosts
file (Linux/macOS) or C:\Windows\System32\drivers\etc\hosts
(Windows) to map the domain:
127.0.0.1 myk8sapi.local
Now, you can access the API using http://myk8sapi.local
.
Conclusion
We have successfully set up a local Kubernetes cluster for .NET applications using Minikube. This guide covered:
- Installing Minikube and Kubectl
- Deploying a .NET Core API in Kubernetes
- Configuring ingress and networking for microservices
By following these steps, you can now develop and test your .NET microservices efficiently in a local Kubernetes environment before deploying to production.