共用方式為


建立 Azure HDInsight 叢集的虛擬網路

此文章提供建立和設定 Azure 虛擬網路的範例和程式碼範例。 為了與 Azure HDInsight 叢集搭配使用。 提供如何建立網路安全性群組 (NSG) 和設定 DNS 的詳細範例。

如需如何將虛擬網路與 Azure HDInsight 搭配使用的背景資訊,請參閱為 Azure HDInsight 規劃虛擬網路

程式碼範例 (Sample) 和範例 (Example) 的必要條件

在執行此文章中的任何程式碼範例之前,請先了解 TCP/IP 網路功能。 如果您不熟悉 TCP/IP 網路功能,在實際執行網路中進行修改之前請先諮詢他人。

此文章中範例的其他必要條件包括下列項目:

  • 如果您使用的是 PowerShell,將必須安裝 AZ 模組
  • 如果您想要使用 Azure CLI 但尚未安裝,請參閱安裝 Azure CLI

重要

如果您要尋找使用 Azure 虛擬網路將 HDInsight 連線到內部部署網路的逐步指引,請參閱將 HDInsight 連線至內部部署網路文件。

範例:網路安全性群組與 HDInsight

此節中的範例示範如何建立網路安全性群組規則。 規則允許 HDInsight 與 Azure 管理服務通訊。 使用範例之前,請先調整 IP 位址,以符合您要使用之 Azure 區域的 IP 位址。 您可以在 HDInsight 管理 IP 位址中找到此資訊。

Azure Resource Manager 範本

下列 Resource Manager 範本所建立的虛擬網路會限制輸入流量,但允許來自 HDInsight 所需 IP 位址的流量。 此範本也會在虛擬網路中建立 HDInsight 叢集。

Azure PowerShell

使用下列 PowerShell 指令碼建立限制輸入流量的虛擬網路,並允許來自北歐區域之 IP 位址的流量。

重要

在此範例中變更 hdirule1hdirule2 的 IP 位址,以符合您使用的 Azure 區域。 您可以在 HDInsight 管理 IP 位址中找到此資訊。

$vnetName = "Replace with your virtual network name"
$resourceGroupName = "Replace with the resource group the virtual network is in"
$subnetName = "Replace with the name of the subnet that you plan to use for HDInsight"

# Get the Virtual Network object
$vnet = Get-AzVirtualNetwork `
    -Name $vnetName `
    -ResourceGroupName $resourceGroupName

# Get the region the Virtual network is in.
$location = $vnet.Location

# Get the subnet object
$subnet = $vnet.Subnets | Where-Object Name -eq $subnetName

# Create a Network Security Group.
# And add exemptions for the HDInsight health and management services.
$nsg = New-AzNetworkSecurityGroup `
    -Name "hdisecure" `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    | Add-AzNetworkSecurityRuleConfig `
        -name "hdirule1" `
        -Description "HDI health and management address 52.164.210.96" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "52.164.210.96" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 300 `
        -Direction Inbound `
    | Add-AzNetworkSecurityRuleConfig `
        -Name "hdirule2" `
        -Description "HDI health and management 13.74.153.132" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "13.74.153.132" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 301 `
        -Direction Inbound `
    | Add-AzNetworkSecurityRuleConfig `
        -Name "hdirule3" `
        -Description "HDI health and management 168.61.49.99" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "168.61.49.99" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 302 `
        -Direction Inbound `
    | Add-AzNetworkSecurityRuleConfig `
        -Name "hdirule4" `
        -Description "HDI health and management 23.99.5.239" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "23.99.5.239" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 303 `
        -Direction Inbound `
    | Add-AzNetworkSecurityRuleConfig `
        -Name "hdirule5" `
        -Description "HDI health and management 168.61.48.131" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "168.61.48.131" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 304 `
        -Direction Inbound `
    | Add-AzNetworkSecurityRuleConfig `
        -Name "hdirule6" `
        -Description "HDI health and management 138.91.141.162" `
        -Protocol "*" `
        -SourcePortRange "*" `
        -DestinationPortRange "443" `
        -SourceAddressPrefix "138.91.141.162" `
        -DestinationAddressPrefix "VirtualNetwork" `
        -Access Allow `
        -Priority 305 `
        -Direction Inbound `

# Set the changes to the security group
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg

# Apply the NSG to the subnet
Set-AzVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name $subnetName `
    -AddressPrefix $subnet.AddressPrefix `
    -NetworkSecurityGroup $nsg
$vnet | Set-AzVirtualNetwork

此範例示範如何新增規則,以允許所需 IP 位址上的輸入流量。 其不會包含可限制來自其他來源之輸入存取的規則。 下列程式碼示範如何啟用來自網際網路的 SSH 存取:

Get-AzNetworkSecurityGroup -Name hdisecure -ResourceGroupName RESOURCEGROUP |
Add-AzNetworkSecurityRuleConfig -Name "SSH" -Description "SSH" -Protocol "*" -SourcePortRange "*" -DestinationPortRange "22" -SourceAddressPrefix "*" -DestinationAddressPrefix "VirtualNetwork" -Access Allow -Priority 306 -Direction Inbound

Azure CLI

使用下列步驟建立限制輸入流量的虛擬網路,但允許來自 HDInsight 所需 IP 位址的流量。

  1. 使用下列命令來建立名為 hdisecure的新網路安全性群組。 將 RESOURCEGROUP 取代為包含 Azure 虛擬網路的資源群組。 將 LOCATION 取代為群組建立所在的位置 (區域)。

    az network nsg create -g RESOURCEGROUP -n hdisecure -l LOCATION
    

    建立群組之後,您會收到新群組的相關資訊。

  2. 使用下列將規則新增至新的網路安全性群組,這些規則允許從 Azure HDInsight 健康狀態和管理服務透過連接埠 443 的輸入通訊。 將 RESOURCEGROUP 取代為包含 Azure 虛擬網路的資源群組名稱。

    重要

    在此範例中變更 hdirule1hdirule2 的 IP 位址,以符合您使用的 Azure 區域。 您可以在 HDInsight 管理 IP 位址中找到此資訊。

    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule1 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "52.164.210.96" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 300 --direction "Inbound"
    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule2 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "13.74.153.132" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 301 --direction "Inbound"
    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule3 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "168.61.49.99" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 302 --direction "Inbound"
    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule4 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "23.99.5.239" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 303 --direction "Inbound"
    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule5 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "168.61.48.131" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 304 --direction "Inbound"
    az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n hdirule6 --protocol "*" --source-port-range "*" --destination-port-range "443" --source-address-prefix "138.91.141.162" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 305 --direction "Inbound"
    
  3. 若要擷取此網路安全性群組的唯一識別碼,請使用下列命令:

    az network nsg show -g RESOURCEGROUP -n hdisecure --query "id"
    

    此命令會傳回類似下列文字的值:

    "/subscriptions/SUBSCRIPTIONID/resourceGroups/RESOURCEGROUP/providers/Microsoft.Network/networkSecurityGroups/hdisecure"
    
  4. 使用下列命令將網路安全性群組套用至子網路。 將 GUIDRESOURCEGROUP 值取代為上一個步驟傳回的值。 將 VNETNAMESUBNETNAME 取代為您想要建立的虛擬網路名稱和子網路名稱。

    az network vnet subnet update -g RESOURCEGROUP --vnet-name VNETNAME --name SUBNETNAME --set networkSecurityGroup.id="/subscriptions/GUID/resourceGroups/RESOURCEGROUP/providers/Microsoft.Network/networkSecurityGroups/hdisecure"
    

    此命令完成之後,您可以將 HDInsight 安裝至虛擬網路。

這些步驟只會開啟 Azure 雲端上 HDInsight 健康狀態和管理服務的存取權。 任何其他來自虛擬網路外部之對 HDInsight 叢集的存取都會遭到封鎖。 若要允許從外部虛擬網路存取,您必須新增額外的網路安全性群組規則。

下列程式碼示範如何啟用來自網際網路的 SSH 存取:

az network nsg rule create -g RESOURCEGROUP --nsg-name hdisecure -n ssh --protocol "*" --source-port-range "*" --destination-port-range "22" --source-address-prefix "*" --destination-address-prefix "VirtualNetwork" --access "Allow" --priority 306 --direction "Inbound"

範例:DNS 設定

虛擬網路與已連線內部部署網路之間的名稱解析

此範例做出以下假設:

  • 您的 Azure 虛擬網路會使用 VPN 閘道連線至內部部署網路。

  • 虛擬網路中的自訂 DNS 伺服器會執行 Linux 或 Unix 作為作業系統。

  • Bind 安裝在自訂 DNS 伺服器上。

在虛擬網路的自訂 DNS 伺服器上:

  1. 若要尋找虛擬網路的 DNS 尾碼,請使用 Azure PowerShell 或 Azure CLI:

    RESOURCEGROUP 取代為包含虛擬網路的資源群組名稱,然後輸入下列命令:

    $NICs = Get-AzNetworkInterface -ResourceGroupName "RESOURCEGROUP"
    $NICs[0].DnsSettings.InternalDomainNameSuffix
    
    az network nic list --resource-group RESOURCEGROUP --query "[0].dnsSettings.internalDomainNameSuffix"
    
  2. 在虛擬網路的自訂 DNS 伺服器上,使用下列文字作為 /etc/bind/named.conf.local 檔案的內容:

    // Forward requests for the virtual network suffix to Azure recursive resolver
    zone "0owcbllr5hze3hxdja3mqlrhhe.ex.internal.cloudapp.net" {
        type forward;
        forwarders {168.63.129.16;}; # Azure recursive resolver
    };
    

    0owcbllr5hze3hxdja3mqlrhhe.ex.internal.cloudapp.net 值取代為虛擬網路的 DNS 尾碼。

    此設定會將虛擬網路 DNS 尾碼的所有 DNS 要求都路由傳送至 Azure 遞迴解析程式。

  3. 在虛擬網路的自訂 DNS 伺服器上,使用下列文字作為 /etc/bind/named.conf.options 檔案的內容:

    // Clients to accept requests from
    // TODO: Add the IP range of the joined network to this list
    acl goodclients {
        10.0.0.0/16; # IP address range of the virtual network
        localhost;
        localnets;
    };
    
    options {
            directory "/var/cache/bind";
    
            recursion yes;
    
            allow-query { goodclients; };
    
            # All other requests are sent to the following
            forwarders {
                192.168.0.1; # Replace with the IP address of your on-premises DNS server
            };
    
            dnssec-validation auto;
    
            auth-nxdomain no;    # conform to RFC1035
            listen-on { any; };
    };
    
    • 10.0.0.0/16 值取代為虛擬網路的 IP 位址範圍。 此項目允許來自此範圍內位址的名稱解析要求。

    • 將內部部署網路的 IP 位址範圍新增至 acl goodclients { ... } 區段。 項目允許來自內部部署網路中資源的名稱解析要求。

    • 192.168.0.1 值取代為內部部署 DNS 伺服器的 IP 位址。 此項目會將所有其他 DNS 要求路由傳送至內部部署 DNS 伺服器。

  4. 若要使用設定,請重新啟動 Bind。 例如: sudo service bind9 restart

  5. 將條件式轉寄站新增至內部部署 DNS 伺服器。 設定條件式轉寄站,以將步驟 1 中之 DNS 尾碼的要求傳送至自訂 DNS 伺服器。

    注意

    如需如何新增條件式轉寄站的詳細資訊,請參閱 DNS 軟體的文件。

完成這些步驟之後,您可以使用完整網域名稱 (FQDN) 連線至任一虛擬網路中的資源。 您現在可以將 HDInsight 安裝至虛擬網路。

兩個已連線虛擬網路之間的名稱解析

此範例做出以下假設:

  • 您的兩個 Azure 虛擬網路是使用 VPN 閘道或對等進行連線。

  • 兩個網路中的自訂 DNS 伺服器會執行 Linux 或 Unix 作為作業系統。

  • Bind 安裝在自訂 DNS 伺服器上。

  1. 若要尋找這兩個虛擬網路的 DNS 尾碼,請使用 Azure PowerShell 或 Azure CLI:

    RESOURCEGROUP 取代為包含虛擬網路的資源群組名稱,然後輸入下列命令:

    $NICs = Get-AzNetworkInterface -ResourceGroupName "RESOURCEGROUP"
    $NICs[0].DnsSettings.InternalDomainNameSuffix
    
    az network nic list --resource-group RESOURCEGROUP --query "[0].dnsSettings.internalDomainNameSuffix"
    
  2. 使用下列文字作為自訂 DNS 伺服器上 /etc/bind/named.config.local 檔案的內容。 在這兩個虛擬網路的自訂 DNS 伺服器上進行這項變更。

    // Forward requests for the virtual network suffix to Azure recursive resolver
    zone "0owcbllr5hze3hxdja3mqlrhhe.ex.internal.cloudapp.net" {
        type forward;
        forwarders {10.0.0.4;}; # The IP address of the DNS server in the other virtual network
    };
    

    0owcbllr5hze3hxdja3mqlrhhe.ex.internal.cloudapp.net 值取代為「其他」虛擬網路的 DNS 尾碼。 此項目會將遠端網路 DNS 尾碼的要求路由傳送至該網路中的自訂 DNS。

  3. 在這兩個虛擬網路的自訂 DNS 伺服器上,使用下列文字作為 /etc/bind/named.conf.options 檔案的內容:

    // Clients to accept requests from
    acl goodclients {
        10.1.0.0/16; # The IP address range of one virtual network
        10.0.0.0/16; # The IP address range of the other virtual network
        localhost;
        localnets;
    };
    
    options {
            directory "/var/cache/bind";
    
            recursion yes;
    
            allow-query { goodclients; };
    
            forwarders {
            168.63.129.16;   # Azure recursive resolver
            };
    
            dnssec-validation auto;
    
            auth-nxdomain no;    # conform to RFC1035
            listen-on { any; };
    };
    

    10.0.0.0/1610.1.0.0/16 值取代為虛擬網路的 IP 位址範圍。 此項目允許每個網路中的資源對 DNS 伺服器提出要求。

    不適用於虛擬網路 DNS 尾碼 (例如,microsoft.com) 的任何要求都會由 Azure 遞迴解析程式來處理。

  4. 若要使用設定,請重新啟動 Bind。 例如,兩部 DNS 伺服器上的 sudo service bind9 restart

完成這些步驟之後,您可以使用完整網域名稱 (FQDN) 連線至虛擬網路中的資源。 您現在可以將 HDInsight 安裝至虛擬網路。

部署 HDInsight 叢集之前先測試您的設定

部署叢集之前,您可以透過在與規劃叢集相同的 VNet 和子網路中的 Azure Linux 虛擬機器上執行 HDInsight 網路驗證程式工具 (英文),來檢查您的許多網路組態設定是否正確。

下一步