建立虛擬機器 (VM) 後,您可以透過變更 VM 的大小來放大或縮小 VM。 在某些情況下,您必須先解除配置 VM。 如果目前裝載 VM 的相同硬體叢集無法提供新的大小,就可能需要解除配置。 請務必瞭解,即使不需要解除分配,如果虛擬機目前正在執行,變更其大小會導致重新啟動。 基於這個理由,您應該考慮將 VM 大小變更為干擾性程式,特別是針對裝載於 VM 上的具狀態工作負載。
如果您的 VM 使用進階儲存體,請確實選擇 s 版本的大小,以取得進階儲存體支援。 例如,請選擇 Standard_E4s_v3,而不是 Standard_E4_v3。
# Set variables$resourceGroup = 'myResourceGroup'$vmName = 'myVM'$size = 'Standard_DS3_v2'# Get the VM$vm = Get-AzVM -ResourceGroupName$resourceGroup -Name$vmName# Change the VM size$vm.HardwareProfile.VmSize = $size# Update the VMUpdate-AzVM -ResourceGroupName$resourceGroup -VM$vm
# Import the Azure moduleImport-Module Az
# Login to your Azure accountConnect-AzAccount# Set variables$resourceGroup = 'myResourceGroup'$vmName = 'myVM'$size = 'Standard_DS3_v2'# Select the subscriptionSelect-AzSubscription -SubscriptionId'<subscriptionID>'# Get the VM$vm = Get-AzVM -ResourceGroupName$resourceGroup -Name$vmName# Change the VM size$vm.HardwareProfile.VmSize = $size# Update the VMUpdate-AzVM -ResourceGroupName$resourceGroup -VM$vm
如果目前裝載 VM 的硬體叢集無法提供可用性設定組中 VM 的新大小,則您必須解除配置可用性設定組中所有的 VM,才能調整 VM 大小。 在已調整某個 VM 的大小後,您也可能需要更新可用性設定組中其他 VM 的大小。 若要調整可用性設定組中 VM 的大小,請執行下列指令碼。 您可以將 $resourceGroup、$vmName、$newVmSize 和 $availabilitySetName 的值取代為您自己的值。
Azure PowerShell
# Set variables$resourceGroup = "myResourceGroup"$vmName = "myVM"$newVmSize = "<newVmSize>"$availabilitySetName = "<availabilitySetName>"# Check if the desired VM size is available$availableSizes = Get-AzVMSize `
-ResourceGroupName$resourceGroup `
-VMName$vmName |
Select-Object -ExpandProperty Name
if ($availableSizes -notcontains$newVmSize) {
# Deallocate all VMs in the availability set$as = Get-AzAvailabilitySet `
-ResourceGroupName$resourceGroup `
-Name$availabilitySetName$virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM$virtualMachines | Stop-AzVM -Force -NoWait# Resize and restart the VMs in the availability set$virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newVmSize }
$virtualMachines | Update-AzVM$virtualMachines | Start-AzVMexit
}
# Resize the VM$vm = Get-AzVM `
-ResourceGroupName$resourceGroup `
-VMName$vmName$vm.HardwareProfile.VmSize = $newVmSizeUpdate-AzVM `
-VM$vm `
-ResourceGroupName$resourceGroup
此指令碼設定變數 $resourceGroup、$vmName、$newVmSize 和 $availabilitySetName。 然後,使用 Get-AzVMSize 檢查是否可以提供所需的 VM 大小,以及檢查輸出是否包含所需的大小。 如果無法提供所需的大小,指令碼會解除配置可用性設定組中的所有 VM、調整其大小,然後重新加以啟動。 如果有所需的大小可用,指令碼會調整 VM 的大小。
下列指令碼會在重設大小之前檢查是否可以提供所需的 VM 大小。 如果無法提供所需的大小,指令碼會結束並顯示錯誤訊息。 如果可以提供所需的大小,指令碼會解除配置 VM、調整其大小,然後重新加以啟動。 您可以將 resourceGroup、vm、和 size 的值取代為您自己的值。
Azure CLI
# Set variablesresourceGroup=myResourceGroup
vm=myVM
size=Standard_DS3_v2
# Check if the desired VM size is available
if ! az vm list-vm-resize-options--resource-group$resourceGroup--name$vm--query"[].name" | grep -q$size; then
echo "The desired VM size is not available."
exit 1
fi
# Deallocate the VMaz vm deallocate --resource-group$resourceGroup--name$vm# Resize the VMaz vm resize --resource-group$resourceGroup--name$vm--size$size# Start the VMaz vm start --resource-group$resourceGroup--name$vm
以下指令碼設定變數 resourceGroup、vm 和 size。 然後,使用 az vm list-vm-resize-options 檢查是否可以提供所需的 VM 大小,以及檢查輸出是否包含所需的大小。 如果無法提供所需的大小,指令碼會結束並顯示錯誤訊息。 如果所需的大小可用,指令碼會解除配置 VM、調整其大小,然後重新加以啟動。
Azure CLI
# Set variablesresourceGroup="myResourceGroup"vmName="myVM"
newVmSize="<newVmSize>"
availabilitySetName="<availabilitySetName>"# Check if the desired VM size is availableavailableSizes=$(az vm list-vm-resize-options \
--resource-group$resourceGroup \
--name$vmName \
--query"[].name" \
--output tsv)
if [[ ! $availableSizes =~ $newVmSize]]; then
# Deallocate all VMs in the availability setvmIds=$(az vmss list-instances \
--resource-group$resourceGroup \
--name$availabilitySetName \
--query"[].instanceId" \
--output tsv)
az vm deallocate \
--ids$vmIds \
--no-wait# Resize and restart the VMs in the availability set
az vmss update \
--resource-group$resourceGroup \
--name$availabilitySetName \
--set virtualMachineProfile.hardwareProfile.vmSize=$newVmSize
az vmss start \
--resource-group$resourceGroup \
--name$availabilitySetName \
--instance-ids$vmIds
exit
fi
# Resize the VMaz vm resize \
--resource-group$resourceGroup \
--name$vmName \
--size$newVmSize
若要在 Terraform 程式碼中調整 VM 的大小,請將 azurerm_linux_virtual_machine 或 azurerm_windows_virtual_machine 資源區塊中的 size 參數修改為所需的大小,然後執行 terraform plan -out main.tfplan,以查看將進行的 VM 大小變更。 然後,執行 terraform apply main.tfplan 套用變更以調整 VM 的大小。
重要
下列 Terraform 範例會在您使用建立原始虛擬機器的狀態檔案時,修改現有虛擬機器的大小。 如需完整的 Terraform 程式碼,請參閱 Windows Terraform 快速入門。