使用 ARM 模板创建 Azure DNS 区域和记录Quickstart: Create an Azure DNS zone and record using an ARM template

本快速入门介绍如何使用 Azure 资源管理器模板(ARM 模板)来创建 DNS 区域以及其中的 A 记录。This quickstart describes how to use an Azure Resource Manager template (ARM Template) to create a DNS zone with an A record in it.

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.

部署到 AzureDeploy to Azure

先决条件Prerequisites

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

查看模板Review the template

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

本快速入门将使用后缀 azurequickstart.org 创建唯一的 DNS 区域。In this quickstart, you'll create a unique DNS zone with a suffix of azurequickstart.org. 指向两个 IP 地址的 A 记录也将放置在该区域中。An A record pointing to two IP addresses will also be placed in the zone.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "newZoneName": {
      "type": "string",
      "defaultValue": "[concat(uniqueString(resourceGroup().id), '.azurequickstart.org')]",
      "metadata": {
        "description": "The name of the DNS zone to be created.  Must have at least 2 segements, e.g. hostname.org"
      }
    },
    "newRecordName": {
      "type": "string",
      "defaultValue": "www",
      "metadata": {
        "description": "The name of the DNS record to be created.  The name is relative to the zone, not the FQDN."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Network/dnsZones",
      "apiVersion": "2018-05-01",
      "name": "[parameters('newZoneName')]",
      "location": "global"
    },
    {
      "type": "Microsoft.Network/dnsZones/A",
      "apiVersion": "2018-05-01",
      "name": "[concat(parameters('newZoneName'), '/', parameters('newRecordName'))]",
      "location": "global",
      "dependsOn": [
        "[parameters('newZoneName')]"
      ],
      "properties": {
        "TTL": 3600,
        "ARecords": [
          {
            "ipv4Address": "1.2.3.4"
          },
          {
            "ipv4Address": "1.2.3.5"
          }
        ]
      }
    }
  ],
  "outputs": {
    "nameServers": {
      "type": "array",
      "value": "[reference(parameters('newZoneName')).nameServers]"
    }
  }
}

该模板中已定义了两个 Azure 资源:Two Azure resources have been defined in the template:

部署模板Deploy the template

  1. 按照说明登录到 Azure。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. chinaeast2)"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-azure-dns-new-zone/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.

    模板部署使用指向两个 IP 地址的 A 记录创建一个区域。The template deployment creates a zone with one A record pointing to two IP addresses. 资源组名称是追加了 rg 的项目名称。The resource group name is the project name with rg appended.

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

    Azure DNS 区域资源管理器模板 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. 登录 Azure 门户Sign in to the Azure portal.

  2. 从左侧窗格中选择“资源组”。Select Resource groups from the left pane.

  3. 选择你在上一部分中创建的资源组。Select the resource group that you created in the previous section. 默认资源组名称是追加了 rg 的项目名称。The default resource group name is the project name with rg appended.

  4. 资源组应包含以下资源:The resource group should contain the following resources seen here:

    DNS 区域部署资源组

  5. 选择带有后缀 azurequickstart.org 的 DNS 区域,验证该区域是否使用引用值 1.2.3.41.2.3.5A 记录正确创建。Select the DNS zone with the suffix of azurequickstart.org to verify that the zone is created properly with an A record referencing the value of 1.2.3.4 and 1.2.3.5.

    DNS 区域部署

  6. 复制上一步中其中一个名称服务器名称。Copy one of the name server names from the previous step.

  7. 打开一个命令提示符,并运行以下命令:Open a command prompt, and run the following command:

    nslookup www.<dns zone name> <name server name>
    

    例如:For example:

    nslookup www.2lwynbseszpam.azurequickstart.org ns1-01.azure-dns.cn.
    

    应会看到类似以下屏幕截图的内容:You should see something like the following screenshot:

    DNS 区域 nslookup

主机名 www.2lwynbseszpam.azurequickstart.org 解析为 1.2.3.41.2.3.5,正如你配置的那样。The host name www.2lwynbseszpam.azurequickstart.org resolves to 1.2.3.4 and 1.2.3.5, just as you configured it. 此结果表明名称解析正常工作。This result verifies that name resolution is working correctly.

清理资源Clean up resources

当不再需要使用 DNS 区域创建的资源时,请删除资源组。When you no longer need the resources that you created with the DNS zone, delete the resource group. 这会删除该 DNS 区域和所有相关资源。This removes the DNS zone 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:

  • DNS 区域DNS zone
  • A 记录A record

使用 ARM 模板创建第一个 DNS 区域和记录之后,接下来即可在自定义域中为 Web 应用创建记录。Now that you've created your first DNS zone and record using an ARM template, you can create records for a web app in a custom domain.