快速入門:使用 Azure PowerShell 建立虛擬機擴展集

適用於: ✔️ Linux VM ✔️ Windows VM ✔️ 統一擴展集

注意

下列文章適用於統一 虛擬機器擴展集。 建議您將彈性虛擬機器擴展集用於新的工作負載。 在我們的彈性虛擬機器擴展集概觀中,深入了解這個新的協調流程模式。

虛擬機擴展集可讓您部署和管理一組自動調整虛擬機。 您可以手動調整擴展集中的 VM 數目,或根據 CPU、記憶體需求或網路流量等資源使用量來定義自動調整的規則。 然後,Azure 負載平衡器會將流量分散到擴展集中的 VM 實例。 在本快速入門中,您會建立虛擬機擴展集,並使用 Azure PowerShell 部署範例應用程式。

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

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 鍵執行程式碼或命令。

建立擴展集

您必須先使用 New-AzResourceGroup 建立資源群組,才能建立擴展集。 下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組:

New-AzResourceGroup -ResourceGroupName "myResourceGroup" -Location "EastUS"

現在使用 New-AzVmss 建立虛擬機器擴展集。 下列範例會建立名為 myScaleSet 的擴展集,其使用 Windows Server 2016 Datacenter 平臺映射。 會自動建立虛擬網路、公用IP位址和負載平衡器的 Azure 網路資源。 出現提示時,您可以為擴展集中的 VM 執行個體設定自己的系統管理認證:

重要

自 2023 年 11 月起,如果未指定協調流程模式,則使用 PowerShell 和 Azure CLI 建立的 VM 擴展集會預設為彈性協調流程模式。 如需此變更的詳細資訊,以及您應該採取的動作,請移至 針對 VMSS PowerShell/CLI 客戶的中斷性變更 - Microsoft 社群中樞 (英文)

New-AzVmss `
  -ResourceGroupName "myResourceGroup" `
  -Location "EastUS" `
  -VMScaleSetName "myScaleSet" `
  -VirtualNetworkName "myVnet" `
  -SubnetName "mySubnet" `
  -PublicIpAddressName "myPublicIPAddress" `
  -LoadBalancerName "myLoadBalancer" `
  -OrchestrationMode 'Uniform' `
  -UpgradePolicyMode "Automatic"

建立及設定所有擴展集資源和 VM 需要幾分鐘的時間。

部署範例應用程式

若要測試擴展集,請安裝基本的 Web 應用程式。 Azure 自定義腳本擴充功能可用來下載和執行在 VM 實例上安裝 IIS 的腳本。 此擴充功能適用於部署後組態、軟體安裝或其他任何組態/管理工作。 如需詳細資訊,請參閱自訂指令碼延伸模組概觀

使用自定義腳本擴充功能來安裝基本的 IIS 網頁伺服器。 套用安裝 IIS 的自定義腳本擴充功能,如下所示:

# Define the script for your Custom Script Extension to run
$publicSettings = @{
    "fileUris" = (,"https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/automate-iis.ps1");
    "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"
}

# Get information about the scale set
$vmss = Get-AzVmss `
            -ResourceGroupName "myResourceGroup" `
            -VMScaleSetName "myScaleSet"

# Use Custom Script Extension to install IIS and configure basic website
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
    -Name "customScript" `
    -Publisher "Microsoft.Compute" `
    -Type "CustomScriptExtension" `
    -TypeHandlerVersion 1.8 `
    -Setting $publicSettings

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
    -ResourceGroupName "myResourceGroup" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

允許流量流向應用程式

若要允許存取基本 Web 應用程式,請使用 New-AzNetworkSecurityRuleConfigNew-AzNetworkSecurityGroup 建立網路安全性群組。 如需詳細資訊,請參閱 Azure 虛擬機器擴展集的網路功能

# Get information about the scale set
$vmss = Get-AzVmss `
            -ResourceGroupName "myResourceGroup" `
            -VMScaleSetName "myScaleSet"

#Create a rule to allow traffic over port 80
$nsgFrontendRule = New-AzNetworkSecurityRuleConfig `
  -Name myFrontendNSGRule `
  -Protocol Tcp `
  -Direction Inbound `
  -Priority 200 `
  -SourceAddressPrefix * `
  -SourcePortRange * `
  -DestinationAddressPrefix * `
  -DestinationPortRange 80 `
  -Access Allow

#Create a network security group and associate it with the rule
$nsgFrontend = New-AzNetworkSecurityGroup `
  -ResourceGroupName  "myResourceGroup" `
  -Location EastUS `
  -Name myFrontendNSG `
  -SecurityRules $nsgFrontendRule

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName  "myResourceGroup" `
  -Name myVnet

$frontendSubnet = $vnet.Subnets[0]

$frontendSubnetConfig = Set-AzVirtualNetworkSubnetConfig `
  -VirtualNetwork $vnet `
  -Name mySubnet `
  -AddressPrefix $frontendSubnet.AddressPrefix `
  -NetworkSecurityGroup $nsgFrontend

Set-AzVirtualNetwork -VirtualNetwork $vnet

# Update the scale set and apply the Custom Script Extension to the VM instances
Update-AzVmss `
    -ResourceGroupName "myResourceGroup" `
    -Name "myScaleSet" `
    -VirtualMachineScaleSet $vmss

測試您的擴展集

若要查看您的擴充集運作情形,請在網頁瀏覽器中存取範例 Web 應用程式。 使用 Get-AzPublicIpAddress 取得負載平衡器的公用 IP 位址。 下列範例會顯示 myResourceGroup 資源群組中建立的 IP 位址:

Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select IpAddress

將負載平衡器的公用 IP 位址輸入網頁瀏覽器中。 負載平衡器會將流量散發至您的其中一個 VM 執行個體,如下列範例所示:

Running IIS site

清除資源

不再需要時,您可以使用 Remove-AzResourceGroup 來移除資源群組、擴展集和所有相關資源,如下所示。 -Force 參數會確認您想要刪除資源,而不另外對您提示將要進行此作業。 -AsJob 參數不會等待作業完成,就會將控制項傳回給提示字元。

Remove-AzResourceGroup -Name "myResourceGroup" -Force -AsJob

下一步

在本快速入門中,您已建立基本擴展集,並使用自定義腳本擴充功能在 VM 實例上安裝基本的 IIS 網頁伺服器。 若要深入瞭解,請繼續進行如何建立和管理 Azure 虛擬機器擴展集 的教學課程。