Skip to content

ASP.NET Core Series 03: Containerizing a RESTful API and Deploying to Kubernetes Using Azure Container Service

Hi, all!

We have developed a RESTful API in the previous ASP.NET Core blog post series, and then we deployed that to the Azure App Services. In this blog post series, I’ll try to mention how we can containerize a RESTful API, and then how we can deploy it to Kubernetes using Azure Container Service, based my experience in the .NET Core transformation process at the company I’m currently working at.

Topics are:

  1. Building a Docker Image and Push to Docker Hub
  2. Creating a Kubernetes Cluster using Azure Container Service
    1. Creating an SSH RSA Public Key
    2. Creating a Service Principal Client ID and Secret
    3. Creating a Kubernetes Cluster
    4. Connecting to Kubernetes Cluster
    5. Running ASP.NET Core RESTful API inside to Kubernetes Cluster’s Pod
    6. Exposing Kubernetes Pod to Outside with Kubernetes Service
    7. Creating and Exposing Kubernetes Deployment Controller

NOTE: In this article, I won’t describe some topics, such as what the Docker is and why we need that etc. For this article, you need to know some Docker information.

As we know, a container image is a lightweight, stand-alone, executable package of a piece of software. Containers isolate applications in the same infrastructure. Thus, development and staging environment differences are separated from each other, thereby helping to reduce any conflicts that may occur.

We will use the example project that we developed in my previous post titled “ASP.NET Core Series 01: Building RESTful API with Dapper and Deploy to Azure App Services” for containerizing. Let’s download the example project from GitHub, and then unzip to the desktop.

1. Building a Docker Image and Push to Docker Hub

NOTE: Btw, I’m using DockerToolbox on the Windows.

First, we need to create a Dockerfile to building an image. Let’s create a Dockerfile in the root directory of the project as shown below.

In the Dockerfile, we defined that we want to use the official compiled ASP.NET Core image and expose on port “5000“. Also as an entrypoint, we specified the “aspnetcore-rest-api-with-dapper.dll” file that we’ll get after publishing the project. Now, let’s build the project using a command-line tool as shown below.

NOTE: Don’t forget to restore packages using the “dotnet restore” command.

After building the project, we need to publish the project using as following command:

Now, we can build an image using the Dockerfile.

We built an image called “aspnetcorerestapionlinux” using the “bin\Release\netcoreapp1.1\publish” path. Let’s see the image using the following command:

There are the “aspnetcorerestapionlinux” image as we can see on the above picture.

Now, let’s run the container to make sure everything is okay.

We ran the “aspnetcorerestapionlinux” image in a container and exposed it to outside on the port “5000“. Let’s open the “ on the browser.

Yeap, the image is running successfully in the container. Now, we have the containerized ASP.NET Core RESTful API.

We need to push the “aspnetcorerestapionlinux” image to the Docker Hub. Thus, we can pull this image, and then run in a Kubernetes cluster with Azure Container Service. First, we need to have a Docker Hub account. If you don’t have an account, you can create one here.

Let’s add a tag on the image as shown below.

NOT: Don’t forget to change “gokgokalp” field with your Docker Hub username.

Now, we need to login.

After the login process, we can push the image to the Docker Hub.

2. Creating a Kubernetes Cluster using Azure Container Service

Azure Container Service provides solutions to simplify the creation of virtual machines for containerized applications, and to make it easier to configure and manage clusters. Microsoft made the Kubernetes container orchestration service completely available a few months ago. In this way, deploying containerized applications efficiently and scaling them is made easy.

Now, we will create a basic Kubernetes cluster using Azure Container Service. We need the following items to create a cluster:

  • Azure subscription – I mentioned in “ASP.NET Core Series 01” blog post, how we can get a subscription.
  • SSH RSA public key – We need to create this for authenticating to virtual machines.
  • Service Principal client ID and secret – Also, we need to “Azure Active Directory service principal client ID” and a “secret”, for using Kubernetes orchestrator.

2.1 Creating an SSH RSA Public Key

Git for Windows should be installed on your computer. If you need:

Let’s create a “myPrivateKey” and “myCert” certificate using the “openssl.exe” with Git Bash.

While in this process, we need to fill some information such as “Country Name, State, E-mail, Organization Name“. After this process, the “myPrivateKey” and “myCert” certificate will be created under the related file path.

Now, we will create a public key with the private key that we have created.

We have to create an additional key to use PuTTY SSH client as shown below:

After execution the above command, so we got the private key called “myPrivateKey_rsa“.

Now, we will create a public key so that we can create a Linux VM on the Azure portal. First, we need to have PuTTY. If you need, you can download it from here. Let’s run the “PuTTYgen“, and click “Load“. Now we have to choose the “myPrivateKey_rsa” key file that we have created.

Now, we have a public key starting with “ssh-rsa” prefix as you can see on the above picture. We need to keep this public key to use later.

2.2 Creating a Service Principal Client ID and Secret

At this point, we can create an Azure AD (Active Directory) service principal using the portal’s terminal. After the login process to Azure portal, let’s click “Cloud Shell” button at the top right of the portal.

Now let’s create a resource group called “aspnetcore-test-restapi” using the following command.

After creating the resource group, we need to replace “mySubscriptionID” field of the following command with our subscription ID. After this, let’s run the command.

Now, we will see the following output.

We will use “appId” and “password” fields as principal client ID and secret.

2.3 Creating a Kubernetes Cluster

  1. Log in to the Azure Portal.
  2. Click “New” button at the top left of the Portal.
  3. Select “Containers > Azure Container Service” section as shown below.

  4. In the “Basics” section, click “Use existing” button from resource group and select the “aspnetcore-test-restapi” value that we have created, and then fill other fields as shown below.
  5. Now we’ll specify some important information such as orchestrator from the “Master configuration” section. Select “Kubernetes” as an “Orchestrator“, and then fill the “DNS name prefix” and others fields as shown below.

    Then complete the other fields such as “SSH public key” and “Service principal” with the information that we have created earlier.
  6. In the “Agent configuration” section, let’s select “1” as the “Agent count“, and “Standard DS2” as the “Agent virtual machine size“.

Now, we have a Kubernetes cluster.

2.4 Connecting to Kubernetes Cluster

We need to have “kubectl” to be able to connect to the Kubernetes cluster. First, let’s download and install Azure CLI from here. After installation step, open a command tool and install “kubectl” using the following command.

Now we have to download Kubernetes cluster configuration info. Therefore, login to Azure portal.

Let’s run the following command (don’t forget to change related fields):

After the downloading process, let’s see the node list to make sure the cluster configuration info has been successfully downloaded.

2.5 Running ASP.NET Core RESTful API inside to Kubernetes Cluster’s Pod

If I should say something about Pod, as in its documentation:

Pods are the smallest deployable units of computing in Kubernetes.

A Pod can contain one or more application containers. First, we need to create a new YAML file called “aspnetcore-rest-api-pod.yaml” under the root directory of the project to be able to create a pod in the Kubernetes cluster.

In the “image” section of YAML file, we specified our Docker image that we have pushed to Docker repo. If you remembered, we have exposed the API in a container on the port “5000“.

Now let’s run the following command using a command tool, under the root directory of the project.

We created the pod successfully. Let’s see the pod list.

As we can see the “aspnetcore-restapi” pod’s status is “running“. In this pod is running ASP.NET Core RESTful API that we have developed. At this point, we just need to expose this pod using Kubernetes Service.

2.6 Exposing Kubernetes Pod to Outside with Kubernetes Service

Basically, Kubernetes Service is an abstraction to creating policies and sets of a logical pod. If you want to reach more information, just look at here.

We need to create a new YAML file called “aspnetcore-rest-api-ks.yaml” for Kubernetes Service.

With the above YAML file, we’ll create a Kubernetes Service called “aspnetcore-restapi-service” and target TCP port “5000“. We should also make sure that the “app: aspnetcore-restapi” value is the same with pod label that we have created. Let’s run the following command:

Now we can see the service in the service list.

If we look at the above picture, we can see the “aspnetcore-restapi-service” service has a “” external IP and exposed to outside.

NOTE: IP assigning operation could take a few minutes.

When we reach to the external IP through the browser, we should see the Swagger UI as shown below.

Finally, we just need to create the Deployment Controller.

2.7 Creating and Exposing Kubernetes Deployment Controller

At this point, we have created a Kubernetes cluster using the Azure Container Service, and we defined a pod as the container of our RESTful API. Then, we defined a Kubernetes Service and exposed it to outside on the port “80” with a Load Balancer.

Well, if you read the topic “Durability of pods (or lack thereof)here, you can see that pods are not durable entities and you can’t reuse them in some situations, such as scheduling failures and node failures. This means when the pod dies, the related service will be unavailable. At this point, we need to use Kubernetes Controller to be production ready. There are a few Controllers such as Deployments and Replication in the Kubernetes. In here, we’ll use the Deployments, which is the new generation of the Replication Controller. Deployments generally provide important operations such as self-healing, rollout management and scaling.

Let’s create a new YAML file called “aspnetcore-rest-api-deployment.yaml“.

We defined the “replicas” section of the YAML file as “3“, and then under the “containers” section, we specified image and port info as they were in the pod creation YAML file. Thus, if any situation occurs such as scheduling failures and node failures, the Deployment will make sure that the number of replicas is 3.

Before creating of the deployment, we need to delete the “aspnetcore-rest-api-pod.yaml” pod and the “aspnetcore-rest-api-ks.yaml” service that we have created.

Now, we can create a deployment using the following command:

Let’s have a look at the pods.

We can see on the above picture, the deployment created the 3 pods and they are in “running” statuses. It also possible to automatically scale more loads with a deployment. For more information, click here.

The last thing we only have to do is to create a service and expose again this 3 pods to outside. For this operation, we’ll use again the “aspnetcore-rest-api-ks.yaml” file. Because a few steps before we have deleted the service to be able to create pods with the deployment controller.

This service will expose the 3 pods with the label “app: aspnetcore-restapi” to outside from the port “80“.

That’s all. We exposed these 3 pods to outside with the Load Balancer. If we want to see the endpoints, just run the following command:

After running the command, we can see that the Load Balancer service exposed the ““, “” and “” pods to outside on the “

I know, this is a long article, but it’s a really enjoyable and important topic. Currently, I’m working on Containerizing and Kuberneting topics at my company’s .NET Core transformation process for some projects. I hope, this article would help who needs any information about Containerizing and Kuberneting to .NET Core applications.

In the next .NET Core article series, I’ll try to describe how we can include these operations in the CI pipeline process using Jenkins.

Some References and Resources

Bu makale toplam (4649) kez okunmuştur.


Published inASP.NET CoreAzureContainerizing


  1. mustafa mustafa

    süper bir konu kalemine sağlık

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.