Containers on Azure – Deploy your .NET Core Docker Container to ACS with Kubernetes

arrow_downward

 

Containers are all over the place in the IT world right now. Their highest impact can most likely be felt in the context of microservice architectures. Small, independent but interoperating services are often deployed alongside each other. Containers create a good abstraction and separate services running on the same host.

Managing microservices in containers, however, creates new challenges for the tools that we use during development and operations. Here, we want to explore how creating and deploying a containerized .NET Core service works with Kubernetes on Azure.

Goal

The goal of this article is that you are able to create, publish and deploy a .NET Core application in a Docker container to a Kubernetes cluster hosted on Azure. You will first set up a Kubernetes cluster on Azure, then create your own .NET Core service, package it as a Docker image and deploy it to the cluster.

 

All the infrastructure resources required to run the cluster are provided by Azure, such as the virtual machines, networks, IP addresses and load balancers. Kubernetes is used to manage these resources by applying configuration via commands or scripts. The infrastructure that we’re going to set up consists of one master node (required to access and manage the cluster) and two agent nodes (which will run the services we deploy). The services run in pods on the agent nodes, which are created from the Docker images we provide. The services in the pods are only available internally by default, but we want to make them accessible from the web and therefore provision load balancers and public IP addresses.

 

Here is an overview of the infrastructure that we’re creating.

Managing a Docker Infrastructure on Azure with Kubernetes

(click image to enlarge)

Requirements

Before you start, make sure that you have the following tools installed on your machine and that you have access to an Azure account. You can create one for free to try it out.

The instructions are written for MacOS but should work in the same or a similar way on Windows.

Steps Overview

Create a Kubernetes Cluster

We need to create the cluster on Azure and use Kubernetes as the orchestrator. Later, we will deploy containers into this cluster.

Instructions

First, open a new terminal window and log in to Azure. Follow the instructions on the screen.

Next, we’ll set some script variables which will make it easier later. Make sure to create the resource group in Azure before using it here.

 

Now it’s time to create the cluster:

This command tells Azure to create a new Azure Container Services resource using Kubernetes as the orchestrator. There will be one master node and two agent nodes. The agents will be created in the size specified above. You might want to scale down the master node to A1_V2 in order to keep the costs at a minimum. The –generate-ssh-keys makes sure that Azure creates a new service principal in Azure AD used for the cluster. It generates SSH keys stored on the master node.

If you want to keep costs low, you can configure the VMs to automatically shut down. In the Azure Portal, you can go to the VMs, select a VM, go to Auto-shutdown and enable the shutdown for a certain time like 7pm. Otherwise this experimental setup creates unnecessary costs. Repeat this for all your VMs!

 

Next, get the cluster credentials:

This stores a credentials file for the cluster on your local machine, which will be used to access the cluster later on.

 

You can now manage your cluster with kubectl. For example, the next command lists all nodes in the cluster:

Before we deploy our own code, try it out by deploying a simple nginx container to the cluster:

 This deploys the nginx web server container to the cluster behind a load balancer on port 80.

 

Wait for the deployment to complete. Running the following command will show the external IP address of the load balancer once it finished deployment:

 Finally, go to http://your-external-ip:80 and you will see the default nginx welcome screen.

Congrats, you created a Kubernetes-managed cluster on Azure and deployed your first Docker container!

Sources and Further Reading

Create a .NET Core Project

We want to deploy our own application and not just an existing image. We’re going to create a small .NET Core project for that.

Instructions

Let’s first create a new project in order to deploy our own application to the cluster.

 This creates a new .NET Core project based on the WebApi template.

Feel free to adjust the code in any way you like, for example in Visual Studio Code. In this example, we’ll just use the code generated by the template, which returns some default values in the /api/values controller.

 

In the console, restore the dependencies and run the project.

 You should now be able to browse to http://localhost:5000/api/values and receive a JSON response.

 

Finally, publish a release version of the application to the /out folder.

 That was simple. Next we’ll pack it up in a container image.

Sources and Further Reading

Create a Docker container

In order to deploy the application to the cluster, we need to pack it up in an image. We use Docker to do so.

Instructions

First, we need a Dockerfile, which is a simple text file called ‚Dockerfile‘ without any file endings (at least for now).

 It specifies which base Docker image to use (dotnet 1.1.2 in our case), where to place the files and where to copy them from. It also runs the application on port 8080 and exposes the container to incoming TCP traffic on this port. The entrypoint is specified as our published .DLL file.

Now, build the Docker image.

This command will build the container image with the Dockerfile provided in the current directory and uses the tag ‚projects‘, which will later be used to identify the container.

 

Next, run the container locally.

 This will bind the container to port 8080 and show the log output in the console.

You should now be able to access the same application in the browser under http://localhost:8080/api/values.

 

If something didn’t work, you can access the log files of the container like this:

Great, so now let’s publish and deploy a container to Azure!

Sources and Further Reading

Publish the container

After successfully creating the container image, it is time to deploy it as a container to the cluster. Containers in Kubernetes are called pods.

Instructions

In order to deploy the container image to a cluster, we need to publish it to a registry first. There are several registries available, some public and some private. Probably best known is Docker Hub, which is public by default, but private repositories are available for a monthly fee. We decide to go with Azure Container Registry, a private repository service that charges by the amount of storage used.

You can either create an ACR through the portal or with the following CLI command:

Now, login with Docker at the ACR. Before you do that, check your Docker config file ($HOME/.dockercfg or $HOME/.docker/config.json). If there is a credstore entry, you might need to delete it first. Get the username and password from the Access Keys tab of the Azure Container Registry in the Azure Portal. Enable Admin access if not enabled.

Tag the local container image that you want to publish with the directory within the repository that you want to push it to.

Finally, push the container image to the repository.

Nice, the image is now stored in the Azure Container Registry and ready to be deployed to the cluster.

Sources and Further Reading

Deploy the container

It’s now time to create a container based on the image we just pushed.

Instructions

Deploy your custom container to the cluster and expose it with the following commands. You specify the name you want to use as an identity as well as the path to the image in the ACR. Then you expose the port 8080 with a load balancer.

Now run the following command again to get the public IP of the newly deployed service.

Navigate to http://your-external-ip:8080/api/values to see the output of your service.

Nice! Your own application is now running in your Kubernetes cluster.

Sources and Further Reading

Monitor the cluster

After deployment, you might want to know how your cluster is performing. Run the Kubernetes dashboard to get visual insights into your cluster.

Instructions

You can now proxy te the Kubernetes cluster and open the dashboard in order to monitor your cluster:

This will make the dashboard available at http://127.0.0.1:8001/ui.

A Kubernetes Dashboard Monitoring an Azure Cluster

Sources and Further Reading

 

Cool, this was hard but you managed to deploy your own .NET Core application from a Docker image into a Kubernetes cluster hosted on Azure.


Get Angularity updates

mail

 

Schreibe einen Kommentar!

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

comment
face
mail
place