.NET Tutorial - Deploy a microservice to Azure

Intro

Purpose

Learn to deploy microservices, built with .NET and Docker, to Microsoft Azure.

Prerequisites

Required: This tutorial uses the app created in the Your First Microservice tutorial.

Required: Docker Hub account.

Time to Complete

15-20 minutes

Scenario

Take a previously created microservice and deploy it to Azure, using DockerHub and Azure Kubernetes Service (AKS).

Push to Docker Hub

Docker Hub is a central place to upload Docker images. Many products, including Microsoft Azure, can create containers based on images in Docker Hub.

Sign in to Docker Hub

In your command prompt, run the following command:

In your terminal, run the following command:

Terminal
docker login

Use the username and password created when you downloaded Docker in the previous tutorial. You can visit the Docker Hub website to reset your password if needed.

Upload image to Docker Hub

Re-tag (rename) your Docker image under your username and push it to Docker Hub using the following commands:

Terminal
docker tag mymicroservice [YOUR DOCKER USERNAME]/mymicroservice
docker push [YOUR DOCKER USERNAME]/mymicroservice

The push command will upload your image to Docker Hub, which may take some time.

Set up Azure tools

Create an Azure account

If you're new to Azure, you can create a free account. If you have an existing account, you can skip this step.

Create a free Azure account

Install Azure CLI

The Azure CLI provides tools for managing your Azure account.

Sign in to Azure

Once you've installed, open a new command prompt and sign in to your Azure account by running the following command:

Once you've installed, open a new terminal and sign in to your Azure account by running the following command:

Terminal
az login

Install AKS CLI

Kubernetes is a container orchestration platform. An orchestrator is responsible for running, distributing, scaling, and healing apps comprised of a collection of containers. Azure Kubernetes Service (AKS) provides Kubernetes as a managed service.

Run the following command to install the command-line tools for AKS:

Terminal
az aks install-cli

You may be alerted with recommendations to set system PATH variables. These are not required for this tutorial.

Create Azure resources

Create a resource group

A resource group is used to organize a set of resources related to a single app.

Run the following command to create a resource group on the West US region:

Terminal
az group create --name MyMicroserviceResources --location westus

If you want to use a different location on the previous command, you can run the following command to see which regions are available on your account and pick one closer to you:

Terminal
az account list-locations -o table

If you want to use a different subscription for the session you can get a list of all subscriptions by running the following command:

Terminal
az account list --all

Then you can run the following command to set a specific subscription for the session:

Terminal
az account set -s NAME_OR_ID

Create an AKS cluster

Run the following command to create an AKS cluster in the resource group:

It's normal for this command to take several minutes to complete.

Terminal
az aks create --resource-group MyMicroserviceResources --name MyMicroserviceCluster --node-count 1 --enable-addons http_application_routing --generate-ssh-keys

Run the following command to download the credentials to deploy to your AKS cluster:

Terminal
az aks get-credentials --resource-group MyMicroserviceResources --name MyMicroserviceCluster

Deploy to Azure

Return to app directory

Since you opened a new command prompt in the previous step, you'll need to return to the directory you created your service in.

Since you opened a new terminal in the previous step, you'll need to return to the directory you created your service in.

Terminal
cd MyMicroservice

Create a deployment file

The AKS tools use a .yaml file to define how to deploy your container.

Create a file called deploy.yaml with this command:

Terminal
touch deploy.yaml
Terminal
fsutil file createnew deploy.yaml 0

You can then open it in your favorite text editor.

You can then open it in your favorite text editor manually or with this command:

Terminal
open deploy.yaml
Terminal
start deploy.yaml

Replace the content of the deploy.yaml to the following in the text editor, making sure to replace [YOUR DOCKER ID] with your actual Docker ID.

deploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
 name: mymicroservice
spec:
 replicas: 1
 template:
 metadata:
 labels:
 app: mymicroservice
 spec:
 containers:
 - name: mymicroservice
 image: [YOUR DOCKER ID]/mymicroservice:latest
 ports:
 - containerPort: 80
 env:
 - name: ASPNETCORE_URLS
 value: http://*:80
 selector:
 matchLabels:
 app: mymicroservice
---
apiVersion: v1
kind: Service
metadata:
 name: mymicroservice
spec:
 type: LoadBalancer
 ports:
 - port: 80
 selector:
 app: mymicroservice

Run deployment

Run the following command to deploy your microservice based on the settings in deploy.yaml:

Terminal
kubectl apply -f deploy.yaml

Test your deployed service

Run the following command to see the details of your deployed service:

Terminal
kubectl get service mymicroservice --watch

Among other things, the previous command will show the external IP address that your service is available on (EXTERNAL-IP).

Using the external IP address, open a new browser window and navigate to http://[YOUR EXTERNAL IP ADDRESS]/weatherforecast

If the EXTERNAL-IP is marked as <pending>, a new line will automatically appear once the external IP has been allocated.

Press CTRL+C on your command prompt to end the kubectl get service command.

Press CTRL+C on your terminal to end the kubectl get service command

Congratulations! You've deployed a microservice to Azure.

Scale your service

A benefit of using Kubernetes is that you can easily scale up a deployment to multiple instances to handle additional load. Currently, there's only a single instance, so let's scale to two instances.

Run the following command to scale your service up to two instances:

Terminal
kubectl scale --replicas=2 deployment/mymicroservice

Clean up resources

When you're done testing the microservice, you can delete all resources that you created with the following command:

Terminal
az group delete -n MyMicroserviceResources
Follow us