利用虛擬機規(guī)模集,可以部署和管理一組相同的稀并、自動縮放的虛擬機冲九。 可以手動縮放規(guī)模集中的 VM 數荧关,也可以定義規(guī)則,以便根據資源使用情況(如 CPU 使用率格仲、內存需求或網絡流量)進行自動縮放搁廓。 在本教程中引颈,將在 Azure 中部署虛擬機規(guī)模集。 你將學習如何執(zhí)行以下操作:
使用自定義腳本擴展定義要縮放的 IIS 站點
為規(guī)模集創(chuàng)建負載均衡器
創(chuàng)建虛擬機規(guī)模集
增加或減少規(guī)模集中的實例數
創(chuàng)建自動縮放規(guī)則
本教程需要 Azure PowerShell 模塊 3.6 或更高版本境蜕。 運行Get-Module -ListAvailable AzureRM即可查找版本蝙场。 如果需要升級,請參閱安裝 Azure PowerShell 模塊粱年。
規(guī)模集概述
利用虛擬機規(guī)模集售滤,可以部署和管理一組相同的、自動縮放的虛擬機逼泣。 規(guī)模集中的 VM 將分布在邏輯容錯域和更新域的一個或多個放置組中趴泌。 這些放置組由配置類似的 VM 組成,與可用性集相似拉庶。
可以根據需要在規(guī)模集中創(chuàng)建 VM嗜憔。 可以定義自動縮放規(guī)則來控制如何以及何時在規(guī)模集中添加或刪除 VM。 這些規(guī)則可以根據 CPU 負載氏仗、內存用量或網絡流量等指標觸發(fā)吉捶。
使用 Azure 平臺映像時夺鲜,規(guī)模集最多支持 1,000 個 VM。 對于有重要安裝或 VM 自定義要求的工作負荷呐舔,可能需要創(chuàng)建自定義 VM 映像币励。 使用自定義映像時,在規(guī)模集中最多可以創(chuàng)建 300 個 VM珊拼。
創(chuàng)建用于縮放的應用
創(chuàng)建規(guī)模集之前食呻,需使用New-AzureRmResourceGroup創(chuàng)建一個資源組。 以下示例在 ChinaEast 位置創(chuàng)建一個名為 myResourceGroupAutomate 的資源組:
PowerShell復制
New-AzureRmResourceGroup-ResourceGroupNamemyResourceGroupScaleSet-LocationChinaEast
在前面的教程中澎现,你已了解如何使用自定義腳本擴展來自動執(zhí)行 VM 配置仅胞。 創(chuàng)建一個規(guī)模集配置,然后應用自定義腳本擴展來安裝并配置 IIS:
PowerShell復制
# Create a config object$vmssConfig=New-AzureRmVmssConfig`-LocationChinaEast `-SkuCapacity2`-SkuNameStandard_DS2 `-UpgradePolicyModeAutomatic# Define the script for your Custom Script Extension to run$publicSettings= @{"fileUris"= (,"https://raw.githubusercontent.com/iainfoulds/azure-samples/master/automate-iis.ps1");"commandToExecute"="powershell -ExecutionPolicy Unrestricted -File automate-iis.ps1"}# Use Custom Script Extension to install IIS and configure basic websiteAdd-AzureRmVmssExtension-VirtualMachineScaleSet$vmssConfig`-Name"customScript"`-Publisher"Microsoft.Compute"`-Type"CustomScriptExtension"`-TypeHandlerVersion1.8`-Setting$publicSettings
創(chuàng)建規(guī)模負載均衡器
Azure 負載均衡器是位于第 4 層(TCP剑辫、UDP)的負載均衡器干旧,通過在正常運行的 VM 之間分發(fā)傳入流量提供高可用性。 負載均衡器運行狀況探測器監(jiān)視每個 VM 上的給定端口妹蔽,并僅將流量分發(fā)給正常運行的 VM椎眯。 有關詳細信息,請參閱有關如何對 Windows 虛擬機進行負載均衡的下一個教程胳岂。
創(chuàng)建一個具有公共 IP 地址编整、對端口 80 上的 Web 流量進行分發(fā)的負載均衡器:
PowerShell復制
# Create a public IP address$publicIP=New-AzureRmPublicIpAddress`-ResourceGroupNamemyResourceGroupScaleSet `-LocationChinaEast `-AllocationMethodStatic `-NamemyPublicIP# Create a frontend and backend IP pool$frontendIP=New-AzureRmLoadBalancerFrontendIpConfig`-NamemyFrontEndPool `-PublicIpAddress$publicIP$backendPool=New-AzureRmLoadBalancerBackendAddressPoolConfig-NamemyBackEndPool# Create the load balancer$lb=New-AzureRmLoadBalancer`-ResourceGroupNamemyResourceGroupScaleSet `-NamemyLoadBalancer `-LocationChinaEast `-FrontendIpConfiguration$frontendIP`-BackendAddressPool$backendPool# Create a load balancer health probe on port 80Add-AzureRmLoadBalancerProbeConfig-NamemyHealthProbe `-LoadBalancer$lb`-Protocoltcp `-Port80`-IntervalInSeconds15`-ProbeCount2# Create a load balancer rule to distribute traffic on port 80Add-AzureRmLoadBalancerRuleConfig`-NamemyLoadBalancerRule `-LoadBalancer$lb`-FrontendIpConfiguration$lb.FrontendIpConfigurations[0] `-BackendAddressPool$lb.BackendAddressPools[0] `-ProtocolTcp `-FrontendPort80`-BackendPort80# Update the load balancer configurationSet-AzureRmLoadBalancer-LoadBalancer$lb
創(chuàng)建規(guī)模集
現在,使用New-AzureRmVmss創(chuàng)建一個虛擬機規(guī)模集旦万。 以下示例創(chuàng)建一個名為 myScaleSet 的規(guī)模集:
PowerShell復制
# Reference a virtual machine image from the gallerySet-AzureRmVmssStorageProfile$vmssConfig`-ImageReferencePublisherMicrosoftWindowsServer `-ImageReferenceOfferWindowsServer `-ImageReferenceSku2016-Datacenter `-ImageReferenceVersionlatest# Set up information for authenticating with the virtual machineSet-AzureRmVmssOsProfile$vmssConfig`-AdminUsernameazureuser `-AdminPasswordP@ssword! `-ComputerNamePrefixmyVM# Create the virtual network resources$subnet=New-AzureRmVirtualNetworkSubnetConfig`-Name"mySubnet"`-AddressPrefix10.0.0.0/24$vnet=New-AzureRmVirtualNetwork`-ResourceGroupName"myResourceGroupScaleSet"`-Name"myVnet"`-Location"ChinaEast"`-AddressPrefix10.0.0.0/16`-Subnet$subnet$ipConfig=New-AzureRmVmssIpConfig`-Name"myIPConfig"`-LoadBalancerBackendAddressPoolsId$lb.BackendAddressPools[0].Id `-SubnetId$vnet.Subnets[0].Id# Attach the virtual network to the config objectAdd-AzureRmVmssNetworkInterfaceConfiguration`-VirtualMachineScaleSet$vmssConfig`-Name"network-config"`-Primary$true`-IPConfiguration$ipConfig# Create the scale set with the config object (this step might take a few minutes)New-AzureRmVmss`-ResourceGroupNamemyResourceGroupScaleSet `-NamemyScaleSet `-VirtualMachineScaleSet$vmssConfig
創(chuàng)建和配置所有的規(guī)模集資源和 VM 需要幾分鐘時間闹击。
測試應用
若要查看 IIS 網站的實際運行情況,請使用Get-AzureRmPublicIPAddress獲取負載均衡器的公共 IP 地址成艘。 以下示例獲取作為規(guī)模集的一部分創(chuàng)建的 myPublicIP 的 IP 地址:
PowerShell復制
Get-AzureRmPublicIPAddress-ResourceGroupNamemyResourceGroupScaleSet-NamemyPublicIP | select IpAddress
將公共 IP 地址輸入到 Web 瀏覽器中赏半。 將顯示應用,包括負載均衡器將流量分發(fā)到的 VM 的主機名:
若要查看規(guī)模集的實際運行情況淆两,可以強制刷新 Web 瀏覽器断箫,以查看負載均衡器如何在運行應用的所有 VM 之間分發(fā)流量。
管理任務
在規(guī)模集的整個生命周期內秋冰,可能需要運行一個或多個管理任務仲义。 此外,可能還需要創(chuàng)建自動執(zhí)行各種生命周期任務的腳本剑勾。 Azure PowerShell 提供了一種用于執(zhí)行這些任務的快速方法埃撵。 以下是一些常見任務。
查看規(guī)模集中的 VM
若要查看規(guī)模集中運行的 VM 的列表虽另,請使用Get-AzureRmVmssVM暂刘,如下所示:
PowerShell復制
# Get current scale set$scaleset=Get-AzureRmVmss`-ResourceGroupNamemyResourceGroupScaleSet `-VMScaleSetNamemyScaleSet# Loop through the instanaces in your scale setfor($i=1;$i-le($scaleset.Sku.Capacity-1);$i++) {Get-AzureRmVmssVM-ResourceGroupNamemyResourceGroupScaleSet `-VMScaleSetNamemyScaleSet `-InstanceId$i}
增加或減少 VM 實例
若要查看規(guī)模集中當前包含的實例數,請使用Get-AzureRmVmss并查詢 sku.capacity:
PowerShell復制
Get-AzureRmVmss-ResourceGroupNamemyResourceGroupScaleSet `-VMScaleSetNamemyScaleSet | `? ? Select-ExpandPropertySku
然后捂刺,可以使用Update-AzureRmVmss手動增加或減少規(guī)模集中虛擬機的數目谣拣。 以下示例將規(guī)模集中 VM 的數目設置為 5:
PowerShell復制
# Get current scale set$scaleset=Get-AzureRmVmss`-ResourceGroupNamemyResourceGroupScaleSet `-VMScaleSetNamemyScaleSet# Set and update the capacity of your scale set$scaleset.sku.capacity =5Update-AzureRmVmss-ResourceGroupNamemyResourceGroupScaleSet `-NamemyScaleSet `-VirtualMachineScaleSet$scaleset
這將花費數分鐘來更新規(guī)模集中指定數目的實例募寨。
配置自動縮放規(guī)則
你可以定義自動縮放規(guī)則,而不是手動縮放規(guī)模集中實例的數目森缠。 這些規(guī)則監(jiān)視規(guī)模集中的實例拔鹰,并根據所定義的指標和閾值做出相應響應。 如果平均 CPU 負載高于 60% 且持續(xù)時間超過 5 分鐘贵涵,以下示例將增加一個實例列肢。 如果平均 CPU 負載低于 30% 且持續(xù)時間超過 5 分鐘,則將減少一個實例:
PowerShell復制
# Define your scale set information$mySubscriptionId= (Get-AzureRmSubscription).Id$myResourceGroup="myResourceGroupScaleSet"$myScaleSet="myScaleSet"$myLocation="China North"# Create a scale up rule to increase the number instances after 60% average CPU usage exceeded for a 5 minute period$myRuleScaleUp=New-AzureRmAutoscaleRule`-MetricName"Percentage CPU"`-MetricResourceId/subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet`-OperatorGreaterThan `-MetricStatisticAverage `-Threshold60`-TimeGrain00:01:00`-TimeWindow00:05:00`-ScaleActionCooldown00:05:00`-ScaleActionDirectionIncrease `-ScaleActionValue1# Create a scale down rule to decrease the number of instances after 30% average CPU usage over a 5 minute period$myRuleScaleDown=New-AzureRmAutoscaleRule`-MetricName"Percentage CPU"`-MetricResourceId/subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet`-OperatorLessThan `-MetricStatisticAverage `-Threshold30`-TimeGrain00:01:00`-TimeWindow00:05:00`-ScaleActionCooldown00:05:00`-ScaleActionDirectionDecrease `-ScaleActionValue1# Create a scale profile with your scale up and scale down rules$myScaleProfile=New-AzureRmAutoscaleProfile`-DefaultCapacity2`-MaximumCapacity10`-MinimumCapacity2`-Rules$myRuleScaleUp,$myRuleScaleDown`-Name"autoprofile"# Apply the autoscale rulesAdd-AzureRmAutoscaleSetting`-Location$myLocation`-Name"autosetting"`-ResourceGroup$myResourceGroup`-TargetResourceId/subscriptions/$mySubscriptionId/resourceGroups/$myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/$myScaleSet`-AutoscaleProfiles$myScaleProfile
后續(xù)步驟
在本教程中独悴,你已創(chuàng)建了一個虛擬機規(guī)模集例书。 你已學習了如何執(zhí)行以下操作:
使用自定義腳本擴展定義要縮放的 IIS 站點
為規(guī)模集創(chuàng)建負載均衡器
創(chuàng)建虛擬機規(guī)模集
增加或減少規(guī)模集中的實例數
創(chuàng)建自動縮放規(guī)則
請繼續(xù)學習下一教程锣尉,詳細了解虛擬機的負載均衡概念刻炒。
立即訪問http://market.azure.cn