Containerization Of .NET Core Web API Using Kubernetes And Docker

In this post, I'll show you how to build the .Net Core API application step-by-step and then enable docker in that, and after creating images, run that into the Kubernetes.

Prerequisites

  1. Basic Understanding of Cloud.
  2. Basic Understanding of Docker.

For that You can read my blogs related to docker and kubernetes to understand things easily

Install Visual Studio Community (it’s free) with the ASP.NET and web development workload.

1. Create a new ASP.NET Core Web API Project

Containerization of .NET Core Web API using Kubernetes and Docker

2. Configure the new Project

Containerization of .NET Core Web API using Kubernetes and Docker

3. Set additional information of the project like Target Framework, Authentication Type, Enable Https, Enable Docker, Docker OS Support Etc.

Containerization of .NET Core Web API using Kubernetes and Docker

Add the new controller, “DemoController”. It will return Welcome to the Docker World when we hit the get method.

Containerization of .NET Core Web API using Kubernetes and Docker

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!";
        }
    }
}

This is the Dockerfile that is created by Visual Studio when we enable the Docker Support option while creating the new Web Application.

Containerization of .NET Core Web API using Kubernetes and Docker

#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"]

After that when we run the application by using Docker then it will create the Docker Image in Docker Desktop which you installed on your local machine. (Please confirm the Docker Desktop is running properly on your machine and that it's in running mode.)

Containerization of .NET Core Web API using Kubernetes and Docker

Here you will see the output after executing the get method of DemoController.

Containerization of .NET Core Web API using Kubernetes and Docker

Kubernetes

  • Kubernetes is the Container Management Orchestration Tool which is developed by Google for managing their own microservices across the different clusters.
  • Kubernetes also called K8s, basically is Numeronym Standard which is used since the 1980s. For Example, In the K8s there are 8 words in between K and S like that.
  • Google developed an internal system called Borg and later on, named Omega which they use to Orchestrate the Data Center.
  • In 2014, Google introduced Kubernetes as an Open Source and it is written in Golang language. Later on, donated to CNCF.
  • Kubernetes is a tool that automates container deployment, load balancing, and auto-scaling. It will manage all the containers which are running in different environments.

Now we are going to run this application on Kubernetes, for that we are going to enable Kubernetes in Docker Desktop as shown below.

Containerization of .NET Core Web API using Kubernetes and Docker

Later on, we create on Manifest File. Basically, it is used for configuration and it may be the type of YML and JSON, in that we mentioned settings related to different objects and how they will be interconnected with each other. There are different things that are present inside the Manifest File like the Number of Pods, Services, Deployment, and Ingress Controller related things, which all are used to run the application.

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

So here, we mentioned deployment and service section in the manifest file and configured the things related to pods, services, port and docker image and container.

Next, we are going to apply Manifest YAML File using kubectl Kubernetes

kubectl apply -f manifest.yml

Containerization of .NET Core Web API using Kubernetes and Docker

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

Containerization of .NET Core Web API using Kubernetes and Docker

If you want more details about pods, services, endpoints, deployments, and Kubernetes context then you can use the following commands

Containerization of .NET Core Web API using Kubernetes and Docker

There are also many commands which are present and used to scale containers and for something like that, you are able to use as per your project requirement.

So, this was all about the containerization of the .NET Core Web API using Docker and Kubernetes.

I hope you understood the concepts related to Docker.

Happy Coding!

If you want to explore more about Docker and Kubernetes, then please visit the following blogs related to that.