In this post, I’ll teach you how to develop the.Net Core API application step by step, enable Docker in it, and then run it in Kubernetes after producing images.
Prerequisites
- Basic Understanding of Cloud.
- Basic Understanding of Docker.
1. Create a new ASP.NET Core Web API Project
2. Configure the new Project
3.Set extra project information such as Target Framework, Authentication Type, Enable Https, Enable Docker, Docker OS Support, and so on.
Include the “DemoController” new controller. When we call the get method, it will return Welcome to the Docker World.
using Microsoft.AspNetCore.Mvc; using System; namespace WebAPI.Controllers { [ApiController] [Route("demo")] public class DemoController: ControllerBase { [HttpGet] public String Get() { return "Welcome to the Docker World!"; } } }
When we enable the Docker Support option while creating a new Web Application, Visual Studio automatically generates the Dockerfile shown below.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src COPY ["WebAPI/WebAPI.csproj", "WebAPI/"] RUN dotnet restore "WebAPI/WebAPI.csproj" COPY . . WORKDIR "/src/WebAPI" RUN dotnet build "WebAPI.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "WebAPI.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "WebAPI.dll"]
The Docker Image will then be created in Docker Desktop, which you installed on your local computer, when the program is then executed using Docker. (Please verify that the Docker Desktop is installed successfully and is in running mode on your computer.)
Here you will see the output after executing the get method of DemoController.
Kubernetes
- For managing their own microservices across many clusters, Google built the container management orchestration tool known as Kubernetes.
- The Numeronym Standard, commonly known as Kubernetes or K8s, has been in use since the 1980s. For instance, there are 8 words between the letters K and S in the K8s.
- The Borg and Omega internal systems that Google developed are used to orchestrate the data center.
- Google released Kubernetes as an open source project in 2014; it was created in the Golang programming language. Donated later to CNCF.
- A program called Kubernetes automates the deployment of containers, load balancing, and auto-scaling. All of the containers running in various settings will be managed by it.
Now that we’re running this application on Kubernetes, let’s enable Kubernetes in Docker Desktop as demonstrated below.
We build a manifest file afterwards. In essence, it is used for configuration and might be of the YML and JSON types because we discussed settings pertaining to various objects and how they will be connected to one another. The Manifest File contains a variety of items that are necessary to execute the application, including information on the number of pods, services, deployment, and ingress controller.
apiVersion: apps/v1 kind: Deployment metadata: name: webapi labels: app: product-app spec: replicas: 2 selector: matchLabels: service: webapi template: metadata: labels: app: product-app service: webapi spec: containers: - name: webapicontainer image: webapi:dev ports: - containerPort: 80 protocol: TCP env: - name: ASPNETCORE_URLS value: http://+:80 --- apiVersion: v1 kind: Service metadata: name: webapiservice labels: app: product-app service: webapi spec: type: NodePort ports: - port: 8080 targetPort : 80 protocol: TCP selector: service: webapi
Therefore, in this portion of the manifest file, we mentioned the deployment and service sections and established the settings for pods, services, ports, and Docker images and containers.
Next, we are going to apply Manifest YAML File using kubectl Kubernetes
kubectl apply -f manifest.yml
Here you will see our service is running on Port 30360 and now we are able to access that using URL
http://localhost:30360/demo
Use the following commands to get more information about pods, services, endpoints, deployments, and the Kubernetes environment.
You can use one of the various commands available that are used to scale containers in accordance with the needs of your project.
This was all about utilizing Docker and Kubernetes to containerize the.NET Core Web API.
I trust you comprehended the Docker-related topics.