Introduction
In this article, I will explain the basics of how to deploy a .NET Web application in Kubernetes. I will use Minikube to deploy the app on the local machine.
You can either use an existing .NET Web Application or create a new .NET Web Application and containerize it.
I am going to use the app that was created in my previous article.
I’ve made some minor adjustments for clarity and proper capitalization. Containerize Your .NET 7.0 Web Application With Docker
Kubernetes (k8s) can only deploy containers because it only knows how to deploy containers.
In the previous article, I have already containerized the application using Docker.
There are many other tools to containerize the application, such as LXC, Podman, Containerd, etc. However, Docker is the most popular tool for creating and running containers.
Prerequisites
You need to install following tools in your machine:
Create Docker Image
Fist, step to deploy app in Kubernetes is build the image of web app that you want to run in Kubernetes.
I am building image form Containerize Your .NET 7.0 Web Application With Docker
Go inside project folder.
|
|
Run command in terminal to build image:
|
|
Sharing Docker image with a registry
There are many registry to publish docker images. Here, I am using Docker Hub to publish image publicly.
Login to Docker Hub
Use below command to login to the Docker Hub. This command will help you to login in the Docker Hub.
|
|
Push the Image to the Registry:
Push the image to the registry:
|
|
In my case, my docker push command will be like:
|
|
Deploying Your App in Kubernetes
I will deploy an application in Mac OS minikube
local development Kubernetes cluster.
1. Deployment (deployment.yaml)
A deployment allows you to describe an application’s life cycle, such as which images to use for the app, the number of pods that we want. Update pods and replica sets, rollback to previous deployment versions, scale a deployment , pause or continue a deployment, etc
We define set of instructions in a YAML file. ie deployment.yaml
Now, you can apply the deployment.yaml
file by using below command:
|
|
Once, deployment is created you will get deployment.apps/web-app created
success message.
To verify, you can run the below command, which will list the pods.
|
|
The number of pods is depend upon the replicas value in deployment.yaml
file replicas: 1
.
In you case replicas value is 1
.
Even if you delete pods with below command. It will spin up another pod immediately.
Again, if you run kubectl get pods
, you will see new pod.
2. Service (service.yaml)
Services provide a way to expose your application’s Pods to the network or other services within the cluster, and they play a very important role in enabling communication between different parts of your application.
Here is a service.yaml
file that I am using:
Now, you can create the service by using service.yaml
. If we run below command it will crate service:
|
|
Once, service is created you will see service/web-app created
success message.
To verify, you can run the below command, which will list the pods.
|
|
Now, you can see created service in your terminal.
3. Accessing an app via browser
To check the app via browser or form any HTTP client. First you need to find the URL in which app is accessible.
In minikube you can run following command.
minikube service --url=true dotnet7-web-app
The above command will give the base URL. Something like:
Now, you are ready to access you app via browser. For my app I can use
http://127.0.0.1:63660/WeatherForecast
to see the response.
4. Create an Ingress and use custom domain to access App via browser
Ingress is important in k8s it exposes HTTP
and HTTPS
routes from outside the cluster to services within the cluster.
Traffic routing is controlled by rules defined on the Ingress resource.
The Ingress is the equivalent of a reverse proxy in Kubernetes.
Everytime running this command minikube service --url=true dotnet7-web-app
and getting
the app running URL is tedious.
So, I have created the ingress.yml
file.
Here is a ingress.yaml
file that I am going to use:
In this file I have added, host
as kubernete.local
ie. - host: kubernete.local
.
Which is the custom domain. App should be accessible in this custom domain http://kubernete.local
By default, minikube comes with the Nginx as Ingress controller, and you can enable it with below command:
minikube addons enable ingress
To enable ingress addons Minikube will take few minutes because it takes time to download and install Nginx as an Ingress.
Once ingress addons is ready, we can create Ingress with below command:
|
|
Now, you can find the Minikube IP with below command:
minikube ip
Let’s say you have your minikube IP address 192.168.49.2
Now, you need add my host names to /etc/hosts
|
|
If you try http://kubernete.local/WeatherForecast
in your browser, it should connect to app and give the result back.
Here is my output form custom domain http://kubernete.local/WeatherForecast
If you are using M1 with the docker driver. You need to enable minikube tunnel. Keep in mind that your etc/hosts
file needs to map to 127.0.0.1
, instead of the output of minikube ip
or kubectl get ingress
This is an very important.
Conclusion
This article has provided a guide to deploying a .NET Web application in Kubernetes. Throughout the process, I’ve covered essential steps from creating Docker images to setting up Kubernetes deployments and services. By utilizing tools like Minikube, Docker, and Kubernetes, I’ve successfully containerized and deployed the application for local Minikube Kubernetes. Additionally, I have explored advanced concepts such as creating an Ingress to enable custom domain access.