當您建立擴展集時,您會指定部署 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 Cmdlet 來移除資源群組和所有相關資源:
# Delete the gallery
Remove-AzResourceGroup -Name myGalleryRG
# Delete the scale set resource group
Remove-AzResourceGroup -Name myResoureceGroup
後續步驟
在本教學課程中,您已瞭解如何使用 Azure PowerShell 為擴展集建立和使用自訂 VM 映像:
- 建立 Azure Compute Gallery
- 建立映像定義
- 建立映像版本
- 從映像建立擴展集
- 共用映像庫
前往下一個教學課程,以了解如何將應用程式部署至擴展集。