使用依赖云服务的 ARM 模板部署自动缩放设置

Deploying autoscale settings using ARM template that depend on Cloud Service

我正在使用 ARM 模板文件来部署两个资源:

  1. 一个 Azure 云服务(有 4 个角色)
  2. 自动缩放设置(包括所有 4 个角色的规则)

如果云服务存在,并且角色是运行,那么我可以并行部署这两个角色,它可以成功地使用以下模板

首次部署云服务时出现该问题。 这是合理的,因为自动缩放设置需要 targetResourceUri 来应用规则,如果该资源不存在 - 部署失败是件好事。

为此,他们发明了 dependsOn 属性,但由于某种原因我无法让它工作,由于targetResourceUri 不存在(在部署角色之前部署缩放规则太快)。

这是模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceName": {
      "type": "string"
    },
    "PackageLink": {
      "type": "string"
    },
    "serviceConfigurationLink": {
      "type": "string"
    },
    "deploymentLabel": {
      "type": "string"
    },
    "autoscaled_resource_name": {
      "defaultValue": "autoscale-rules-default",
      "type": "string"
    },
    "metricResourceUri_cpu": {
      "type": "string"
    },
    "autoscale_resourceUri": {
      "type": "string"
    },
    "autoscale_rules_enabled": {
      "defaultValue": true,
      "type": "bool"
    },
    "min_instance_count": {
      "type": "string"
    },
    "max_instance_count": {
      "type": "string"
    },
    "default_instance_count_when_metric_unavailable": {
      "type": "string"
    }
  },
  "variables": {
    "resourceLocation": "[resourcegroup().location]"
  },
  "resources": [
    {
      "apiVersion": "2016-04-01",
      "type": "Microsoft.ClassicCompute/domainNames",
      "name": "[parameters('serviceName')]",
      "location": "[variables('resourceLocation')]",
      "resources": [
        {
          "apiVersion": "2016-04-01",
          "name": "production",
          "type": "deploymentSlots",
          "dependsOn": [
            "[resourceId('Microsoft.ClassicCompute/domainNames', parameters('serviceName'))]"
          ],
          "properties": {
            "packageLink": {
              "uri": "[parameters('PackageLink')]"
            },
            "deploymentLabel": "[parameters('deploymentLabel')]",
            "ConfigurationLink": {
              "uri": "[parameters('serviceConfigurationLink')]"
            },
            "deploymentOptions": "StartDeployment"
          }
        }
      ]
    },
    {
      "type": "microsoft.insights/autoscalesettings",
      "name": "[parameters('autoscaled_resource_name')]",
      "apiVersion": "2014-04-01",
      "location": "eastus",
      "dependsOn": [
        "[parameters('serviceName')]"
      ],
      "properties": {
        "profiles": [
          {
            "name": "[parameters('autoscaled_resource_name')]",
            "capacity": {
              "minimum": "[parameters('min_instance_count')]",
              "maximum": "[parameters('max_instance_count')]",
              "default": "[parameters('default_instance_count_when_metric_unavailable')]"
            },
            "rules": [
              {
                "metricTrigger": {
                  "metricName": "Percentage CPU",
                  "metricNamespace": "",
                  "metricResourceUri": "[parameters('metricResourceUri_cpu')]",
                  "timeGrain": "PT5M",
                  "statistic": "Average",
                  "timeWindow": "PT5M",
                  "timeAggregation": "Average",
                  "operator": "GreaterThan",
                  "threshold": 85
                },
                "scaleAction": {
                  "direction": "Increase",
                  "type": "PercentChangeCount",
                  "value": "45",
                  "cooldown": "PT10M"
                }
              }
            ]
          }
        ],
        "enabled": "[parameters('autoscale_rules_enabled')]",
        "name": "[parameters('autoscaled_resource_name')]",
        "targetResourceUri": "[parameters('autoscale_resourceUri')]",
        "notifications": [
          {
            "operation": "Scale",
            "email": {
              "sendToSubscriptionAdministrator": true,
              "sendToSubscriptionCoAdministrators": true,
              "customEmails": []
            }
          }
        ]
      }
    }
  ]
}

这是 powershell 日志:

VERBOSE: Performing the operation "Creating Deployment" on target "************".
WARNING: The DeploymentDebug setting has been enabled. This can potentially log secrets like passwords used in resource
 property or listKeys operations when you retrieve the deployment operations through
Get-AzureRmResourceGroupDeploymentOperation
VERBOSE: 1:00:25 AM - Template is valid.
VERBOSE: 1:00:28 AM - Create template deployment 'azuredeploy-0615-2200'
VERBOSE: 1:00:28 AM - Checking deployment status in 5 seconds
VERBOSE: 1:00:34 AM - Checking deployment status in 10 seconds
VERBOSE: 1:00:44 AM - Resource Microsoft.ClassicCompute/domainNames/deploymentSlots '************/production'
provisioning status is running
New-AzureRmResourceGroupDeployment : 1:00:44 AM - Resource microsoft.insights/autoscalesettings
'autoscale-rules-default' failed with message '{
  "code": "TargetResourceNotFound",
  "message": "The target resource id '/subscriptions/************/resourceGroups/"************/providers/Microsoft.ClassicCompute/domainNames/"************/slots/Production/roles/WorkerRole' was not found."
}'
At C:\Users\************\Deploy-AzureResourceGroup.ps1:98 char:1
+ New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDep
   loymentCmdlet

VERBOSE: 1:00:44 AM - Resource Microsoft.ClassicCompute/domainNames '************' provisioning status is succeeded
VERBOSE: 1:00:45 AM - Checking deployment status in 15 seconds
VERBOSE: 1:01:00 AM - Checking deployment status in 20 seconds
VERBOSE: 1:01:21 AM - Checking deployment status in 25 seconds
VERBOSE: 1:01:47 AM - Checking deployment status in 30 seconds

似乎在确认成功创建云服务之前就已经部署了自动缩放规则。

我的配置有误吗?

所依赖的资源,在我的例子中 autoscalesettings 应该依赖于云服务的 deployment,即 production[=23] =] 或 staging - 它们的类型都是:Microsoft.ClassicCompute/domainNames/deploymentSlots.

重要的部分是:

  "dependsOn": [
    "[resourceId('Microsoft.ClassicCompute/domainNames/deploymentSlots', parameters('serviceName'), 'production')]"
  ]

这是我的做法:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "deploymentLabel": {
      "type": "string"
    },
    "serviceName": {
      "type": "string"
    },
    "PackageLink": {
      "type": "securestring"
    },
    "serviceConfigurationLink": {
      "type": "securestring"
    },
    "autoscaled_resource_name": {
      "type": "string"
    },
    "metricResourceUri": {
      "type": "string"
    },
    "autoscale_resourceUri": {
      "type": "string"
    }
  },
  "variables": {
    "resourceLocation": "[resourcegroup().location]"
  },
  "resources": [
    {
      "apiVersion": "2016-04-01",
      "type": "Microsoft.ClassicCompute/domainNames",
      "name": "[parameters('serviceName')]",
      "location": "[variables('resourceLocation')]",
      "resources": [
        {
          "apiVersion": "2016-04-01",
          "name": "production",
          "type": "deploymentSlots",
          "dependsOn": [
            "[resourceId('Microsoft.ClassicCompute/domainNames', parameters('serviceName'))]"
          ],
          "properties": {
            "packageLink": {
              "uri": "[parameters('PackageLink')]"
            },
            "deploymentLabel": "[parameters('deploymentLabel')]",
            "ConfigurationLink": {
              "uri": "[parameters('serviceConfigurationLink')]"
            },
            "deploymentOptions": "StartDeployment"
          }
        }
      ]
    },
    {
      "type": "microsoft.insights/autoscalesettings",
      "name": "[parameters('autoscaled_resource_name')]",
      "apiVersion": "2014-04-01",
      "location": "eastus",
      "dependsOn": [
        "[resourceId('Microsoft.ClassicCompute/domainNames/deploymentSlots', parameters('serviceName'), 'production')]"
      ], 
      "properties": {
        "profiles": [],
        "enabled": true,
        "name": "[parameters('autoscaled_resource_name')]",
        "targetResourceUri": "[parameters('autoscale_resourceUri')]"
      }
    }
  ]
}