This isn’t a tutorial about kubernetes, kind, or an API with .NET. It’s just something that helped me when I was studying Kubernetes
Prerequisite tools needed
A brief context about kind
Kind is a tool for running local Kubernetes clusters using Docker containers.
First, let’s create a simple API with .NET. (Really simple, I won’t change anything).
Run the following command in the terminal:
dotnet new webapi
Add the following Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app
ENV ASPNETCORE_HTTP_PORTS=80
COPY . ./
RUN dotnet restore api1.csproj
RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
ENV ASPNETCORE_HTTP_PORTS=80
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "api1.dll"]
Run the following Docker command to create the image:
docker build -t api:1.0 .
To test the API, run:
docker run --rm -it -p 8001:80 -e ASPNETCORE_HTTP_PORTS=80 api:1.0docker run --rm -it -p 8001:80 -e ASPNETCORE_HTTP_PORTS=80 api:1.0
Then run:
curl http://localhost:8001/weatherforecast
After creating the API and the Docker image, let’s create a Cluster with the following YAML:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4 nodes:
- role: control-plane
After creating the YAML, run the following command:
kind create cluster --name api --config=Cluster/DeployCluster.yaml
To check the cluster, run:
kubectl cluster-info -context kind-api
Next, we need to add the app image to the cluster with the command:
kind load docker-image api:1.0 --name api
Now, let’s continue working with kubernetes by creating the Deployment and Service YAML files.
Deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata: name: api
spec: selector: matchLabels: app: api version: v1 replicas: 3 template: metadata: labels: app: api version: v1 spec: containers: - name: api image: api:1.0 ports: - containerPort: 80
Service.yaml:
apiVersion: v1
kind: Service
metadata: name: api-service
spec: selector: app: api ports: - name: api port: 80 targetPort: 80
After creating the YAML files for the Deployment and Service, run the following commands:
kubectl apply -f Deployment.yaml kubectl apply -f Service.yaml
You can check the services and deployments with kubectl get deployments
and kubectl get svc
.
When checking the Services you will see that there is no External-IP available, which is needed to call the endpoints in our API. This not a kubernetes or a kind problem but rather a docker issue. For an alternative solution, you can start from this link.
To connect your local computer to the Docker bridge default gateway, use the following port-forward
command:
sudo -E kubectl port-forward svc/api-service 8001:80
Now, you can run:
curl http://localhost:8001/WeatherForecast
If you are running the .NET api, the response will look like this:
[ { "date": "2024-03-08T23:14:25.6202397+00:00", "temperatureC": 28, "temperatureF": 82, "summary": "Hot" }, { "date": "2024-03-09T23:14:25.6202432+00:00", "temperatureC": 26, "temperatureF": 78, "summary": "Scorching" }, { "date": "2024-03-10T23:14:25.6202433+00:00", "temperatureC": 44, "temperatureF": 111, "summary": "Hot" }, { "date": "2024-03-11T23:14:25.6202435+00:00", "temperatureC": 8, "temperatureF": 46, "summary": "Warm" }, { "date": "2024-03-12T23:14:25.6202437+00:00", "temperatureC": -10, "temperatureF": 15, "summary": "Warm" }
]
With this, you have a cluster running locally.
Lens
Lens is a tool to managing and troubleshooting Kubernetes workloads and many other things.
Let’s start by installing Lens and logging in (you will need to create an account in lens and onstain a lens ID).
You can run the following command in the terminakl:
k config view --output yaml
This command print your kubernetes configuration. We will need this to add to Lens. You can either connect the kube folder or copy the kube config and paste it (normally the config is found in $HOME/.kube/config
).
With this, you will have the Cluster connection, and you can see something like this and many other things.