快速入门:使用 ARM 模板创建流量管理器配置文件Quickstart: Create a Traffic Manager profile using an ARM template

本快速入门介绍如何使用 Azure 资源管理器模板(ARM 模板)创建具有使用性能路由方法的外部终结点的流量管理器配置文件。This quickstart describes how to use an Azure Resource Manager template (ARM Template) to create a Traffic Manager profile with external endpoints using the performance routing method.

ARM 模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it.

如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. Azure 门户中会打开模板。The template will open in the Azure portal.

部署到 Azure Deploy to Azure

先决条件Prerequisites

如果没有 Azure 订阅,可在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速启动模板The template used in this quickstart is from Azure Quickstart Templates.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "uniqueDnsName": {
      "type": "string",
      "metadata": {
        "description": "Relative DNS name for the traffic manager profile, must be globally unique."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Network/trafficManagerProfiles",
      "apiVersion": "2018-08-01",
      "name": "ExternalEndpointExample",
      "location": "global",
      "properties": {
        "profileStatus": "Enabled",
        "trafficRoutingMethod": "Performance",
        "dnsConfig": {
          "relativeName": "[parameters('uniqueDnsName')]",
          "ttl": 30
        },
        "monitorConfig": {
          "protocol": "HTTPS",
          "port": 443,
          "path": "/",
          "expectedStatusCodeRanges": [
            {
              "min": 200,
              "max": 202
            },
            {
              "min": 301,
              "max": 302
            }
          ]
        },
        "endpoints": [
          {
            "type": "Microsoft.Network/TrafficManagerProfiles/ExternalEndpoints",
            "name": "endpoint1",
            "properties": {
              "target": "www.microsoft.com",
              "endpointStatus": "Enabled",
              "endpointLocation": "chinaeast2"
            }
          },
          {
            "type": "Microsoft.Network/TrafficManagerProfiles/ExternalEndpoints",
            "name": "endpoint2",
            "properties": {
              "target": "docs.microsoft.com",
              "endpointStatus": "Enabled",
              "endpointLocation": "chinaeast"
            }
          }
        ]
      }
    }
  ]
}

部署模板Deploy the template

  1. 打开本地 Shell,然后按照说明登录到 Azure。Open your local Shell, and then follow the instructions to sign in to Azure.

    $projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
    $location = Read-Host -Prompt "Enter the location (i.e. chinaeast)"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-traffic-manager-external-endpoint/azuredeploy.json"
    
    $resourceGroupName = "${projectName}rg"
    
    New-AzResourceGroup -Name $resourceGroupName -Location "$location"
    New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri
    
    Read-Host -Prompt "Press [ENTER] to continue ..."
    

    等到控制台中显示提示。Wait until you see the prompt from the console.

  2. 从上一个代码块中选择“复制”,以复制 PowerShell 脚本。Select Copy from the previous code block to copy the PowerShell script.

  3. 右键单击 shell 控制台窗格,然后选择“粘贴”。Right-click the shell console pane and then select Paste.

  4. 输入相应的值。Enter the values.

    模板部署将创建包含两个外部终结点的配置文件。The template deployment creates a profile with two external endpoints. Endpoint1 使用位于“中国北部”的目标终结点 www.microsoft.comEndpoint1 uses a target endpoint of www.microsoft.com with the location in China North. Endpoint2 使用位于“中国东部”的目标终结点 docs.microsoft.comEndpoint2 uses a target endpoint of docs.microsoft.com with the location in China East.

    资源组名称是追加了 rg 的项目名称。The resource group name is the project name with rg appended.

    备注

    “uniqueDNSname”需要是全局唯一名称,才能成功部署模板。uniqueDNSname needs to be a globally unique name in order for the template to deploy successfully. 如果部署失败,请从步骤 1 重新开始。If deployment fails, start over with Step 1.

    部署模板需要几分钟时间。It takes a few minutes to deploy the template. 完成后,输出类似于:When completed, the output is similar to:

    Azure 流量管理器资源管理器模板 PowerShell 部署输出

使用 Azure PowerShell 部署模板。Azure PowerShell is used to deploy the template. 除了 Azure PowerShell,还可以使用 Azure 门户、Azure CLI 和 REST API。In addition to Azure PowerShell, you can also use the Azure portal, Azure CLI, and REST API. 若要了解其他部署方法,请参阅部署模板To learn other deployment methods, see Deploy templates.

验证部署Validate the deployment

  1. 使用 Get-AzTrafficManagerProfile 确定流量管理器配置文件的 DNS 名称。Determine the DNS name of the Traffic Manager profile using Get-AzTrafficManagerProfile.

    Get-AzTrafficManagerProfile -Name ExternalEndpointExample -ResourceGroupName $resourceGroupName | Select RelativeDnsName
    

    复制 RelativeDnsName 值。Copy the RelativeDnsName value. 流量管理器配置文件的 DNS 名称为 <relativednsname>.trafficmanager.cnThe DNS name of your Traffic Manager profile is <relativednsname>.trafficmanager.cn.

  2. 在本地 PowerShell 中,运行以下命令,将 {relativeDNSname} 变量替换为 <relativednsname>.trafficmanager.cnFrom a local PowerShell run the following command by replacing the {relativeDNSname} variable with <relativednsname>.trafficmanager.cn.

    Resolve-DnsName -Name {relativeDNSname} | Select-Object NameHost | Select -First 1
    

    你应该获得的 NameHost 为 www.microsoft.comdocs.microsoft.com,具体取决于哪个区域离你更近。You should get a NameHost of either www.microsoft.com or docs.microsoft.com depending on which region is closer to you.

  3. 若要检查是否可以解析为其他终结点,请禁用在上一步中获得的目标的终结点。To check if you can resolve to the other endpoint, disable the endpoint for the target you got in the last step. 将 {endpointName} 替换为 endpoint1 或 endpoint2,分别禁用 www.microsoft.comdocs.microsoft.com 的目标 。Replace the {endpointName} with either endpoint1 or endpoint2 to disable the target for www.microsoft.com or docs.microsoft.com respectively.

    Disable-AzTrafficManagerEndpoint -Name {endpointName} -Type ExternalEndpoints -ProfileName ExternalEndpointExample -ResourceGroupName $resourceGroupName -Force
    
  4. 在本地 PowerShell 中再次运行步骤 2 中的命令。Run the command from Step 2 again in a local PowerShell. 这一次你应该获得其他终结点的其他 NameHost。This time you should get the other NameHost for the other endpoint.

清理资源Clean up resources

如果不再需要流量管理器配置文件,请删除资源组。When you no longer need the Traffic Manager profile, delete the resource group. 这会删除流量管理器配置文件和所有相关资源。This removes the Traffic Manager profile and all the related resources.

若要删除资源组,请调用 Remove-AzResourceGroup cmdlet:To delete the resource group, call the Remove-AzResourceGroup cmdlet:

Remove-AzResourceGroup -Name <your resource group name>

后续步骤Next steps

本快速入门介绍了如何创建流量管理器配置文件,In this quickstart, you created a Traffic Manager profile.

若要详细了解如何路由流量,请继续学习流量管理器教程。To learn more about routing traffic, continue to the Traffic Manager tutorials.