在 Windows 上創(chuàng)建虛擬機(jī)規(guī)模集和部署高度可用的應(yīng)用

利用虛擬機(jī)規(guī)模集,可以部署和管理一組相同的、自動(dòng)縮放的虛擬機(jī)。 可以手動(dòng)縮放規(guī)模集中的 VM 數(shù),也可以定義規(guī)則,以便根據(jù)資源使用情況(如 CPU 使用率、內(nèi)存需求或網(wǎng)絡(luò)流量)進(jìn)行自動(dòng)縮放。 在本教程中,將在 Azure 中部署虛擬機(jī)規(guī)模集。 你將學(xué)習(xí)如何執(zhí)行以下操作:

使用自定義腳本擴(kuò)展定義要縮放的 IIS 站點(diǎn)

為規(guī)模集創(chuàng)建負(fù)載均衡器

創(chuàng)建虛擬機(jī)規(guī)模集

增加或減少規(guī)模集中的實(shí)例數(shù)

創(chuàng)建自動(dòng)縮放規(guī)則

本教程需要 Azure PowerShell 模塊 3.6 或更高版本。 運(yùn)行Get-Module -ListAvailable AzureRM即可查找版本。 如果需要升級(jí),請(qǐng)參閱安裝 Azure PowerShell 模塊。

規(guī)模集概述

利用虛擬機(jī)規(guī)模集,可以部署和管理一組相同的、自動(dòng)縮放的虛擬機(jī)。 規(guī)模集中的 VM 將分布在邏輯容錯(cuò)域和更新域的一個(gè)或多個(gè)放置組中。 這些放置組由配置類似的 VM 組成,與可用性集相似。

可以根據(jù)需要在規(guī)模集中創(chuàng)建 VM。 可以定義自動(dòng)縮放規(guī)則來控制如何以及何時(shí)在規(guī)模集中添加或刪除 VM。 這些規(guī)則可以根據(jù) CPU 負(fù)載、內(nèi)存用量或網(wǎng)絡(luò)流量等指標(biāo)觸發(fā)。

使用 Azure 平臺(tái)映像時(shí),規(guī)模集最多支持 1,000 個(gè) VM。 對(duì)于有重要安裝或 VM 自定義要求的工作負(fù)荷,可能需要創(chuàng)建自定義 VM 映像。 使用自定義映像時(shí),在規(guī)模集中最多可以創(chuàng)建 300 個(gè) VM。

創(chuàng)建用于縮放的應(yīng)用

創(chuàng)建規(guī)模集之前,需使用New-AzureRmResourceGroup創(chuàng)建一個(gè)資源組。 以下示例在 ChinaEast 位置創(chuàng)建一個(gè)名為 myResourceGroupAutomate 的資源組:

PowerShell復(fù)制

New-AzureRmResourceGroup-ResourceGroupNamemyResourceGroupScaleSet-LocationChinaEast

在前面的教程中,你已了解如何使用自定義腳本擴(kuò)展來自動(dòng)執(zhí)行 VM 配置。 創(chuàng)建一個(gè)規(guī)模集配置,然后應(yīng)用自定義腳本擴(kuò)展來安裝并配置 IIS:

PowerShell復(fù)制

# 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ī)模負(fù)載均衡器

Azure 負(fù)載均衡器是位于第 4 層(TCP、UDP)的負(fù)載均衡器,通過在正常運(yùn)行的 VM 之間分發(fā)傳入流量提供高可用性。 負(fù)載均衡器運(yùn)行狀況探測(cè)器監(jiān)視每個(gè) VM 上的給定端口,并僅將流量分發(fā)給正常運(yùn)行的 VM。 有關(guān)詳細(xì)信息,請(qǐng)參閱有關(guān)如何對(duì) Windows 虛擬機(jī)進(jìn)行負(fù)載均衡的下一個(gè)教程。

創(chuàng)建一個(gè)具有公共 IP 地址、對(duì)端口 80 上的 Web 流量進(jìn)行分發(fā)的負(fù)載均衡器:

PowerShell復(fù)制

# 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ī)模集

現(xiàn)在,使用New-AzureRmVmss創(chuàng)建一個(gè)虛擬機(jī)規(guī)模集。 以下示例創(chuàng)建一個(gè)名為 myScaleSet 的規(guī)模集:

PowerShell復(fù)制

# 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 需要幾分鐘時(shí)間。

測(cè)試應(yīng)用

若要查看 IIS 網(wǎng)站的實(shí)際運(yùn)行情況,請(qǐng)使用Get-AzureRmPublicIPAddress獲取負(fù)載均衡器的公共 IP 地址。 以下示例獲取作為規(guī)模集的一部分創(chuàng)建的 myPublicIP 的 IP 地址:

PowerShell復(fù)制

Get-AzureRmPublicIPAddress-ResourceGroupNamemyResourceGroupScaleSet-NamemyPublicIP | select IpAddress

將公共 IP 地址輸入到 Web 瀏覽器中。 將顯示應(yīng)用,包括負(fù)載均衡器將流量分發(fā)到的 VM 的主機(jī)名:

若要查看規(guī)模集的實(shí)際運(yùn)行情況,可以強(qiáng)制刷新 Web 瀏覽器,以查看負(fù)載均衡器如何在運(yùn)行應(yīng)用的所有 VM 之間分發(fā)流量。

管理任務(wù)

在規(guī)模集的整個(gè)生命周期內(nèi),可能需要運(yùn)行一個(gè)或多個(gè)管理任務(wù)。 此外,可能還需要?jiǎng)?chuàng)建自動(dòng)執(zhí)行各種生命周期任務(wù)的腳本。 Azure PowerShell 提供了一種用于執(zhí)行這些任務(wù)的快速方法。 以下是一些常見任務(wù)。

查看規(guī)模集中的 VM

若要查看規(guī)模集中運(yùn)行的 VM 的列表,請(qǐng)使用Get-AzureRmVmssVM,如下所示:

PowerShell復(fù)制

# 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 實(shí)例

若要查看規(guī)模集中當(dāng)前包含的實(shí)例數(shù),請(qǐng)使用Get-AzureRmVmss并查詢 sku.capacity:

PowerShell復(fù)制

Get-AzureRmVmss-ResourceGroupNamemyResourceGroupScaleSet `-VMScaleSetNamemyScaleSet | `? ? Select-ExpandPropertySku

然后,可以使用Update-AzureRmVmss手動(dòng)增加或減少規(guī)模集中虛擬機(jī)的數(shù)目。 以下示例將規(guī)模集中 VM 的數(shù)目設(shè)置為 5:

PowerShell復(fù)制

# 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

這將花費(fèi)數(shù)分鐘來更新規(guī)模集中指定數(shù)目的實(shí)例。

配置自動(dòng)縮放規(guī)則

你可以定義自動(dòng)縮放規(guī)則,而不是手動(dòng)縮放規(guī)模集中實(shí)例的數(shù)目。 這些規(guī)則監(jiān)視規(guī)模集中的實(shí)例,并根據(jù)所定義的指標(biāo)和閾值做出相應(yīng)響應(yīng)。 如果平均 CPU 負(fù)載高于 60% 且持續(xù)時(shí)間超過 5 分鐘,以下示例將增加一個(gè)實(shí)例。 如果平均 CPU 負(fù)載低于 30% 且持續(xù)時(shí)間超過 5 分鐘,則將減少一個(gè)實(shí)例:

PowerShell復(fù)制

# 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)建了一個(gè)虛擬機(jī)規(guī)模集。 你已學(xué)習(xí)了如何執(zhí)行以下操作:

使用自定義腳本擴(kuò)展定義要縮放的 IIS 站點(diǎn)

為規(guī)模集創(chuàng)建負(fù)載均衡器

創(chuàng)建虛擬機(jī)規(guī)模集

增加或減少規(guī)模集中的實(shí)例數(shù)

創(chuàng)建自動(dòng)縮放規(guī)則

請(qǐng)繼續(xù)學(xué)習(xí)下一教程,詳細(xì)了解虛擬機(jī)的負(fù)載均衡概念。

對(duì)虛擬機(jī)進(jìn)行負(fù)載均衡

立即訪問http://market.azure.cn

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容