DEV Community

Cover image for Deploy .NET API to Kubernetes using Kind
Matheus Paixão
Matheus Paixão

Posted on • Edited on

Deploy .NET API to Kubernetes using Kind

Hi Devs and Divas.

Today I am going to show you a simple way to install run and test your API locally with docker caontainer and orchestrating with Kubernetes.

If you ever heard that Kubernetes is super difficult, don't worry, it's not that bad.

devops

In this article you will learn how to deploys a simple API to kubernetes with a local environment:

Pre-requirements

You should have the basic concept knowledge of:

  • Kubernetes
  • Docker/Containers
  • Kind for Kubernetes
  • .net API

Preparing the API

We will create a basic Weatherforecast API that dotnet gives us.
 

Vs Code

  • Open VS CODE, open a new terminal, navigate to the folder you wish to create your project for example (cd source/repos).
  • Now type dotnet new webapi -o "weather" to create a sample webapi project.
  • Now open this project folder in the vscode.
  • To auto-create the Docker support that is available upon project creation in Visual Studio follow the next steps:

  • Install the Docker extension.

  • Press Ctrl + Shift + P

  • Type Add Docker Files to Workspace

  • Select .NET: ASP.NET Core

  • Select Linux

  • Type the port you wish to expose

  • Select No for docker compose. At this first moment we won't need it.

Then you notice it generated a new Dockerfile in your workspace.

Visual Studio

  • Open visual studio and create a ASPNET Core Web API and make sure you enable Docker Support.

 
 

Swagger Support

  • After the project is created you go to Program.cs (.net 6 above) or to Startup.cs (.net 6 below) and remove the IF condition from the swagger setup   From this:
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
Enter fullscreen mode Exit fullscreen mode

Should look like this:

    app.UseSwagger();
    app.UseSwaggerUI();
Enter fullscreen mode Exit fullscreen mode

 

We do this for testing purpose only, because when you push it to kubernetes you need to set which environment you wish to build your application. You can add ASPNETCORE_ENVIRONMENT=Development in your docker profile, it will work as well.
 

Publishing API in Docker Hub

You must have Docker installed and have a Docker hub Account to complete this step.

In your IDE, open terminal and build the API:

docker build -t weather . 
Enter fullscreen mode Exit fullscreen mode

As a prevention measure, test your container before orchestrating with kubernetes. So lets run the image. (You can use whatever port you wish)

docker container run -d -p 8070:80 --name containername weather
Enter fullscreen mode Exit fullscreen mode

Now you should be able to see swagger page if you type localhost:8070/swagger

  • Pushing to Docker Hub

Before pushing you can add tags to your image.

docker tag weather dockerHubUserName/weather
Enter fullscreen mode Exit fullscreen mode

Now you can push it

docker push dockerHubUserName/weather
Enter fullscreen mode Exit fullscreen mode

If you open your docker hub on your browser you should be able to see your image.

 

Installing Kind

To install kind refer to original documentation https://kind.sigs.k8s.io/docs/user/quick-start/

But let me show you what I did.

Image3

  • Now if you have docker running, you should be able to create a cluster.
kind create cluster
Enter fullscreen mode Exit fullscreen mode

Wait till it creates the cluster.

You should see a new container running on your docker. To check if the container is running type docker ps in your terminal and you should see a container named kind-control-plane from the image kindest.

To check if there are any clusters type on your terminal:

kind get clusters
Enter fullscreen mode Exit fullscreen mode

And you should get a response like this:

kind
Enter fullscreen mode Exit fullscreen mode

Preparing the API for Kubernetes

To access your application you need at least 2 kubernetes objects:

deploy.yml - A Deployment provides declarative updates for Pods and ReplicaSets.
service.yml - An abstract way to expose an application running on a set of Pods as a network service. To access your pod you need a service to expose it.

You don't need to know how to write it by hear, you can get a template and edit.

I recommend you use VsCode with YAML extension to edit your yml files, because it indentation is very delicate, and Visual Studio has no support for this extension.

This is a simplified version, for testing purpose, there are many options and features to be added according to your project

  • Create a file in your project weather-deploy.yml

Template:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: Application Name
spec:
  replicas: How many Replicas?
  template:
    metadata:
      labels:
        app: Application Name
    spec:
      containers:
      - name: Application Name
        image: dockerHubUser/applicationName
  selector:
    matchLabels:
      app: Application Name
Enter fullscreen mode Exit fullscreen mode

Our project:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: weather
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: weather
    spec:
      containers:
      - name: weather
        image: dockerHubUser/weather
  selector:
    matchLabels:
      app: weather
Enter fullscreen mode Exit fullscreen mode

Don't forget to add you docker hub User

  • Create a file in your project weather-service.yml

Template:

apiVersion: v1
kind: Service
metadata:
  name: Project Name
spec:
  selector:
    app: Project Name
  ports:
  - port: port?
    targetPort: 80 is the standart port
Enter fullscreen mode Exit fullscreen mode

Our project:

apiVersion: v1
kind: Service
metadata:
  name: weather
spec:
  selector:
    app: weather
  ports:
  - port: 8070
    targetPort: 80
Enter fullscreen mode Exit fullscreen mode

 

Pushing API to Kubernetes

To orchestrate the API to kubernetes pod we will only push this 2 files as the project will come from the docker image.

Terminal:

kubectl apply -f weather-deploy.yml
Enter fullscreen mode Exit fullscreen mode
kubectl apply -f weather-service.yml
Enter fullscreen mode Exit fullscreen mode

Terminal response should be:

deployment.apps/weather created
Enter fullscreen mode Exit fullscreen mode
service/weather created
Enter fullscreen mode Exit fullscreen mode
  • Wait a minute and check if your pod and service are running:

Terminal:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Terminal Response:
Imag2

Terminal:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Terminal response:

kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   xxxxx       <none>        443/TCP    23h
weather      ClusterIP   xxxxx   <none>        8070/TCP   3m53s
Enter fullscreen mode Exit fullscreen mode

 

Accessing your Pod

To access your pod you need to forward a port to the service.

kubectl port-forward service/weather 8070:8070
Enter fullscreen mode Exit fullscreen mode

Now you should be able to see the Swagger page in your browser typing localhost:8070/swagger

Basically you are telling the kubernetes that when you access port 8070 locally, you will access 8070 on kubernetes service, which is our pod's service.

tHANKS

Top comments (1)

Collapse
 
eduardobueno profile image
Eduardo Bueno

Great article! I think it will help people who are new to K8s and want to know more about it by following the walkthrough you described.