快速入门:使用 Azure CLI 创建 Azure DNS 区域和记录Quickstart: Create an Azure DNS zone and record using Azure CLI

本文将引导你完成使用 Azure CLI(适用于 Windows、Mac 和 Linux)创建你的第一个 DNS 区域和记录的步骤。This article walks you through the steps to create your first DNS zone and record using Azure CLI, which is available for Windows, Mac and Linux. 也可以使用 Azure 门户Azure PowerShell 执行这些步骤。You can also perform these steps using the Azure portal or Azure PowerShell.

DNS 区域用来托管某个特定域的 DNS 记录。A DNS zone is used to host the DNS records for a particular domain. 若要开始在 Azure DNS 中托管域,需要为该域名创建 DNS 区域。To start hosting your domain in Azure DNS, you need to create a DNS zone for that domain name. 随后会在此 DNS 区域内为每个 DNS 记录创建域。Each DNS record for your domain is then created inside this DNS zone. 最后,要将 DNS 区域发布到 Internet,需要为域配置名称服务器。Finally, to publish your DNS zone to the Internet, you need to configure the name servers for the domain. 以下描述了上述每一个步骤。Each of these steps is described below.

Azure DNS 还支持专用 DNS 区域。Azure DNS also supports private DNS zones. 若要详细了解专用 DNS 区域,请参阅将 Azure DNS 用于专用域To learn more about private DNS zones, see Using Azure DNS for private domains. 有关如何创建专用 DNS 区域的示例,请参阅通过 CLI 开始使用 Azure DNS 专用区域For an example on how to create a private DNS zone, see Get started with Azure DNS private zones using CLI. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

创建资源组Create the resource group

在创建 DNS 区域之前,创建一个资源组来包含 DNS 区域:Before you create the DNS zone, create a resource group to contain the DNS zone:

az group create --name MyResourceGroup --location "China East"

创建 DNS 区域Create a DNS zone

使用 az network dns zone create 命令创建 DNS 区域。A DNS zone is created using the az network dns zone create command. 若要查看此命令的帮助,请键入 az network dns zone create -hTo see help for this command, type az network dns zone create -h.

以下示例在 MyResourceGroup 资源组中创建名为 contoso.xyz 的 DNS 区域。The following example creates a DNS zone called contoso.xyz in the resource group MyResourceGroup. 使用该示例创建 DNS 区域,将相应的值替换成自己的值。Use the example to create a DNS zone, substituting the values for your own.

az network dns zone create -g MyResourceGroup -n contoso.xyz

创建 DNS 记录Create a DNS record

若要创建 DNS 记录,请使用 az network dns record-set [record type] add-record 命令。To create a DNS record, use the az network dns record-set [record type] add-record command. 有关 A 记录的帮助,请参阅 azure network dns record-set A add-record -hFor help on A records, see azure network dns record-set A add-record -h.

以下示例在资源组“MyResourceGroup”中创建在 DNS 区域“contoso.xyz”中具有相对名称为“www”的一个记录。The following example creates a record with the relative name "www" in the DNS Zone "contoso.xyz" in the resource group "MyResourceGroup". 记录集的完全限定名称为“www.contoso.xyz”。The fully-qualified name of the record set is "www.contoso.xyz". 记录类型为“A”,IP 地址为“10.10.10.10”,并且使用了默认 TTL 3600 秒(1 小时)。The record type is "A", with IP address "10.10.10.10", and a default TTL of 3600 seconds (1 hour).

az network dns record-set a add-record -g MyResourceGroup -z contoso.xyz -n www -a 10.10.10.10

查看记录View records

若要列出区域中的 DNS 记录,请运行:To list the DNS records in your zone, run:

az network dns record-set list -g MyResourceGroup -z contoso.xyz

测试名称解析Test the name resolution

现在,你已有一个测试 DNS 区域且其中有一个测试“A”记录,可以使用名为 nslookup 的工具来测试名称解析了。Now that you have a test DNS zone with a test 'A' record, you can test the name resolution with a tool called nslookup.

测试 DNS 名称解析:To test DNS name resolution:

  1. 运行以下 cmdlet 以获取区域的名称服务器列表:Run the following cmdlet to get the list of name servers for your zone:

    az network dns record-set ns show --resource-group MyResourceGroup --zone-name contoso.xyz --name @
    
  2. 从上一步的输出中复制其中一个名称服务器名称。Copy one of the name server names from the output of the previous step.

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

    nslookup www.contoso.xyz <name server name>
    

    例如:For example:

    nslookup www.contoso.xyz ns1-08.azure-dns.cn.
    

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

    nslookup

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

删除所有资源Delete all resources

当不再需要时,可以通过删除资源组来删除本快速入门中创建的所有资源:When no longer needed, you can delete all resources created in this quickstart by deleting the resource group:

az group delete --name MyResourceGroup

后续步骤Next steps

现在,你已使用 Azure CLI 创建了你的第一个 DNS 区域和记录,可以在自定义域中为 Web 应用创建记录了。Now that you've created your first DNS zone and record using Azure CLI, you can create records for a web app in a custom domain.