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

标签 azure azure-cloud-services azure-resource-manager azure-automation

我正在使用 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 应取决于云服务的部署,即生产>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')]"
      }
    }
  ]
}

关于azure - 使用依赖于云服务的 ARM 模板部署自动缩放设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44577292/

相关文章:

azure - 如何在 Azure 中托管 TCP 监听器?

Azure Lighthouse - 部署完所有内容后调用 HTTP 端点

azure - Cosmosdb mongo 在大集合上创建索引 - 风险

c# - 在 MVC Core 应用程序中使用 AddAzureADB2C 时向 ClaimsPrincipal 添加自定义声明

azure - 有没有办法在 Azure 云服务无响应或繁忙时收到警报?

azure - 当 ProcessEventsAsync(PartitionContext context, ienumerable<EventData> messages) 方法将被触发时

azure - 在每个订阅中使用 azure 策略创建资源组

azure - 如何在ARM模板中转义单引号

azure - 流分析不会将数据输出到 SQL,而是输出到 Blob 存储

Azure函数服务总线触发器并发