
Introduction
In this article, we are going to discuss a few basics of Docker and Kubernetes, as well as the step-by-step implementation of product services using the .NET Core 7 Web API and the containerization of product services with the help of Docker and Kubernetes.
Agenda
- What is Docker?
- Why use Docker?
- Benefits of Docker
- What is Kubernetes?
- Why Kubernetes?
- Benefits of Kubernetes
- Step-by-step implementation of Product Service
- Containerization of Applications using Docker and Kubernetes
Prerequisites
- Visual Studio 2022
- Docker and Kubernetes
- .NET Core 7 SDK
What is Docker?
- Docker is an open-source containerization platform that enables developers to build, run, and deploy applications quickly. Docker package application, including all its libraries, configurations, and dependencies.
- Its primary focus is to automate the deployment of applications inside containers that boot up in seconds.
Why use Docker?
- In the tech world, I think you've heard the phrase “It works on my machine,” and mostly, this happens because of the different libraries, configurations, and dependencies required for the application to run under the operating system.
- Managing application dependencies and configuration is a crucial task for the DevOps team, and Docker has all the capabilities to handle this kind of problem in the software development lifecycle.
- Docker helps us build and deploy distributed microservice applications with the help of continuous integration and a continuous deployment pipeline, which saves a lot of time.
- Docker uses the container as a unit of software that packages application code with all its dependencies so the application can run quickly in isolated environments.
Benefits of Docker
- Application portability: Docker is the container platform and allows running containers on a physical machine, virtual machine, or any other cloud provider in less time without modification
- Faster delivery and deployment: Docker enables us to build and deploy application images across every step of the deployment phase efficiently.
- Scalability: Docker is scalable because it has the ability to increase and decrease the number of application instances easily in different environments.
- Isolation: Docker containerizes and runs the application in an isolated environment with all dependencies and configurations.
- Security: Docker ensures the applications running inside the different containers are isolated from each other, and it has different security layers and tools to manage that.
- High Performance: Docker is generally faster and takes fewer resources than VMs.
- Version Control Management: Docker provides versioning-related things that can track container versions and roll back the same if required.
If you want to learn more about Docker and its basic components, then check out the following article:
https://www.c-sharpcorner.com/article/docker
What is Kubernetes?
- Kubernetes is a portable, extensible, open-source container orchestration platform for managing containerized workloads and services.
- Kubernetes, also called K8s, is basically a numeronym standard that has been used since the 1980s. For example, in Kubernetes, there are 8 words in between K and S like that.
- Google developed an internal system called Borg and later Omega, which they used to orchestrate the data center.
- In 2014, Google introduced Kubernetes as an open-source project, and it is written in the Go language. Later on, she donated to the Cloud Native Computing Foundation (CNCF).
- Kubernetes has all the capabilities to automate container deployment, load balancing, and auto-scaling.
Why Kubernetes?
- Containers are a good way to bundle and run applications in an isolated environment, but with that, we need to manage containers efficiently without any downtime. For example, if the application is running in a production environment and the running container goes down, you will need to create a new container using different commands or some other things. But, at a large level, it’s really hard to manage a number of containers.
- As a solution, Kubernetes comes into the picture because it is a container orchestration platform and has all kinds of capabilities like auto-scaling, load-balancing, version control, health monitoring, auto-scheduling, and many more.
- Kubernetes monitors everything; if multiple users login at the same time and traffic suddenly increases, it will auto-scale and provide other resources to different containers that are running inside the node.
Benefits of Kubernetes
- Auto Scaling: Kubernetes automatically increases and decreases the number of pods based on network traffic and allocates different resources.
- Automate Deployment: Kubernetes automates the deployment of applications with the help of different cloud providers.
- Fault Tolerance: Kubernetes manages all things related to the container. If he finds one of the pods and the container goes down due to high network traffic, it will automatically start a new instance and provide different resources for it.
- Load Balancing: Kubernetes load balances and manages all incoming requests from outside the cluster, and it continuously looks at the running pods under different nodes and sends a request to a particular service using the load balancing technique.
- Rollout and Rollback: Kubernetes rollout and rollback if anything wrong happens with the application after certain changes and managing to version
- Health Monitoring: Kubernetes continuously checks the health of the running node to see if the containers and pods are working fine or not.
- Platform Independent: Kubernetes is an open-source tool, which is why it can move workloads and applications anywhere on public cloud, on-premises, hybrid, and public cloud infrastructure.
If you want to learn more about Kubernetes and its basic components, then check out the following article:
https://www.c-sharpcorner.com/article/kubernetes
Step-by-step implementation of Product Service
Step 1
Create a new .NET Core Web API Project.
Step 2
Install the following NuGet packages for the newly created project:

Step 3
Add a new Product Details entity with different properties related to the product.
namespace NET_Core_WebAPI_Kubernetes_Demo.Entities
{
public class ProductDetails
{
public int Id { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public int ProductPrice { get; set; }
public int ProductStock { get; set; }
}
}
Step 4
Create a DbContextClass class inside the project for managing database-related things and configuration and a SeedData class to insert some data initially while running an application.
DbContextClass.cs
using Microsoft.EntityFrameworkCore;
using NET_Core_WebAPI_Kubernetes_Demo.Entities;
namespace NET_Core_WebAPI_Kubernetes_Demo.Data
{
public class DbContextClass : DbContext
{
public DbContextClass(DbContextOptions<DbContextClass>
options) : base(options)
{
}
public DbSet<ProductDetails> Products { get; set; }
}
}
SeedData.cs
using Microsoft.EntityFrameworkCore;
using NET_Core_WebAPI_Kubernetes_Demo.Entities;
namespace NET_Core_WebAPI_Kubernetes_Demo.Data
{
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new DbContextClass(
serviceProvider.GetRequiredService<DbContextOptions<DbContextClass>>()))
{
if (context.Products.Any())
{
return;
}
context.Products.AddRange(
new ProductDetails
{
Id = 1,
ProductName = "IPhone",
ProductDescription = "IPhone 14",
ProductPrice = 120000,
ProductStock = 100
},
new ProductDetails
{
Id = 2,
ProductName = "Samsung TV",
ProductDescription = "Smart TV",
ProductPrice = 400000,
ProductStock = 120
});
context.SaveChanges();
}
}
}
}
Step 5
Add product service repository to perform different database-related operations using DbContext after simply injecting from the Product Controller.
IProductService
using NET_Core_WebAPI_Kubernetes_Demo.Entities;
namespace NET_Core_WebAPI_Kubernetes_Demo.Repositories
{
public interface IProductService
{
public Task<List<ProductDetails>> ProductListAsync();
public Task<ProductDetails> GetProductDetailByIdAsync(int productId);
public Task<bool> AddProductAsync(ProductDetails productDetails);
public Task<bool> UpdateProductAsync(ProductDetails productDetails);
public Task<bool> DeleteProductAsync(int productId);
}
}
ProductService
using Microsoft.EntityFrameworkCore;
using NET_Core_WebAPI_Kubernetes_Demo.Data;
using NET_Core_WebAPI_Kubernetes_Demo.Entities;
namespace NET_Core_WebAPI_Kubernetes_Demo.Repositories
{
public class ProductService : IProductService
{
private readonly DbContextClass dbContextClass;
public ProductService(DbContextClass dbContextClass)
{
this.dbContextClass = dbContextClass;
}
public async Task<List<ProductDetails>> ProductListAsync()
{
return await dbContextClass.Products.ToListAsync();
}
public async Task<ProductDetails> GetProductDetailByIdAsync(int productId)
{
return await dbContextClass.Products.Where(ele => ele.Id == productId).FirstOrDefaultAsync();
}
public async Task<bool> AddProductAsync(ProductDetails productDetails)
{
await dbContextClass.Products.AddAsync(productDetails);
var result = await dbContextClass.SaveChangesAsync();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
public async Task<bool> UpdateProductAsync(ProductDetails productDetails)
{
var isProduct = ProductDetailsExists(productDetails.Id);
if (isProduct)
{
dbContextClass.Products.Update(productDetails);
var result = await dbContextClass.SaveChangesAsync();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
return false;
}
public async Task<bool> DeleteProductAsync(int productId)
{
var findProductData = dbContextClass.Products.Where(_ => _.Id == productId).FirstOrDefault();
if (findProductData != null)
{
dbContextClass.Products.Remove(findProductData);
var result = await dbContextClass.SaveChangesAsync();
if (result > 0)
{
return true;
}
else
{
return false;
}
}
return false;
}
private bool ProductDetailsExists(int productId)
{
return dbContextClass.Products.Any(e => e.Id == productId);
}
}
}






Join the conversation! Your thoughts help the community grow.