使用 ARM 範本在擴展集中建立虛擬機器
本文將逐步解說如何使用 ARM 範本來建立虛擬機器擴展集。
Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。
如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。
必要條件
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
ARM 範本
Azure Resource Manager 範本是一個 JavaScript 物件標記法 (JSON) 檔案,會定義專案的基礎結構和設定。 範本使用宣告式語法。 您可以描述預期的部署,而不需要撰寫程式設計命令順序來建立部署。
ARM 範本可讓您部署相關資源的群組。 在單一範本中,您可以建立虛擬機器擴展集、安裝應用程式,並設定自動縮放規則。 使用變數和參數,就可以重複使用此範本來更新現有的、或建立其他的擴展集。 您可以透過 Azure 入口網站、Azure CLI 或 Azure PowerShell,或從持續整合 / 持續傳遞 (CI/CD) 管線部署範本。
檢閱範本
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources"
}
},
"vmSku": {
"type": "string",
"defaultValue": "Standard_D2s_v3",
"metadata": {
"description": "Size of VMs in the VM Scale Set."
}
},
"vmssName": {
"type": "string",
"metadata": {
"description": "String used as a base for naming resources (9 characters or less). A hash is prepended to this string for some resources, and resource-specific information is appended."
}
},
"instanceCount": {
"type": "int",
"defaultValue": 1,
"minValue": 1,
"maxValue": 100,
"metadata": {
"description": "Number of VM instances (100 or less)."
}
},
"adminUsername": {
"type": "string",
"metadata": {
"description": "Admin username on all VMs."
}
},
"authenticationType": {
"type": "string",
"defaultValue": "sshPublicKey",
"allowedValues": [
"sshPublicKey",
"password"
],
"metadata": {
"description": "Type of authentication to use on the Virtual Machine. SSH key is recommended."
}
},
"adminPasswordOrKey": {
"type": "securestring",
"metadata": {
"description": "SSH Key or password for the Virtual Machine. SSH key is recommended."
}
},
"securityType": {
"type": "string",
"defaultValue": "TrustedLaunch",
"allowedValues": [
"Standard",
"TrustedLaunch"
],
"metadata": {
"description": "Security Type of the Virtual Machine."
}
},
"_artifactsLocation": {
"type": "string",
"defaultValue": "[deployment().properties.templatelink.uri]",
"metadata": {
"description": "The base URI where artifacts required by this template are located"
}
},
"_artifactsLocationSasToken": {
"type": "securestring",
"defaultValue": "",
"metadata": {
"description": "The sasToken required to access _artifactsLocation. When the template is deployed using the accompanying scripts, a sasToken will be automatically generated"
}
}
},
"variables": {
"addressPrefix": "10.0.0.0/16",
"subnetPrefix": "10.0.0.0/24",
"networkApiVersion": "2020-11-01",
"virtualNetworkName": "[concat(parameters('vmssName'), 'vnet')]",
"publicIPAddressName": "[concat(parameters('vmssName'), 'pip')]",
"subnetName": "[concat(parameters('vmssName'), 'subnet')]",
"loadBalancerName": "[concat(parameters('vmssName'), 'lb')]",
"publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
"networkSecurityGroupName": "[concat(parameters('vmssName'), 'nsg')]",
"bePoolName": "[concat(parameters('vmssName'), 'bepool')]",
"lbRuleName": "[concat(parameters('vmssName'), 'lbrule')]",
"lbProbeName": "[concat(parameters('vmssName'), 'lbprobe')]",
"bePoolConfigID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('loadBalancerName'),variables('bePoolName'))]",
"lbProbeID": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('loadBalancerName'),variables('lbProbeName'))]",
"nicName": "[concat(parameters('vmssName'), 'nic')]",
"ipConfigName": "[concat(parameters('vmssName'), 'ipconfig')]",
"frontEndIPConfigID": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations', variables('loadBalancerName'),'loadBalancerFrontEnd')]",
"osType": {
"publisher": "Canonical",
"offer": "0001-com-ubuntu-server-focal",
"sku": "20_04-lts-gen2",
"version": "latest"
},
"imageReference": "[variables('osType')]",
"securityProfileJson": {
"uefiSettings": {
"secureBootEnabled": true,
"vTpmEnabled": true
},
"securityType": "[parameters('securityType')]"
},
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
"keyData": "[parameters('adminPasswordOrKey')]"
}
]
}
}
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2023-04-01",
"name": "[variables('networkSecurityGroupName')]",
"location": "[parameters('location')]",
"properties": {
"securityRules": [
{
"name": "AllowPort9000",
"properties": {
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "9000",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2023-04-01",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
}
}
]
}
},
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2023-04-01",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"properties": {
"publicIPAllocationMethod": "Static",
"dnsSettings": {
"domainNameLabel": "[parameters('vmssName')]"
}
}
},
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2023-04-01",
"name": "[variables('loadBalancerName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard"
},
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
],
"properties": {
"frontendIPConfigurations": [
{
"name": "LoadBalancerFrontEnd",
"properties": {
"publicIPAddress": {
"id": "[variables('publicIPAddressID')]"
}
}
}
],
"backendAddressPools": [
{
"name": "[variables('bePoolName')]"
}
],
"probes": [
{
"name": "[variables('lbProbeName')]",
"properties": {
"port": 9000,
"protocol": "Tcp",
"numberOfProbes": 2,
"intervalInSeconds": 5
}
}
],
"loadBalancingRules": [
{
"name": "[variables('lbRuleName')]",
"properties": {
"frontendIPConfiguration": {
"id": "[variables('frontEndIPConfigID')]"
},
"backendAddressPool": {
"id": "[variables('bePoolConfigID')]"
},
"probe": {
"id": "[variables('lbProbeID')]"
},
"loadDistribution": "Default",
"backendPort": 9000,
"frontendPort": 9000,
"protocol": "Tcp",
"idleTimeoutInMinutes": 4,
"enableFloatingIP": false,
"enableTcpReset": false,
"disableOutboundSnat": false
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachineScaleSets",
"apiVersion": "2023-09-01",
"name": "[parameters('vmssName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('vmSku')]",
"tier": "Standard",
"capacity": "[parameters('instanceCount')]"
},
"dependsOn": [
"[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]",
"[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]"
],
"properties": {
"orchestrationMode": "Flexible",
"platformFaultDomainCount": 1,
"singlePlacementGroup": false,
"virtualMachineProfile": {
"storageProfile": {
"osDisk": {
"createOption": "FromImage",
"caching": "ReadWrite"
},
"imageReference": "[variables('imageReference')]"
},
"osProfile": {
"computerNamePrefix": "[parameters('vmssName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPasswordOrKey')]",
"linuxConfiguration": "[if(equals(parameters('authenticationType'), 'password'), null(), variables('linuxConfiguration'))]"
},
"securityProfile": "[if(equals(parameters('securityType'), 'TrustedLaunch'), variables('securityProfileJson'), null())]",
"networkProfile": {
"networkApiVersion": "[variables('networkApiVersion')]",
"networkInterfaceConfigurations": [
{
"name": "[variables('nicName')]",
"properties": {
"primary": true,
"ipConfigurations": [
{
"name": "[variables('ipConfigName')]",
"properties": {
"primary": true,
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"loadBalancerBackendAddressPools": [
{
"id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('loadBalancerName'), variables('bePoolName'))]"
}
]
}
}
]
}
}
]
},
"extensionProfile": {
"extensions": [
{
"name": "lapextension",
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"enableAutomaticUpgrade": true,
"settings": {
"fileUris": [
"[uri(parameters('_artifactsLocation'), concat('installserver.sh', parameters('_artifactsLocationSasToken')))]",
"[uri(parameters('_artifactsLocation'), concat('workserver.py', parameters('_artifactsLocationSasToken')))]"
],
"commandToExecute": "bash installserver.sh"
}
}
}
]
}
}
}
},
{
"type": "Microsoft.Insights/autoscaleSettings",
"apiVersion": "2022-10-01",
"name": "[concat(parameters('vmssName'), '-autoscalehost')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmSSName'))]"
],
"properties": {
"name": "[concat(parameters('vmssName'), '-autoscalehost')]",
"targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmSSName'))]",
"enabled": true,
"profiles": [
{
"name": "Profile1",
"capacity": {
"minimum": "1",
"maximum": "10",
"default": "1"
},
"rules": [
{
"metricTrigger": {
"metricName": "Percentage CPU",
"metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmSSName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "GreaterThan",
"threshold": 60
},
"scaleAction": {
"direction": "Increase",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT1M"
}
},
{
"metricTrigger": {
"metricName": "Percentage CPU",
"metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', parameters('vmSSName'))]",
"timeGrain": "PT1M",
"statistic": "Average",
"timeWindow": "PT5M",
"timeAggregation": "Average",
"operator": "LessThan",
"threshold": 30
},
"scaleAction": {
"direction": "Decrease",
"type": "ChangeCount",
"value": "1",
"cooldown": "PT1M"
}
}
]
}
]
}
}
]
}
範本中定義了下列資源:
- Microsoft.Network/virtualNetworks
- Microsoft.Network/publicIPAddresses
- Microsoft.Network/loadBalancers
- Microsoft.Compute/virtualMachineScaleSets
- Microsoft.Insights/autoscaleSettings
- Microsoft.Network/networkSecurityGroups
定義擴展集
若要使用範本建立縮放,您可以定義適當的資源。 虛擬機器擴展集資源類型的核心部分是:
屬性 | 屬性描述 | 範例範本值 |
---|---|---|
type | 要建立的 Azure 資源類型 | Microsoft.Compute/virtualMachineScaleSets |
NAME | 擴展集名稱 | myScaleSet |
location | 要建立擴展集的位置 | 美國東部 |
sku.name | 每個擴展集執行個體的 VM 大小 | 標準_A1 |
sku.capacity | 最初要建立的 VM 執行個體數目 | 2 |
imageReference | 要用於 VM 執行個體的平台或自訂映像 | Canonical Ubuntu Server 16.04-LTS |
osProfile.computerNamePrefix | 每個 VM 執行個體的名稱前置詞 | myvmss |
osProfile.adminUsername | 每個 VM 執行個體的使用者名稱 | azureuser |
osProfile.adminPassword | 每個 VM 執行個體的密碼 | P@ssw0rd! |
若要自訂擴展集範本,您可以變更 VM 大小或初始容量, 或使用不同的平台或自訂映像。
新增範例應用程式
若要測試您的擴展集,請安裝基本的 Web 應用程式。 當您部署擴展集時,VM 延伸模組可以提供後置部署設定和自動化工作,例如安裝應用程式。 您可以從 Azure 儲存體或 GitHub 下載指令碼,或是在擴充功能執行階段將指令碼提供給 Azure 入口網站。 若要將延伸模組套用至擴展集,請將 extensionProfile 區段新增至上述資源範例。 延伸模組設定檔通常會定義下列屬性:
- 擴充功能類型
- 延伸模組發行者
- 延伸模組版本
- 設定或安裝指令碼的位置
- 在 VM 執行個體上執行的命令
範本會使用自訂指令碼延伸模組來安裝 Bottle、Python web 架構,以及簡單的 HTTP 伺服器。
fileUris 中定義的兩個指令碼 - installserver.sh 和 workserver.py。 會從 GitHub 下載這些檔案,然後 commandToExecute 會執行 bash installserver.sh
來安裝和設定應用程式。
部署範本
您也可以使用 Azure CLI 部署 Resource Manager 範本:
# Create a resource group
az group create --name myResourceGroup --location EastUS
# Deploy template into resource group
az deployment group create -g myResourceGroup -f azuredeploy.json --parameters _artifactsLocation=https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/application-workloads/python/vmss-bottle-autoscale/azuredeploy.json
回答提示以提供擴展集名稱、執行個體計數和 VM 執行個體的管理員認證。 需要幾分鐘的時間才能建立擴展集和支援資源。
驗證部署
若要查看有效的擴展集,請在網頁瀏覽器中存取範例 Web 應用程式。 使用 az network public-ip list 取得負載平衡器的公用 IP 位址,如下所示:
az network public-ip list \
--resource-group myResourceGroup \
--query [*].ipAddress -o tsv
在網頁瀏覽器中輸入負載平衡器的公用 IP 位址,格式為 http://publicIpAddress:9000/do_work。 負載平衡器會將流量散發至您的其中一個 VM 執行個體,如下列範例所示:
清除資源
若不再需要,您可以使用 az group delete 移除資源群組、擴展集和所有相關資源,如下所示。 --no-wait
參數不會等待作業完成,就會將控制項傳回給提示字元。 --yes
參數會確認您想要刪除資源,而不另外對您提示將要進行此作業。
az group delete --name myResourceGroup --yes --no-wait