使用 Linux 规模集模板中的来宾指标执行自动缩放Autoscale using guest metrics in a Linux scale set template

在 Azure 中,会从 VM 和规模集收集两类广泛的指标:主机指标和来宾指标。There are two broad types of metrics in Azure that are gathered from VMs and scale sets: Host metrics and Guest metrics. 粗略地讲,若要使用标准 CPU、磁盘和网络指标,则主机指标非常适合。At a high level, if you would like to use standard CPU, disk, and network metrics, then host metrics are a good fit. 不过,如果需要更多指标,则应考虑使用来宾指标。If, however, you need a larger selection of metrics, then guest metrics should be looked into.

主机指标不需要进行额外设置,因为它们是由主机 VM 收集,而来宾指标则需要在来宾 VM 中安装 Azure 诊断扩展Linux Azure 诊断扩展Host metrics do not require additional setup because they are collected by the host VM, whereas guest metrics require you to install the Azure Diagnostics extension or the Linux Azure Diagnostics extension in the guest VM. 使用来宾指标而不是主机指标的一个常见原因是,与主机指标相比,来宾指标提供更大的指标选择范围。One common reason to use guest metrics instead of host metrics is that guest metrics provide a larger selection of metrics than host metrics. 内存消耗指标就是这样一个例子,它们只会通过来宾指标提供。One such example is memory-consumption metrics, which are only available via guest metrics. 此处列出了支持的主机指标,此处列出了常用的来宾指标。The supported host metrics are listed here, and commonly used guest metrics are listed here. 本文介绍如何修改基本可行规模集模板,以根据 Linux 规模集的来宾指标使用自动缩放规则。This article shows how to modify the basic viable scale set template to use autoscale rules based on guest metrics for Linux scale sets.

更改模板定义Change the template definition

此前的文章中,我们创建了基本的规模集模板。In a previous article we had created a basic scale set template. 我们现在将使用这个此前的模板并对其进行修改,以便创建一个模板来部署其自动缩放功能基于来宾指标的 Linux 规模集。We will now use that earlier template and modify it to create a template that deploys a Linux scale set with guest metric based autoscale.

首先,添加 storageAccountNamestorageAccountSasToken 的参数。First, add parameters for storageAccountName and storageAccountSasToken. 诊断代理将指标数据存储在此存储帐户中的某个内。The diagnostics agent stores metric data in a table in this storage account. 从 Linux 诊断代理版本 3.0 开始,不再支持使用存储访问密钥。As of the Linux Diagnostics Agent version 3.0, using a storage access key is no longer supported. 改为使用 SAS 令牌Instead, use a SAS Token.

     },
     "adminPassword": {
       "type": "securestring"
+    },
+    "storageAccountName": {
+      "type": "string"
+    },
+    "storageAccountSasToken": {
+      "type": "securestring"
     }
   },

接下来,修改规模集 extensionProfile 以包含诊断扩展。Next, modify the scale set extensionProfile to include the diagnostics extension. 在此配置中,指定要从中收集指标的规模集的资源 ID,以及用来存储指标的存储帐户和 SAS 令牌。In this configuration, specify the resource ID of the scale set to collect metrics from, as well as the storage account and SAS token to use to store the metrics. 指定指标的聚合频率(在本例中为每隔一分钟)以及要跟踪的指标(在本例中为已用内存百分比)。Specify how frequently the metrics are aggregated (in this case, every minute) and which metrics to track (in this case, percent used memory). 有关此配置以及除已用内存百分比之外的其他指标的详细信息,请参阅此文档For more detailed information on this configuration and metrics other than percent used memory, see this documentation.

                 }
               }
             ]
+          },
+          "extensionProfile": {
+            "extensions": [
+              {
+                "name": "LinuxDiagnosticExtension",
+                "properties": {
+                  "publisher": "Microsoft.Azure.Diagnostics",
+                  "type": "LinuxDiagnostic",
+                  "typeHandlerVersion": "3.0",
+                  "settings": {
+                    "StorageAccount": "[parameters('storageAccountName')]",
+                    "ladCfg": {
+                      "diagnosticMonitorConfiguration": {
+                        "performanceCounters": {
+                          "sinks": "WADMetricJsonBlob",
+                          "performanceCounterConfiguration": [
+                            {
+                              "unit": "percent",
+                              "type": "builtin",
+                              "class": "memory",
+                              "counter": "percentUsedMemory",
+                              "counterSpecifier": "/builtin/memory/percentUsedMemory",
+                              "condition": "IsAggregate=TRUE"
+                            }
+                          ]
+                        },
+                        "metrics": {
+                          "metricAggregation": [
+                            {
+                              "scheduledTransferPeriod": "PT1M"
+                            }
+                          ],
+                          "resourceId": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', 'myScaleSet')]"
+                        }
+                      }
+                    }
+                  },
+                  "protectedSettings": {
+                    "storageAccountName": "[parameters('storageAccountName')]",
+                    "storageAccountSasToken": "[parameters('storageAccountSasToken')]",
+                    "sinksConfig": {
+                      "sink": [
+                        {
+                          "name": "WADMetricJsonBlob",
+                          "type": "JsonBlob"
+                        }
+                      ]
+                    }
+                  }
+                }
+              }
+            ]
           }
         }
       }

最后,添加 autoscaleSettings 资源以基于这些指标配置自动缩放。Finally, add an autoscaleSettings resource to configure autoscale based on these metrics. 此资源包含一个引用规模集的 dependsOn 子句,确保在尝试自动缩放该规模集之前它已存在。This resource has a dependsOn clause that references the scale set to ensure that the scale set exists before attempting to autoscale it. 如果选择不同的指标作为自动缩放的依据,可使用诊断扩展配置中的 counterSpecifier 作为自动缩放配置中的 metricNameIf you choose a different metric to autoscale on, you would use the counterSpecifier from the diagnostics extension configuration as the metricName in the autoscale configuration. 有关自动缩放配置的详细信息,请参阅自动缩放最佳做法Azure Monitor REST API 参考文档For more information on autoscale configuration, see the autoscale best practices and the Azure Monitor REST API reference documentation.

+    },
+    {
+      "type": "Microsoft.Insights/autoscaleSettings",
+      "apiVersion": "2015-04-01",
+      "name": "guestMetricsAutoscale",
+      "location": "[resourceGroup().location]",
+      "dependsOn": [
+        "Microsoft.Compute/virtualMachineScaleSets/myScaleSet"
+      ],
+      "properties": {
+        "name": "guestMetricsAutoscale",
+        "targetResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', 'myScaleSet')]",
+        "enabled": true,
+        "profiles": [
+          {
+            "name": "Profile1",
+            "capacity": {
+              "minimum": "1",
+              "maximum": "10",
+              "default": "3"
+            },
+            "rules": [
+              {
+                "metricTrigger": {
+                  "metricName": "/builtin/memory/percentUsedMemory",
+                  "metricNamespace": "",
+                  "metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', 'myScaleSet')]",
+                  "timeGrain": "PT1M",
+                  "statistic": "Average",
+                  "timeWindow": "PT5M",
+                  "timeAggregation": "Average",
+                  "operator": "GreaterThan",
+                  "threshold": 60
+                },
+                "scaleAction": {
+                  "direction": "Increase",
+                  "type": "ChangeCount",
+                  "value": "1",
+                  "cooldown": "PT1M"
+                }
+              },
+              {
+                "metricTrigger": {
+                  "metricName": "/builtin/memory/percentUsedMemory",
+                  "metricNamespace": "",
+                  "metricResourceUri": "[resourceId('Microsoft.Compute/virtualMachineScaleSets', 'myScaleSet')]",
+                  "timeGrain": "PT1M",
+                  "statistic": "Average",
+                  "timeWindow": "PT5M",
+                  "timeAggregation": "Average",
+                  "operator": "LessThan",
+                  "threshold": 30
+                },
+                "scaleAction": {
+                  "direction": "Decrease",
+                  "type": "ChangeCount",
+                  "value": "1",
+                  "cooldown": "PT1M"
+                }
+              }
+            ]
+          }
+        ]
+      }
     }
   ]
 }

后续步骤Next steps

可以按照 Azure Resource Manager 文档部署上述模板。You can deploy the preceding template by following the Azure Resource Manager documentation.

若要阅读这一系列的教程,可先阅读最小可行规模集模板文章You can start this tutorial series from the minimum viable scale set template article.

可以了解如何修改最小可行规模集模板,以便将规模集部署到现有的虚拟网络You can see how to modify the minimum viable scale set template to deploy the scale set into an existing virtual network.

可以了解如何修改最小可行规模集模板,并使用自定义映像部署规模集You can see how to modify the minimum viable scale set template to deploy the scale set with a custom image.

你可以了解如何修改最小可行规模集模板,以使用基于来宾的自动缩放部署 Linux 规模集You can see how to modify the minimum viable scale set template to deploy a Linux scale set with guest-based autoscale.

有关规模集的更多常规信息,请参阅规模集概述页For more general information about scale sets, refer to the scale set overview page.