Quickstart: Create a Traffic Manager profile for a highly available web application using Azure CLI

This quickstart describes how to create a Traffic Manager profile that delivers high availability for your web application.

In this quickstart, you'll create two instances of a web application. Each of them is running in a different Azure region. You'll create a Traffic Manager profile based on endpoint priority. The profile directs user traffic to the primary site running the web application. Traffic Manager continuously monitors the web application. If the primary site is unavailable, it provides automatic failover to the backup site.

Diagram of Traffic Manager deployment environment.

If you don't have an Azure trial subscription, create an Azure trial subscription before you begin.

Prerequisites

  • If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

  • This article requires version 2.0.28 or later of the Azure CLI.

Note

Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run az cloud set -n AzureChinaCloud first to change the cloud environment. If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

Create a resource group

Create a resource group with az group create. An Azure resource group is a logical container into which Azure resources are deployed and managed.

The following example creates a resource group named myResourceGroup in the chinaeast location:


  az group create \
    --name myResourceGroup \
    --location chinaeast

Create a Traffic Manager profile

Create a Traffic Manager profile using az network traffic-manager profile create that directs user traffic based on endpoint priority.


mytrafficmanagerprofile='mytrafficmanagerprofile'$RANDOM

az network traffic-manager profile create \
	--name $mytrafficmanagerprofile \
	--resource-group myResourceGroup \
	--routing-method Priority \
	--path '/' \
	--protocol "HTTP" \
	--unique-dns-name $mytrafficmanagerprofile  \
	--ttl 30 \
	--port 80

Create web apps

For this quickstart, you'll need two instances of a web application deployed in two different Azure regions (China East and China North). Each will serve as primary and failover endpoints for Traffic Manager.

Create web app service plans

Create web app service plans using az appservice plan create for the two instances of the web application that you will deploy in two different Azure regions.


az appservice plan create \
    --name myAppServicePlanChinaEast \
    --resource-group myResourceGroup \
    --location chinaeast \
    --sku S1

az appservice plan create \
    --name myAppServicePlanChinaNorth \
    --resource-group myResourceGroup \
    --location chinanorth \
    --sku S1

Create a web app in the app service plan

Create two instances the web application using az webapp create in the App Service plans in the China East and China North Azure regions.


mywebappchinaeast='myWebAppChinaEast'$RANDOM
myWebAppchinanorth='myWebAppChinaNorth'$RANDOM

az webapp create \
    --name $mywebappchinaeast \
    --plan myAppServicePlanChinaEast \
    --resource-group myResourceGroup

az webapp create \
    --name $myWebAppchinanorth \
    --plan myAppServicePlanChinaNorth \
    --resource-group myResourceGroup

Add Traffic Manager endpoints

Add the two Web Apps as Traffic Manager endpoints using az network traffic-manager endpoint create to the Traffic Manager profile as follows:

  • Determine the Web App ID and add the Web App located in the China East Azure region as the primary endpoint to route all the user traffic.
  • Determine the Web App ID and add the Web App located in the China North Azure region as the failover endpoint.

When the primary endpoint is unavailable, traffic automatically routes to the failover endpoint.

China East endpoint


App1ResourceId=$(az webapp show --name $mywebappchinaeast --resource-group myResourceGroup --query id --output tsv)

az network traffic-manager endpoint create \
    --name $mywebappchinaeast \
    --resource-group myResourceGroup \
    --profile-name $mytrafficmanagerprofile \
    --type azureEndpoints \
    --target-resource-id $App1ResourceId \
    --priority 1 \
    --endpoint-status Enabled

China North endpoint


App2ResourceId=$(az webapp show --name $myWebAppchinanorth --resource-group myResourceGroup --query id --output tsv)

az network traffic-manager endpoint create \
    --name $myWebAppchinanorth \
    --resource-group myResourceGroup \
    --profile-name $mytrafficmanagerprofile \
    --type azureEndpoints \
    --target-resource-id  $App2ResourceId \
    --priority 2 \
    --endpoint-status Enabled

Test your Traffic Manager profile

In this section, you'll check the domain name of your Traffic Manager profile. You'll also configure the primary endpoint to be unavailable. Finally, you get to see that the web app is still available. It's because Traffic Manager sends the traffic to the failover endpoint.

In the following example, replace <app1name_chinaeast> and <app2name_chinanorth> with the App Names created for each region in the previous section. Then replace <profile_name> with the profile name used in the previous section.

Determine the DNS name

Determine the DNS name of the Traffic Manager profile using az network traffic-manager profile show.


az network traffic-manager profile show \
    --name $mytrafficmanagerprofile \
    --resource-group myResourceGroup \
    --query dnsConfig.fqdn

Copy the RelativeDnsName value. The DNS name of your Traffic Manager profile is http://<relativednsname>.trafficmanager.cn.

View Traffic Manager in action

  1. In a web browser, enter the DNS name of your Traffic Manager profile (http://<relativednsname>.trafficmanager.cn) to view your Web App's default website.

    Note

    In this quickstart scenario, all requests route to the primary endpoint. It is set to Priority 1.

  2. To view Traffic Manager failover in action, disable your primary site using az network traffic-manager endpoint update.

    
    az network traffic-manager endpoint update \
        --name $mywebappchinaeast \
        --resource-group myResourceGroup \
        --profile-name $mytrafficmanagerprofile \
        --type azureEndpoints \
        --endpoint-status Disabled
    
    
  3. Copy the DNS name of your Traffic Manager profile (http://<relativednsname>.trafficmanager.cn) to view the website in a new web browser session.

  4. Verify that the web app is still available.

Clean up resources

When you're done, delete the resource groups, web applications, and all related resources using az group delete.


az group delete \
    --resource-group myResourceGroup

Next steps

In this quickstart, you created a Traffic Manager profile that provides high availability for your web application. To learn more about routing traffic, continue to the Traffic Manager tutorials.