教學課程:使用 Azure PowerShell 建立及使用虛擬機器擴展集的自訂映像

當您建立擴展集時,您會指定部署 VM 執行個體時所要使用的映像。 若要減少部署 VM 執行個體後的工作數量,您可以使用自訂的 VM 映像。 此自訂 VM 映像包括任何必要的應用程式安裝或組態。 在擴展集中建立的任何 VM 執行個體都會使用自訂 VM 映像,並已可以處理您的應用程式流量。 在本教學課程中,您將了解如何:

  • 建立 Azure Compute Gallery
  • 建立映像定義
  • 建立映像版本
  • 從映像建立擴展集
  • 共用映像庫

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

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com/powershell ,從另一個瀏覽器索引標籤啟動 Cloud Shell。 選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 enter 鍵加以執行。

建立並設定來源 VM

首先,使用 New-AzResourceGroup 建立資源群組,然後使用 New-AzVM 建立 VM。 接著,此 VM 會用來當作映像的來源。 下列範例會在名為 myResourceGroup 的資源群組中建立名為 myVM 的 VM:

New-AzResourceGroup -Name 'myResourceGroup' -Location 'EastUS'

New-AzVm `
   -ResourceGroupName 'myResourceGroup' `
   -Name 'myVM' `
   -Location 'East US' `
   -VirtualNetworkName 'myVnet' `
   -SubnetName 'mySubnet' `
   -SecurityGroupName 'myNetworkSecurityGroup' `
   -PublicIpAddressName 'myPublicIpAddress' `
   -OpenPorts 80,3389

儲存 VM 變數

您可以使用 Get-AzVM 來查看資源群組中的可用 VM 清單。 當您知道 VM 名稱和資源群組之後,您可以再次使用 Get-AzVM 來取得該 VM 物件,並將其儲存在變數中以供日後使用。 此範例會從 "myResourceGroup" 資源群組取得名為 myVM 的 VM,然後將其指派至 $vm 變數。

$sourceVM = Get-AzVM `
   -Name myVM `
   -ResourceGroupName myResourceGroup

映像資源庫是用於啟用映像共用的主要資源。 資源庫名稱允許的字元為大寫或小寫字母、數字、點和句點。 資源庫名稱不能包含連字號。 資源庫名稱在您的訂用帳戶內必須是唯一的。

使用 New-AzGallery 建立映像資源庫。 下列範例會在 myGalleryRG 資源群組中建立名為 myGallery 的資源庫。

$resourceGroup = New-AzResourceGroup `
   -Name 'myGalleryRG' `
   -Location 'EastUS'

$gallery = New-AzGallery `
   -GalleryName 'myGallery' `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -Description 'Azure Compute Gallery for my organization'	

建立映像定義

映像定義會建立映像的邏輯群組。 並且可用來管理在其中建立的映像版本相關資訊。 映像定義名稱可以由大寫或小寫字母、數字、點、虛線和句點組成。 若要深入了解您可以為映像定義指定哪些值,請參閱映像定義

使用 New-AzGalleryImageDefinition 建立映像定義。 在此範例中,資源庫映像會命名為 myGalleryImage,並為特製化映像而建立。

$galleryImage = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'myImageDefinition' `
   -OsState specialized `
   -OsType Windows `
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU'

建立映像版本

使用 New-AzGalleryImageVersion 從 VM 建立映像版本。

映像版本允許的字元是數字及句點。 數字必須在 32 位元整數的範圍內。 格式:MajorVersion.MinorVersion.Patch

在此範例中,映像版本為 1.0.0,並且會複寫到「美國東部」和「美國中南部」資料中心。 選擇要複寫的目的地區域時,您必須包含作為複寫目標的「來源」區域。

若要從 VM 建立映像版本,請使用 $vm.Id.ToString() 作為 -Source

$region1 = @{Name='South Central US';ReplicaCount=1}
$region2 = @{Name='East US';ReplicaCount=2}
$targetRegions = @($region1,$region2)

New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $galleryImage.Name`
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $resourceGroup.ResourceGroupName `
   -Location $resourceGroup.Location `
   -TargetRegion $targetRegions  `
   -Source $sourceVM.Id.ToString() `
   -PublishingProfileEndOfLifeDate '2023-12-01'

將映像複寫到所有目的地區域可能需要一些時間。

從映像建立擴展集

現在,使用 New-AzVmss 建立擴展集,以使用 -ImageName 參數來定義前一個步驟中建立的自訂 VM 映像。 為了將流量散發到個別的虛擬機器執行個體,也會建立負載平衡器。 負載平衡器包含在 TCP 連接埠 80 上分配流量的規則,同時允許 TCP 連接埠 3389 上的遠端桌面流量以及 TCP 連接埠 5985 上的 PowerShell 遠端處理。 出現提示時,請為擴展集中的 VM 執行個體提供適當的系統管理認證:

重要

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

# Define variables for the scale set
$resourceGroupName = "myScaleSet"
$scaleSetName = "myScaleSet"
$location = "East US"

# Create a resource group
New-AzResourceGroup -ResourceGroupName $resourceGroupName -Location $location

# Create a configuration 
$vmssConfig = New-AzVmssConfig `
   -Location $location `
   -OrchestrationMode Flexible `
   -SkuCapacity 2 `
   -SkuName "Standard_D2s_v3"

# Reference the image version
Set-AzVmssStorageProfile $vmssConfig `
  -OsDiskCreateOption "FromImage" `
  -ImageReferenceId $galleryImage.Id

# Create the scale set 
New-AzVmss `
  -ResourceGroupName $resourceGroupName `
  -Name $scaleSetName `
  -VirtualMachineScaleSet $vmssConfig

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

我們建議您在映像庫層級上共用存取權。 使用電子郵件地址和 Get-AzADUser Cmdlet 來取得使用者的物件識別碼,然後使用 New-AzRoleAssignment 來提供資源庫的存取權。 以您自己的資訊取代範例電子郵件 (在此範例中為 alinne_montes@contoso.com)。

# Get the object ID for the user
$user = Get-AzADUser -StartsWith alinne_montes@contoso.com
# Grant access to the user for our gallery
New-AzRoleAssignment `
   -ObjectId $user.Id `
   -RoleDefinitionName Reader `
   -ResourceName $gallery.Name `
   -ResourceType Microsoft.Compute/galleries `
   -ResourceGroupName $resourceGroup.ResourceGroupName

清除資源

當不再需要時,您可以使用 Remove-AzResourceGroup 命令來移除資源群組及所有相關資源:

# Delete the gallery 
Remove-AzResourceGroup -Name myGalleryRG

# Delete the scale set resource group
Remove-AzResourceGroup -Name myResoureceGroup

下一步

在本教學課程中,您已了解如何使用 Azure PowerShell 來建立及使用擴展集的自訂 VM 映像:

  • 建立 Azure Compute Gallery
  • 建立映像定義
  • 建立映像版本
  • 從映像建立擴展集
  • 共用映像庫

前往下一個教學課程,以了解如何將應用程式部署至擴展集。