how to get started with Kubernetes (k8s) and Dotnet core

how to get started with Kubernetes (k8s) and Dotnet core

What is Kubernetes

Kubernetes is an open-source container orchestration system for automating the deployment, scaling, and management of containerized applications. It provides a platform-agnostic way to manage containerized workloads, making it easy to deploy and scale applications in a consistent and predictable manner. In this project, we will create a simple .NET Core web application, package it in a Docker container, and deploy it to a Kubernetes cluster. This will give you an understanding of how to use Kubernetes to manage your containerized applications and the different components and concepts involved in a typical Kubernetes deployment.

You can find the source code and the complete project on my GitHub page at the following link:

github.com/vamcan/k8s-Test

Please give it a star if you found it useful :)

let's do it!

  1. Create a new .NET Core web application using the command dotnet new web -n MyApp

  2. Add a simple API endpoint in the Startup.cs file for example:

     app.MapGet("/", () => "Hello World!");
    
  3. Create a Dockerfile in the root of the project with the following contents:

     FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
     WORKDIR /app
     EXPOSE 80
     EXPOSE 443
    
     FROM mcr.microsoft.com/dotnet/sdk:7.0 as build
     WORKDIR /app
    
     COPY . .
     RUN dotnet restore
     RUN dotnet publish -o /app/published-app
    
     FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime
     WORKDIR /app
     COPY --from=build /app/published-app /app
     ENTRYPOINT [ "dotnet", "/app/MyApp.dll" ]
    
  4. A Dockerfile is a script that contains a list of commands that are executed to create a Docker image.

    The FROM instruction specifies the base image that the final image will be built on top of. The first instruction FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base specifies that the base image is the official Microsoft .NET Core runtime image for version 7.0.

    WORKDIR /app sets the working directory for the following instructions to /app within the container.

    EXPOSE 80 and EXPOSE 443 specifies the ports that the container will listen to, in this case it's listening on port 80 and 443.

    The next FROM instruction specifies the build image, FROM mcr.microsoft.com/dotnet/sdk:7.0 as build. This image includes the .NET Core SDK, which is required to build the application.

    WORKDIR /app sets the working directory for the following instructions to /app within the container.

    COPY . . copies all files from the current working directory on the host to the /app directory in the container.

    RUN dotnet restore runs the dotnet restore command to restore NuGet packages for the application.

    RUN dotnet publish -o /app/published-app runs the dotnet publish command to build and publish the application to the /app/published-app directory in the container.

    The final FROM instruction specifies the runtime image, FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime. This image includes the .NET Core runtime and is used to run the application.

    WORKDIR /app sets the working directory for the following instructions to /app within the container.

    COPY --from=build /app/published-app /app copies the published application from the build stage to the /app directory in the container.

    ENTRYPOINT [ "dotnet", "/app/MyApp.dll" ] specifies the command that should be run when the container starts. This command runs the .NET Core runtime and specifies the application's DLL file, MyApp.dll, as the entry point for the container.

    Build the Docker image using the command docker build -t myapp .

    Run the Docker container using the command docker run -p 8082:80 myapp

  5. You can test your application by visiting http://localhost:8082 in a web browser.

    Kubernetes Deployments

  6. Once you have your cluster set up, you can create a Kubernetes deployment using the following YAML files:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: myapp
       labels:
         app: my-app
     spec:
       replicas: 2
       selector:
         matchLabels:
           service: myapp
       template:
         metadata:
           labels:
             app: my-sample-app
             service: myapp
         spec:
           containers:
             - name: myapp
               image: myapp
               imagePullPolicy: Never
               ports:
                 - containerPort: 80
                   protocol: TCP
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: myapp
       labels:
         app: my-sample-app
         service: myapp
     spec:
       type: LoadBalancer
       ports:
         - port: 11000
           targetPort: 80
           protocol: TCP
       selector:
         service: myapp
    
  7. The Deployment resource is used to manage the desired state of a set of replicas of your application. It ensures that the specified number of replicas (in this case, 2) are running at any given time. It also provides self-healing capabilities, meaning if a replica goes down, the Deployment will automatically create a new one to replace it.

    The metadata section of the Deployment defines a name for the Deployment, "myapp", as well as labels "app: my-sample-app". The labels are used to identify the Deployment and can be used for filtering and querying.

    The spec section of the Deployment defines the desired state of the replicas, the number of replicas is defined as 2, and the selector section is used to determine which pods the Deployment should manage. The matchLabels section defines a label "service: myapp" that the pods should have.

    The template section defines the actual pod template that will be used to create new replicas. It has the same labels defined in the metadata section and the spec section defines the container image that should be used and the port that should be exposed. The imagePullPolicy is set to Never, meaning that the image will not be pulled from a remote registry, it assumes that the image is already present on the node.

    The Service resource is used to expose the application inside the cluster to the outside world, or to other parts of the cluster. It provides a stable endpoint for the pods that are managed by the Deployment.

    The metadata section of the Service defines a name for the service, "myapp", and labels "app: my-sample-app" and "service: myapp".

    The spec section of the Service defines the type of the service, in this case, a LoadBalancer, which will create an external load balancer in the cluster. The ports section defines the ports that should be exposed, in this case, it's port 11000 and target port is 80.

    The selector section is used to determine which pods the Service should route traffic to, in this case, it is looking for pods with the label "service: myapp"

    In summary, the Deployment resource is used to manage the desired state of the replicas of your application, and the Service resource is used to expose your application to the outside world or to other parts of the cluster.

  8. Apply these files to the cluster using the command kubectl apply -f deployment.yaml

  9. You can test your application by visiting http://localhost:11000 in a web browser.

  10. and you can use this command to delete clusters kubectl delete -f deployment.yaml

  11. In This Article, we try to understand how to create a .NET Core application and run it in a Docker container, and deploy it to a Kubernetes cluster.

    You can find the source code and the complete project on my GitHub page at the following link:

    https://github.com/vamcan/k8s-Test

    Please give it a star if you found it useful :)

Did you find this article valuable?

Support reza ghasemi by becoming a sponsor. Any amount is appreciated!