Setting up an Azure Kubernetes Cluster for a .NET Core MVC app

The last couple of weeks I’ve been playing around with Kubernetes (popular abbreviation: k8s). Kubernetes is the #1 container orchestration tool that helps you deploying and scaling containers.

It took me some time figuring out how to get my simple .NET Core MVC app running in Kubernetes on Azure. I started off with some very basic Docker knowledge, and it turned out Kubernetes throws many more concepts in your face you need to get to know!

I will show you the steps it took to get my .NET Core MVC app running on a managed Kubernetes cluster in Azure. I might skip a lot of details here. If you want to understand some concepts better (like Docker, private container registries, Kubernetes) I suggest you google them during the steps they’re mentioned in.

Prerequisites:

  • An Azure subscription
  • A .NET MVC Core app
  • Docker or Docker Toolbox
  • A private container registry (for instance Azure Container Registry )

Step 1: Create a Kubernetes cluster

You can create a Kubernetes cluster using the Azure CLI (either in the portal or from your local PowerShell). These steps are also available in the portals GUI, but since you’re going to need the command line pretty often in this tutorial we’ll stick to commands only 🙂

First I created a new resource group

Then I added a Kubernetes cluster with a single node (=VM) of the Standard_A1_v2 size. By default you’ll get 3 nodes with better specs, but I didn’t need that right now.

Please note you can always add increase or decrease the nodes, but you won’t be able to add nodes of different VM specs. So this is pretty important decision you make up front.

I also added this thing called http_application_routing that allows me to easily access the application through http on our cluster.

From my experience it will take a minute or 15 before everything is setup and the nodes have been provisioned. In the Azure portal you’ll see a bunch of new resources and a another new resource group (the cluster infrastructure group).

Image for post
Image for post

When everything is setup you can associate the CLI with the cluster by using the following command

This allows you to access the cluster through the kubectl commands (by default installed on the Azure Portal, if you want to use kubectl locally check these instructions).

After this step I can control my Kubernetes cluster through the command line. A simple check is to run this command.

Which showed me:

This is the single node (virtual machine) I provisioned to the cluster. You won’t see a master node. If you see a master node I think you’ve setup an unmanaged kubernetes cluster through Azure Container Service (and used acs in stead of aks in the commands).

With the unmanaged service you are responsible for updating the control plane, and there are more differences I won’t go into right now.

Step 2: Dockerize the .NET Core MVC app

There are pretty good tutorials out there explaining this. I’ll show you my docker file and the commands I use.

My project is called OMT.Web en this is the Docker file to build it into a container image.

It copies the solution to the build container (microsoft/aspnetcore-build) which publishes the MVC project to the runtime container (microsoft/aspnetcore:2.0). By default port 80 is exposed since it’s handling web requests.

I build this Docker file (that’s called Web) from the root of my solution using the following command:

Before I can push it to the private registry I have to authenticate first using docker login.

Now I can tag my local image and push it to the private registry

It will show me something like:

The image is in the registry, and the cluster is ready for use. Let’s deploy.

Step 3: Deploy the container in Kubernetes

As I told you in the intro: Kubernetes throws a bunch of concepts at you:

  • Deployment: Configuring and creating of all of the below
  • Pods: One or multiple containers that will be guaranteed to run on the same node (handy if you have multiple containers that depend on each other presence on the same node)
  • Services: Allows traffic to your pods
  • Ingress: Allows traffic to your services through domain names

I created a YAML file (omt.yaml) that sets up a pod, service and ingress through a deployment:

As you can see in the file, the image is pulled from my private registry. Kubernetes uses credentials for this. I had to save these credentials in Kubernetes first through the command line.

I created the deployment of my app with the following command.

It showed me

So the container is running on the cluster, but how do we access it? The Azure Kubernetes cluster has a public IP assigned to the ingress controller to it. You can find it through querying the ingress.

You can also let Azure create a domain name for your resource. Read all about this in the documentation of http application routing. Convenient if you just want to add a CNAME record in yourdomain.com pointing to the cluster.

Once you’ve setup the DNS records your .NET Core MVC application will be served from your Kubernetes cluster in Azure.

Tips

  • The tutorials on the Kubernetes website are pretty good if you need more knowledge on Kubernetes itself.
  • You can view your Azure Kubernetes dashboard by using the Azure CLI powershell
  • Test your container on your machine first before you push it to the registry (it’s harder to debug it there :-))

Written by

.NET developer living in The Netherlands. I write stories because it helps me sharing and remembering knowledge.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store