
Introduction
In this article, we are going to discuss a few basics of auto-scaling in Kubernetes, as well as the step-by-step implementation of weather forecast application using the .NET Core 6 Web API and the containerization of weather forecast service with the help of Docker and Kubernetes.
Agenda
- What is Docker?
- Why Docker?
- Benefits of Docker
- What is Kubernetes?
- Why Kubernetes?
- Benefits of Kubernetes
- Auto-scaling in Kubernetes
- Step-by-step implementation of the Weather Forecast Application
- Containerization of Applications using Docker and Kubernetes
- Auto-Scaling Implementation with Kubernetes
- Monitor Applications with High Traffic
Prerequisites
- Visual Studio 2022
- Docker and Kubernetes
- .NET Core 6 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 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 can 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 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 several 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 log in 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
Auto-scaling in Kubernetes
Auto Scaling in Kubernetes means that Kubernetes can automatically change the number of duplicate copies of your application running (called "replica pods") depending on how much your application is being used. It does this by keeping an eye on how much the central processing unit (CPU) of your computer is being used, or by looking at other specific measurements you've set up. This way, your application always has enough resources to handle different levels of demand without needing someone to adjust it manually.
Two main types of auto-scaling in Kubernetes
There are two main types of auto-scaling in Kubernetes
1. Horizontal pod auto scaler (HPA)
- It keeps an eye on how much of your computer's CPU is being used.
- You tell it what percentage of CPU usage you want to aim for, and how many copies of your application should be running at a minimum and maximum.
- If your CPU usage goes above or below the target you set, HPA will automatically add or remove copies of your application to make sure the CPU usage stays where you want it.
2. Vertical pod auto scaler (VPA)
- It looks at how much CPU and memory your application is using.
- Instead of adding or removing copies of your application, as HPA does, VPA changes how much CPU and memory each copy of your application asks for.
- This helps make sure that each copy of your application gets just the right amount of CPU and memory it needs, which can make your whole system run more efficiently.
Step-by-step implementation of Weather Forecast API
Step 1. Create a new .NET Core Web API Project.
Step 2. Weather Forecast class with required properties.
namespace AutoScaleK8SDemo
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}
Step 3. Weather forecast controller with action method.
using Microsoft.AspNetCore.Mvc;
namespace AutoScaleK8SDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
Step 4. Register for the required services.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.Run();
Containerization of applications using Docker and Kubernetes
Note. Please make sure Docker and Kubernetes are running on your system.
Step 1. Create a Docker image for our newly created application.
# Use the official .NET Core SDK as a parent image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app
# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore
# Copy the rest of the application code
COPY . .
# Publish the application
RUN dotnet publish -c Release -o out
# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./
# Expose the port your application will run on
EXPOSE 80
# Start the application
ENTRYPOINT ["dotnet", "AutoScaleK8SDemo.dll"]
Step 2. Build the Docker image.
docker build -t web-api.
The docker build command is used to build a Docker image from a Docker file. It includes a variety of options, including the -t option to specify a tag for an image.

This command creates a Docker image that uses the Dockerfile in the current directory (.) and marks it as web-API.
Step 3. Run the docker image inside a docker container.
docker run -d -p 5001:80 — name web-api-container web-api
- -d: Detached mode (run in the background).
- -p 5001:80: Map port 5001 on your local machine to port 80 inside the container.
- -name web-apiContainer: Assign a name to the container.
- web-api: Use the image you built earlier.












Rijwan AnsariPosted Aug 4, 2024, 3:53 AM
Nice article. Thanks for sharing