自动缩放联机终结点
适用范围:Azure CLI ml 扩展 v2(最新版)Python SDK azure-ai-ml v2(最新版)
自动缩放会自动运行适量的资源来处理应用程序的负载。 联机终结点支持通过与 Azure Monitor 自动缩放功能的集成进行自动缩放。
Azure Monitor 自动缩放支持一组丰富的规则。 可以配置基于指标的缩放(例如,CPU 利用率 >70%)、基于计划的缩放(例如,针对业务高峰期的缩放规则)或两者的组合。 有关详细信息,请参阅 Azure 中的自动缩放概述。
目前,可以使用 Azure CLI、REST、ARM 或基于浏览器的 Azure 门户来管理自动缩放。 今后也会添加对其他 Azure 机器学习 SDK(例如 Python SDK)的支持。
先决条件
- 一个已部署的终结点。 使用联机终结点部署机器学习模型并对其进行评分。
- 若要使用自动缩放,必须将角色
microsoft.insights/autoscalesettings/write
分配给管理自动缩放的标识。 你可以使用允许此操作的任何内置或自定义角色。 有关管理 Azure 机器学习角色的一般指南,请参阅管理用户和角色。 有关 Azure Monitor 自动缩放设置的详细信息,请参阅 Microsoft.Insights autoscalesettings。
定义自动缩放配置文件
若要为终结点启用自动缩放,首先要定义自动缩放配置文件。 此配置文件定义默认、最小和最大规模集容量。 以下示例将默认容量和最小容量设置为两个 VM 实例,将最大容量设置为五个 VM 实例:
以下代码片段设置终结点和部署名称:
# Note: this is based on the deploy-managed-online-endpoint.sh: it just adds autoscale settings in the end
set -e
#set the endpoint name from the how-to-deploy excercise
# <set_endpoint_deployment_name>
# set your existing endpoint name
ENDPOINT_NAME=your-endpoint-name
DEPLOYMENT_NAME=blue
# </set_endpoint_deployment_name>
export ENDPOINT_NAME=autoscale-endpt-`echo $RANDOM`
# create endpoint and deployment
az ml online-endpoint create --name $ENDPOINT_NAME -f endpoints/online/managed/sample/endpoint.yml
az ml online-deployment create --name blue --endpoint $ENDPOINT_NAME -f endpoints/online/managed/sample/blue-deployment.yml --all-traffic
az ml online-endpoint show -n $ENDPOINT_NAME
# <set_other_env_variables>
# ARM id of the deployment
DEPLOYMENT_RESOURCE_ID=$(az ml online-deployment show -e $ENDPOINT_NAME -n $DEPLOYMENT_NAME -o tsv --query "id")
# ARM id of the deployment. todo: change to --query "id"
ENDPOINT_RESOURCE_ID=$(az ml online-endpoint show -n $ENDPOINT_NAME -o tsv --query "properties.\"azureml.onlineendpointid\"")
# set a unique name for autoscale settings for this deployment. The below will append a random number to make the name unique.
AUTOSCALE_SETTINGS_NAME=autoscale-$ENDPOINT_NAME-$DEPLOYMENT_NAME-`echo $RANDOM`
# </set_other_env_variables>
# create autoscale settings. Note if you followed the how-to-deploy doc example, the instance count would have been 1. Now after applying this poilcy, it will scale up 2 (since min count and count are 2).
# <create_autoscale_profile>
az monitor autoscale create \
--name $AUTOSCALE_SETTINGS_NAME \
--resource $DEPLOYMENT_RESOURCE_ID \
--min-count 2 --max-count 5 --count 2
# </create_autoscale_profile>
# Add rule to default profile: scale up if cpu util > 70 %
# <scale_out_on_cpu_util>
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "CpuUtilizationPercentage > 70 avg 5m" \
--scale out 2
# </scale_out_on_cpu_util>
# Add rule to default profile: scale down if cpu util < 25 %
# <scale_in_on_cpu_util>
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "CpuUtilizationPercentage < 25 avg 5m" \
--scale in 1
# </scale_in_on_cpu_util>
# add rule to default profile: scale up based on avg. request latency (endpoint metric)
# <scale_up_on_request_latency>
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "RequestLatency > 70 avg 5m" \
--scale out 1 \
--resource $ENDPOINT_RESOURCE_ID
# </scale_up_on_request_latency>
#create weekend profile: scale to 2 nodes in weekend
# <weekend_profile>
az monitor autoscale profile create \
--name weekend-profile \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--min-count 2 --count 2 --max-count 2 \
--recurrence week sat sun --timezone "Pacific Standard Time"
# </weekend_profile>
# <delete_endpoint>
# delete the autoscaling profile
az monitor autoscale delete -n "$AUTOSCALE_SETTINGS_NAME"
# delete the endpoint
az ml online-endpoint delete --name $ENDPOINT_NAME --yes --no-wait
# </delete_endpoint>
接下来,获取部署和终结点的 Azure 资源管理器 ID:
# ARM id of the deployment
DEPLOYMENT_RESOURCE_ID=$(az ml online-deployment show -e $ENDPOINT_NAME -n $DEPLOYMENT_NAME -o tsv --query "id")
# ARM id of the deployment. todo: change to --query "id"
ENDPOINT_RESOURCE_ID=$(az ml online-endpoint show -n $ENDPOINT_NAME -o tsv --query "properties.\"azureml.onlineendpointid\"")
# set a unique name for autoscale settings for this deployment. The below will append a random number to make the name unique.
AUTOSCALE_SETTINGS_NAME=autoscale-$ENDPOINT_NAME-$DEPLOYMENT_NAME-`echo $RANDOM`
以下代码片段创建自动缩放配置文件:
az monitor autoscale create \
--name $AUTOSCALE_SETTINGS_NAME \
--resource $DEPLOYMENT_RESOURCE_ID \
--min-count 2 --max-count 5 --count 2
注意
有关详细信息,请参阅自动缩放参考页
使用指标创建横向扩展规则
常用的横向扩展规则是在平均 CPU 负载较高时增加 VM 实例数目。 如果 CPU 平均负载持续 5 分钟大于 70%,则以下示例将再分配两个节点(不超过最大数量):
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "CpuUtilizationPercentage > 70 avg 5m" \
--scale out 2
该规则是 my-scale-settings
配置文件的一部分(autoscale-name
与配置文件的 name
匹配)。 condition
参数的值表示当“VM 实例中的平均 CPU 使用率在 5 分钟内超过 70%”时,规则应触发。满足该条件时,将分配两个以上的 VM 实例。
注意
有关 CLI 语法的详细信息,请参阅 az monitor autoscale
。
使用指标创建横向缩减规则
当负载较轻时,横向缩减规则可以减少 VM 实例数目。 如果 CPU 负载持续 5 分钟小于 30%,则以下示例将释放单个节点,但至少保留 2 个:
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "CpuUtilizationPercentage < 25 avg 5m" \
--scale in 1
基于终结点指标创建缩放规则
上述规则将应用于部署。 现在,请添加应用于终结点的规则。 在此示例中,如果请求延迟持续 5 分钟大于 70 毫秒的平均值,则分配另一个节点。
az monitor autoscale rule create \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--condition "RequestLatency > 70 avg 5m" \
--scale out 1 \
--resource $ENDPOINT_RESOURCE_ID
基于计划创建缩放规则
还可以创建仅在特定日期或特定时间应用的规则。 此示例将周末的节点计数设置为 2。
az monitor autoscale profile create \
--name weekend-profile \
--autoscale-name $AUTOSCALE_SETTINGS_NAME \
--min-count 2 --count 2 --max-count 2 \
--recurrence week sat sun --timezone "Pacific Standard Time"
删除资源
如果你不打算使用自己的部署,请将其删除:
# delete the autoscaling profile
az monitor autoscale delete -n "$AUTOSCALE_SETTINGS_NAME"
# delete the endpoint
az ml online-endpoint delete --name $ENDPOINT_NAME --yes --no-wait
# </delete_endpoint>
后续步骤
若要详细了解如何使用 Azure Monitor 进行自动缩放,请参阅以下文章: