Tuesday, August 17, 2021

[Video] Running simple ASP.Net Web API in Kubernetes on Docker Desktop

This post mainly contains the notes that I used to create a video. The video talks about hosting ASP.Net WebAPI into Kubernetes. The API can be accessible from the host machine using http://localhost:<port>/. The aim is to show simple hosting in Kubernetes that runs using Docker Desktop. 

It is advised to watch the video to get more clarity. 

Dev environment

Please refer to the previous post about setting up Docker Desktop and K8s environment. Along with that, it is required to have Visual Studio 2019 with docker container ASP.Net tools installed.

Source code

This post is not about creating a new visual studio project and enable the K8s support. Instead, download the sample from the below location

https://github.com/joymon/dotnet-demos/tree/master/web/webapi/simple-k8s-hosting/SimpleK8sHosting

Running using Docker Desktop

Just click on the run button in Visual Studio 2019 to run using Docker Desktop. The Dockerfile and other setups are already done in the sample.

Publish to Docker hub - the image registry

Below are the commands to tag and push to Docker Hub

# docker test locally
docker build -t simplek8shosting .

# tag and push
docker tag simplek8shosting joymon/simplek8shosting:0.0.3
docker push joymon/simplek8shosting:0.0.3
If the registry is not Docker Hub give the appropriate tag along with the version.

Deploy to K8s and test

Use the k8s-docker-desktop-deploy.yml file to deploy to local k8s.

kubectl apply -f .\k8s-docker-desktop-deploy.yml
After deploying find out the port to be used from the local machine. kubectl get all
kubectl get service webapiservice
From the output of the above commands, we can get the exposed port to reach the service from outside of the cluster. Note that the port 8080 of service is to call within the cluster not from outside.

Some internals

The video talks about some basics of how things are wired up. Especially the IPs and the ports. The communication is hoping one to another to reach the final container that serves our request.

EchoController and tests

The EchoController has a GET method that returns some details about the running environment and request. That includes the hostname, IPs, environment variables, and request headers.

Replica test

Increase the replicas to see the request is served from different pods.

Video

More details are in the video. Watch it to see it in action.

References

https://bterkaly.medium.com/running-asp-net-applications-in-kubernetes-a-detailed-step-by-step-approach-96c98f273d1a

No comments: