如何在 ARM 模板中动态生成流量管理器终结点?

How do I dynamically generate Traffic Manager endpoints in an ARM template?

我有一个 ARM 模板,它使用 copy 构造创建任意数量的 Azure webapps,就像这样(删除了不相关的部分):

{
  "parameters": { 
    "numWebsites": {
      "type": "int",
      "defaultValue": 2
    }
  },
  "resources": [
    "name": "[concat('webapp-', copyIndex()]",
    "type": "Microsoft.Web/sites",
    "copy": {
      "name": "websitescopy",
      "count": "[parameters('numWebsites')]"
    }
  ]
}

我还想为创建的每个网站创建一个带有端点的流量管理器配置文件。但是,似乎没有办法在流量管理器资源的 endpoints 参数中使用 copy。我见过的所有示例都有端点 explicitly listed out,但我不知道提前创建了多少 Web 应用程序,所以这对我不起作用。

如何在我的模板中动态生成端点?我试过在 trafficManagerProfiles 资源中使用 copy 语句,但这会创建多个配置文件,每个配置文件都有一个端点。

这是一个创建外部端点的示例 "child resource",配置文件是单独创建的,没有任何端点,然后此资源添加端点。它使用外部端点,但对于 webapp 应该同样有效,并且与标准复制功能兼容。

HtH, 加雷思

    {
        "apiVersion": "2015-11-01",
        "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
        "name": "ExternalEndpointExample/endpoint1",
        "dependsOn": ["Microsoft.Network/trafficManagerProfiles/ExternalEndpointExample"],
        "location": "global",
        "properties": {
            "target": "ep1.microsoft.com",
            "endpointStatus": "Enabled",
            "endpointLocation": "northeurope"
        }
    }

我还没有对此进行测试,但看来 copy/copyIndex 现在应该是流量管理器端点的受支持方案:

https://feedback.azure.com/forums/217313-networking/suggestions/12907815-support-copy-copyindex-in-traffic-manager-depend

这是我不久前实现的示例:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "solution-abbreviation": {
      "type": "string",
      "minLength": 1
    },
    "environment-abbreviation": {
      "type": "string",
      "allowedValues": [
        "dev",
        "test",
        "prod"
      ]
    },

    "userinterface-abbreviation": {
      "type": "string",
      "minLength": 1
    },
    "userinterface-locations": {
      "type": "array",
      "minLength": 1
    },
    "userinterface-appserviceplan-sku": {
      "type": "string",
      "allowedValues": [
        "Free",
        "Shared",
        "Basic",
        "Standard"
      ]
    },
    "userinterface-appserviceplan-workersize": {
      "type": "string",
      "allowedValues": [
        "0",
        "1",
        "2"
      ]
    },
    "userinterface-appserviceplan-numberofworkers": {
      "type": "int"
    }
  },
  "variables": {
    "userinterface-trafficmanager-name": "[concat(parameters('solution-abbreviation'), '-', parameters('environment-abbreviation'), '-', parameters('userinterface-abbreviation'))]"
  },
  "resources": [
    {
      "name": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[parameters('userinterface-locations')[copyIndex()]]",
      "apiVersion": "2014-06-01",
      "dependsOn": [ ],
      "tags": {
        "displayName": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]"
      },
      "copy": {
        "name": "[concat('serverfarms', '-copy')]",
        "count": "[length(parameters('userinterface-locations'))]"
      },
      "properties": {
        "name": "[concat(variables('userinterface-trafficmanager-name'), '-', parameters('userinterface-locations')[copyIndex()])]",
        "sku": "[parameters('userinterface-appserviceplan-sku')]",
        "workerSize": "[parameters('userinterface-appserviceplan-workersize')]",
        "numberOfWorkers": "[parameters('userinterface-appserviceplan-numberofworkers')]"
      }
    }
  ],
  "outputs": {
  }
}

我不清楚接受的答案,到目前为止 Paul 的答案仅提供了部分示例。在故障排除期间,我遇到了另一个与名称段长度相关的错误,这不容易理解,所以这是我的工作解决方案(non-relevant 部分也被删除):

    {
  "type": "Microsoft.Network/trafficManagerProfiles",
  "apiVersion": "2017-05-01",
  "location": "global",
  "name": "[variables('trafManagerProfileName')]",
  "properties": { ...}
},
{
  "apiVersion": "2015-11-01",
  "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
  "name": "[concat(variables('trafManagerProfileName'), '/Endpoint', copyIndex())]",
  "dependsOn": [
    "[concat('Microsoft.Network/trafficManagerProfiles/', variables('trafManagerProfileName'))]",
    "[concat(parameters('app_name')[copyIndex()])]"
  ],
  "location": "global",
  "properties": {
    "target": "[concat(parameters('app_name')[copyIndex()])]"        
  },
  "copy": {
    "count": "[variables('app_count')]",
    "name": "app_copy"
  }
},
{
  "type": "Microsoft.Web/sites",
  "name": "[concat(parameters('app_name')[copyIndex()])]",
  "copy": {
    "count": "[variables('app_count')]",
    "name": "app_copy"
  }
}

您可以参考以下模板添加流量管理端点并启用复制。

Azure 不提供在副本中添加端点的功能,因此您需要创建一个单独的资源并将其 link 到原始资源以添加端点。这样模板内部就支持复制功能了。

"resources": [
    {
      "apiVersion": "2017-05-01",
      "type": "Microsoft.Network/trafficManagerProfiles",
      "name": "[parameters('resourceName')]",
      "location": "global",
      "properties": {
        "profileStatus": "Enabled",
        "trafficRoutingMethod": "Performance",
        "dnsConfig": {
          "relativeName": "[parameters('uniqueDnsName')]",
          "ttl": "[parameters('timeToLive')]"
        },
        "monitorConfig": {
          "protocol": "[parameters('protocol')]",
          "port": "[parameters('portName')]",
          "path": "[parameters('pathName')]",
          "intervalInSeconds": "[parameters('monitorIntervalInSeconds')]",
          "timeoutInSeconds": "[parameters('monitorTimeoutInSeconds')]",
          "toleratedNumberOfFailures": "[parameters('toleratedNumberOfFailures')]"
        }
      }
    },
    {
      "apiVersion": "2017-05-01",
      "type": "Microsoft.Network/trafficManagerProfiles/azureEndpoints",
      "dependsOn": [
        "Microsoft.Network/trafficManagerProfiles/ExampleTMProfile"
      ],
      "location": "global",
      "name": "[concat('ExampleTMProfile/Endpoint', copyIndex())]",
      "copy": {
        "name": "azureEndpointsCopy",
        "count": "[length(parameters('azureEndpointNameArray'))]"
      },
      "properties": {
        "targetResourceId": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('azureEndpointNameArray')[copyIndex()])]",
        "endpointStatus": "Enabled"
      }
    }
  ]