Oct 12

Your First Azure Kubernetes Service Cluster – Using .NET Core MVC Website

In this post I’m going to show you the steps I do in my conference talk “Getting Started with Azure Kubernetes Service”.


To start with, you need to have a few things. If you don’t already have an Azure account look at getting some free resources at https://azure.microsoft.com/en-us/free/

I’m on a Windows 10 system with Bash enabled and Ubuntu installed. I like using WSL with Ubuntu because my target OS type for my microservices and websites run on Linux. It helps me stay sharp with Linux commands, etc.

I have .NET Core installed. Make sure you have the latest version. https://www.microsoft.com/net/download

I also have Docker for Windows so I can build the images. You’ll see this later in this post.
Other things you’ll need is the AZ CLI. Once that is installed you can execute the AZ command to install the Kubernetes CLI

az aks install-cli

Hopefully everything is installed and ready at this point. Now, from your terminal, you need to log into your Azure Subscription.
Execute the following command will give you a series of characters you use at the site https://microsoft.com/devicelogin.  If not already logged in you’ll be prompted to log into your Azure subscription. Once done your terminal will show some details of the subscription you just logged into.

az login

Build a Cluster

In my talk I mention a script that I use to create a cluster and an Azure Container Registry. I found the script some time ago and tweaked it a little. It uses AZ commands so you’ll need to make sure you’re logged into the subscription you want first. I recommend making a copy of the commands in the script, paste into a text editor, then modify to your needs. Start with the Environment Variables at the top. **Warning** It takes roughly 15 minutes and could be longer. Most of the time is taken waiting for the VM’s to be provisioned and come online. The script is located at https://gist.github.com/seanw122/e7b43b543f2a44be767739ce3866237f

Building the MVC Site

While the cluster is being created you can create the ASP.NET MVC site. On your computer create a new folder. The name of the folder will be the name of the project so chose wisely. In my example I have a simple name of proj1. Amazing name I know.
So, now I have my folder “D:/code/proj1”. In the address bar of the window click once. It should highlight the whole string. Type cmd and press Enter. You should see the Command Prompt window located at “D:/code/proj1”.
Now for some .NET Core. Type in the following command. It will create an ASP.NET MVC website using a generic template.

dotnet new mvc

After the site is created you’ll see several files. Find in the Controllers folder the file HomeController.cs. Edit that file and modify the method About:

public IActionResult About()
    ViewData["Message"] = "My About page. " + Environment.MachineName + ": " + Environment.OSVersion + ": " + DateTime.Now.ToString();
    return View();

This shows the Machine Name, OS Version, and the current date and time in UTC.
This is point out that the machine name is the name of the pod it is running on. The OS Version will prove that it’s running on Linux though it will show “UNIX” with some version. The date and time shows the page is running live.

Now create a new file name “Dockerfile” with no extension then put in the contents:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base

FROM microsoft/dotnet:2.1-sdk AS build
COPY . .
RUN dotnet restore proj1.csproj
RUN dotnet build proj1.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish proj1.csproj -c Release -o /app

FROM base AS final
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "proj1.dll"]

Place this file in your proj1 folder. It needs to be there for the next command to work.


Docker Image

Now we’ll use Docker to build an image, tag to a different version, then push to our new Azure Container Registry.

docker build . -t myproj:v1

docker tag myproj:v1 {ACR Name}.azurecr.io/myproj:v1

docker push {ACR Name}.azurecr.io/myproj:v1

The first command builds the image. It will pull the image for .NET Core, ASP.NET Core then layer on our new MVC site.  The second command adds a tag to the image. There is only one final image. Now it has two tags. I do it this way so there is one for local and one specific to the ACR we’re pushing to.  Be sure to replace the {ACR Name} with the actual ACR Name you used in the script to create the cluster. The third command pushes the image to ACR specified in the tag.

In the case where the push tries but fails stating “Authorization Required”, use the following command to login. Be sure to use the name of the ACR you’re targeting without the curly braces.

az acr login --name {ACR Name}


Deploying to Cluster

By now the image should be successfully pushed to the Azure Container Registry. With you text editor create two new files and place the following contents.

First file save as <i>myproj-service.yml</i>

apiVersion: v1
kind: Service
  name: my-project-service
    app: my-project-server
  type: LoadBalancer
    - port: 8080
      targetPort: 80

And the second one as <i>myproj-deployment.yml</i>

apiVersion: extensions/v1beta1
kind: Deployment
  name: my-project-deployment
  replicas: 3
  minReadySeconds: 10
    type: RollingUpdate
      maxUnavailable: 1
      maxSurge: 1
        app: my-project-server
        - name: my-project
          image: {ACR Name}.azurecr.io/myproj:v1
            - containerPort: 80


Using the same terminal used to create the cluster we’re going to send these two files to the cluster to create a Deployment of 3 Pods that is accessible by a Service.  First you may need to navigate to the folder where you created these files. In my example I created the files in the same location as my MVC site, “D:/code/proj1”. To navigate to that location:

cd /mnt/d/code/proj1

Now execute this command to list the files and verify the two files we’re going to send are indeed in the folder.

ls -al

Now to create the Service. Why Service first?  In our case it will obtain a public IP address and that takes a few minutes. So, we should get that started now.

kubectl create -f myproj-service.yml

With the Service creation on the way we’ll now create the Deployment and the Pods.

kubectl create -f myproj-deployment.yml

A Deployment specifies information about the Pods to be created. Behind the scenes it creates a Replica Set. In our example we have it set to 3 replicas. The Scheduler works to maintain that number of active Pods.

To see the status of our Service execute:

kubectl get service -o wide

Look for your Service listed and the associated External IP address.  It may still be <i>Pending</i> in which case just wait a minute and try again.

Once you have an External IP address copy that address and we’ll put that in a browser’s address bar. But, note the port number. In this example it’s set to 8080. So, be sure to specify that in the browser as well.

Now your new site should appear!  Click on the link at the top for About. In the text that appears you should see the machine name, OS Version, and date & time.  The machine name is the name of the Pod that is hosting that request of the web server. The OS Version will say “Unix” plus some version numbers.



You created a new Kubernetes cluster on Azure and now hosting a new ASP.NET MVC website. There’s SO much more about AKS. To get a list of links I found useful go to my other post at http://seanwhitesell.com/2018/06/23/resources-for-getting-started-with-azure-kubernetes-service-with-net-core-prometheus-and-grafana

hublot replica | replica watches | cartier replica sale | breitling replica sale