您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

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

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

在本快速入门中,你将使用 Azure PowerShell 创建你的第一个 DNS 区域和记录。In this quickstart, you create your first DNS zone and record using Azure PowerShell. 也可以使用 Azure 门户Azure CLI 执行这些步骤。You can also perform these steps using the Azure portal or the Azure CLI.

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 PowerShell 的 DNS 部署环境示意图。

Azure DNS 还支持创建专用域。Azure DNS also supports creating private domains. 有关如何创建第一个专用 DNS 区域和记录的分步说明,请参阅 Azure DNS 专用区域入门(使用 PowerShell)For step-by-step instructions about how create your first private DNS zone and record, see Get started with Azure DNS private zones using PowerShell.

必备条件Prerequisites

  • 具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 免费创建帐户Create an account for free.
  • 本地安装的 Azure PowerShell 或 Azure Cloud ShellAzure PowerShell installed locally or Azure Cloud Shell

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To start Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 选择“试用”不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Start Cloud Shell.

  2. 选择代码块上的“复制”按钮以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上选择 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 选择 Enter 运行此代码。Select Enter to run the code.

创建资源组Create the resource group

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

New-AzResourceGroup -name MyResourceGroup -location "eastus"

创建 DNS 区域Create a DNS zone

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

New-AzDnsZone -Name contoso.xyz -ResourceGroupName MyResourceGroup

创建 DNS 记录Create a DNS record

可以使用 New-AzDnsRecordSet cmdlet 创建记录集。You create record sets by using the New-AzDnsRecordSet cmdlet. 下面的示例在资源组“MyResourceGroup”的 DNS 区域“contoso.xyz”中创建相对名称为“www”的一个记录。The following example creates a record with the relative name "www" in the DNS Zone "contoso.xyz", in 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 秒。The record type is "A", with IP address "10.10.10.10", and the TTL is 3600 seconds.

New-AzDnsRecordSet -Name www -RecordType A -ZoneName contoso.xyz -ResourceGroupName MyResourceGroup -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "10.10.10.10")

查看记录View records

若要列出区域中的 DNS 记录,请使用:To list the DNS records in your zone, use:

Get-AzDnsRecordSet -ZoneName contoso.xyz -ResourceGroupName MyResourceGroup

测试名称解析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:

    Get-AzDnsRecordSet -ZoneName contoso.xyz -ResourceGroupName MyResourceGroup -RecordType ns
    
  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.com.
    

    应当会看到类似以下屏幕的内容: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.

清理资源Clean up resources

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

Remove-AzResourceGroup -Name MyResourceGroup

后续步骤Next steps

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