Deploying DotNet Core Application using Kubernetes

Simarpreet Singh Gujral
Akeo
Published in
4 min readNov 22, 2019

--

In my last blog, I talked about hosting a sample application using docker. Now, continuing with the series like we mentioned, we’ll talk about how to deploy DotNet Core application using Kubernetes.

First things, first, since you are here to learn how to deploy a DotNet application, we believe that you already have a sample dotnet core application ready with you. Secondly, the deployment steps mentioned here have been performed on a Ubuntu16.04 machine.

Step 1: Creating dockerfile

  • Create a dockerfile with the following contents and place it in the root location of the project repository.
##DockerFile#Pulling dotnet sdk image from dockerhubFROM microsoft/dotnet:2.1-sdk##Create required directories and initial updateRUN \  apt-get update && \  mkdir /home/coderepo && \  mkdir -p /var/webapps/app##Copy repository contents to Docker ContainerCOPY . /home/coderepo/##Building the projectRUN \  cd /home/coderepo/ && \  dotnet publish -c Release && \  cd src/project_name/bin/Release/netcoreapp2.1/publish/ && \  cp -r . /var/webapps/app/##Copy script to Docker ContainerCOPY startup.sh /home/# Make startup file executableRUN chmod 755 /home/startup.sh#Expose PortEXPOSE 80# Set the default command to execute when creating a new containerENTRYPOINT [“/home/startup.sh”]

Note: Don’t forget to replace the project_name with the actual project name.

  • Create startup.sh file with the following contents and place it in the root location of the project repository.
#!/bin/bash
cd /var/webapps/app/
/usr/bin/dotnet project_name.dll
/bin/bash

Note: Don’t forget to replace the project_name with the actual project name.

Step 2: Creating Kubernetes yaml file

  • Create a Kubernetes yaml file (dotnetapi.yaml) with the following contents and place it in the root location of the project repository.
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-dotnet
labels:
app: sample-dotnet
spec:
selector:
matchLabels:
app: sample-dotnet
template:
metadata:
labels:
app: sample-dotnet
spec:
containers:
- image: ip_address_of_the_machine:5000/dotnetcore-api-image
name: dotnet-api
imagePullPolicy: Always
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: sample-dotnet-svc
spec:
ports:
- name: “sample-dotnet-api”
targetPort: 80
port: 80
nodePort: 30295
protocol: TCP
selector:
app: sample-dotnet
type: NodePort

The above snippet comprises of:

  • apiVersion — which version of the Kubernetes API is being used to create this object
  • kind — what kind of object is being created
  • metadata — data that helps uniquely identify the object, including a name string, UID, and optional namespace
  • spec — what state is being desired for the object

The selector field defines how the deployment find the pods to manage, it is defined in the template (app: sample-dotnet). The template.spec defines the docker image being used to create the pod. Replace the ip_address_of_the_machine:5000 with the private docker registry details. The containerport defines the port on which application will run inside the pod.

The Kubernetes Service section: The Kubernetes services is responsible for enabling network access to a set of pods.

  • port: defines the port on which application is running inside the pod
  • nodePort: defines the port to be bind to the base machine
  • protocol: TCP default protocol for the network communication
  • selector: to identify the deployment created above

Step 3: Jenkins Configuration

  • Creating Jenkins job
  • Open the Jenkins url and click on “New Item”
  • Provide a name for the job and select freestyle job
  • Provide the git repository url and credentials for cloning the project. Also specify the branch
  • Select “Execute Shell” from the add build step
  • Provide the steps as shown
  • Save the job and execute it
  • This will create docker image and push to the private docker registry. Also, the kubectl apply command will configure the pod with dotnet core application running inside it. The purpose of pushing the docker image to the docker registry is that this image can be used on any other Ubuntu machine to run the application.
  • Access the sample application on the url http://ip_address_of_machine:30295

And voila we are done! The next in line in the automated deployment series is how to ‘Deploy NodeJs applications using Kubernetes.’

--

--