快速入門:使用 Azure PowerShell 建立 Azure DNS 區域和記錄

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

在本快速入門中,您會使用 Azure PowerShell 建立您的第一個 DNS 區域和記錄。 您也可以使用 Azure 入口網站Azure CLI 來執行這些步驟。

DNS 區域用於裝載特定網域的 DNS 記錄。 若要開始將網域裝載到 Azure DNS 中,您必須建立該網域名稱的 DNS 區域。 接著在此 DNS 區域內,建立網域的每筆 DNS 記錄。 最後,若要將 DNS 區域發佈至因特網,您必須設定網域的名稱伺服器。 本文將說明上述每個步驟。

Diagram of DNS deployment environment using the Azure PowerShell.

Azure DNS 也支援建立私人網域。 如需如何建立第一個私人 DNS 區域和記錄的逐步指示,請參閱 使用 PowerShell 開始使用 Azure DNS 私人區域。

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶
  • 已在本機安裝 Azure PowerShell 或 Azure Cloud Shell

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Screenshot that shows an example of Try It for Azure Cloud Shell.
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 Button to launch Azure Cloud Shell.
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

建立資源群組

建立 DNS 區域之前,請先建立資源群組以包含 DNS 區域:

New-AzResourceGroup -name MyResourceGroup -location "eastus"

建立 DNS 區域

DNS 區域是使用 New-AzDnsZone Cmdlet 所建立。 下列範例會在名為 MyResourceGroup 的資源群組中建立名為 contoso.xyz 的 DNS 區域。 使用範例來建立 DNS 區域,以取代您自己的值。

New-AzDnsZone -Name contoso.xyz -ResourceGroupName MyResourceGroup

建立 DNS 記錄

使用 New-AzDnsRecordSet Cmdlet 建立記錄集。 下列範例會在資源群組 MyResourceGroup的 DNS 區域 contoso.xyz中建立具有相對名稱www的記錄。 記錄集的完整名稱為 www.contoso.xyz。 記錄類型為 A,具有IP位址 10.10.10.10,而TTL為3600秒。

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

檢視記錄

若要列出您區域中的 DNS 記錄,請使用:

Get-AzDnsRecordSet -ZoneName contoso.xyz -ResourceGroupName MyResourceGroup

測試名稱解析

既然您有測試 DNS 區域,且具有測試 『A』 記錄,您可以使用名為 nslookup 的工具來測試名稱解析。

若要測試 DNS 名稱解析:

  1. 執行下列 Cmdlet 以取得您區域的名稱伺服器清單:

    Get-AzDnsRecordSet -ZoneName contoso.xyz -ResourceGroupName MyResourceGroup -RecordType ns
    
  2. 從上一個步驟的輸出複製其中一個名稱伺服器名稱。

  3. 開啟命令提示字元,然後執行下列命令:

    nslookup www.contoso.xyz <name server name>
    

    例如:

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

    您應該會看到類似下列畫面的內容:

    Screenshot shows a command prompt window with an n s lookup command and values for Server, Address, Name, and Address.

主機名 www.contoso.xyz 解析為 10.10.10.10,就像您設定一樣。 此結果會驗證名稱解析是否正常運作。

清除資源

若不再需要,您可以刪除資源群組,以刪除本快速入門中建立的所有資源:

Remove-AzResourceGroup -Name MyResourceGroup

下一步

既然使用 Azure PowerShell 建立您的第一個 DNS 區域和記錄,您可以在自訂網域中建立 Web 應用程式的記錄。