April 6, 2019

Deploy dotnet core app with Docker and Kubernetes


This example will be using dotnet core 2.2.202, Docker 18.09.2 and Kubernetes 1.10.11

Very well, lets get started.

Enable Kubernetes

You need to enable Kubernetes in Docker settings, at least if you are on Windows.

Once enabled, we can open a terminal and fire away.

Create the app

We need an app to deploy in a Docker container. We can cheat by generating a simple Razor app from dotnet core.

Great. We have an app!

Create Dockerfile

We need a Dockerfile that Kubernetes can use to deploy.

Create the file in Windows cmd (if you’re using *nix you know what to do):

Add this contents:

Dockerfile completed. We can now proceed.

Build Docker image

Easy peasy.

Deploy the image in Kubernetes

You can now see what the run command gave you using kubectl get all:

Wat?! Lots of stuff in here! Not only a pod with the app but also a replicaset and deployment. We do not get a service for free though.

Create a service to wrap the pod(s)

We do this by calling kubectl expose for our deployment.

There we go. Now we have a service to interact with instead of talking directly to the pods. This abstraction helps us alot since the replicaset may bring up or kill pods whenever.

Launch http://localhost:[service port] to view your sample app. In the example here, my service port is 32697 .

Check logs

You can check logs of your pods of course.

Nice, we can see our sample app launching on port 80.

Clean up

Since this is only a small example I’m sure you want to clean up after yourself. You can do this using kubectl delete.

System is once again fresh and clean. Yay

Glossary

Deployment

A Deployment controller provides declarative updates for Pods and ReplicaSets.

You describe a desired state in a Deployment object, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.


Kubernetes docs on Deployments

Pod

A Pod is the basic building block of Kubernetes–the smallest and simplest unit in the Kubernetes object model that you create or deploy. A Pod represents a running process on your cluster.

Kubernetes docs on Pods

ReplicaSet

ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods.

Kubernetes docs on ReplicaSet

Service

A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them – sometimes called a micro-service. The set of Pods targeted by a Service is (usually) determined by a Label Selector (see below for why you might want a Service without a selector).

Kubernetes docs on Services