【问题标题】:Add Url Rule to Azure Application Gateway from a different ARM template从不同的 ARM 模板将 URL 规则添加到 Azure 应用程序网关
【发布时间】:2019-01-26 17:16:18
【问题描述】:

我有一个资源组 ARM 模板,用于创建为 url 路由配置的应用程序网关。它根据 url 路径规则将流量发送到该资源组中的不同 Web 应用程序。我部署了基础资源组 ARM 模板,然后每个 Web 应用都有自己的 ARM 模板,用于在应用服务计划上设置 Web 应用。我试图弄清楚如何将规则添加到应用程序网关上的现有 URL 路径映射,而无需在每个模板中定义整个应用程序网关。这样,我可以简单地添加 Web 应用程序并让它们使用特定的路径规则“注册”到应用程序网关。

我考虑过使用链接模板,其中我的基本模板将包含所有共享资源(数据库、应用服务计划和应用网关),但即使使用链接模板,我认为我也无法添加规则应用程序网关。

更新 因此,我通过添加对现有应用程序网关的引用来修改我的模板,然后为新的 BackEndPoolAddress 和新的路径规则添加变量。它最终是这样的(仅缩写为相关部分):

 "variables": {
    "appGateway": "[reference(concat('Microsoft.Network/applicationGateways/', 'appGateWay-', uniqueString(resourceGroup().id)), '2017-06-01')]",
    "pathRule": {
      "name": "[concat(parameters('websiteName'), '- RoutingRule')]",
      "properties": {
        "paths": [
          "[parameters('routingRule')]"
        ],
        "backendAddressPool": {
          "id": "[concat(variables('appGateway').id, '/backendAddressPools/',parameters('websiteName'), 'BackEndPool')]"
        },
        "backendHttpSettings": {
          "id": "[variables('appGateway').backendHttpSettingsCollection[0]]"
        }
      }
    },
    "backendPool": {
      "name": "[concat(parameters('websiteName'), 'BackEndPool')]",
      "properties": {
        "IpAddress": "[reference(variables('webSiteName')).defaultHostName]"
      }
    }
  },
 "resources": [
  ...
    {
      "apiVersion": "2017-06-01",
      "name": "[variables('appGateway').name]",
      "type": "Microsoft.Network/applicationGateways",
      "location": "[resourceGroup().location]",
      "properties": {
        "backendAddressPools": "[concat(variables('appGateway').backendAddressPools, variables('backendPool'))]",
        "urlPathMaps": [
          {
            "name": "[variables('appGateway').urlPathMaps[0]]",
            "pathRules": "[concat(variables('appGateway').urlPathMaps[0].pathRules, variables('pathRule'))]"
          }
        ]
      }
    }
  ],

但是,我收到一个模板验证错误,提示我无法使用变量部分中的引用函数。如果我不将它添加到变量部分,如何在我的变量部分中为池和 pathRule 构建正确的路径?

【问题讨论】:

  • 嗨,迈克。您能否分享一些关于您如何通过嵌套模板路由工作的见解。我正在尝试使用嵌套模板来实现以下目标:为每个应用程序拥有一个单独的模板文件,并从主文件中调用所有这些文件。任何帮助表示赞赏。谢谢。
  • @Avi 我最近实际上从 ARM 切换到了 CLI,所以我不再有那个代码,但是使用 CLI 真的很容易,因为它是幂等的。我将在下面添加一个答案,展示如何使用一个简单的 Azure CLI 脚本更新负载均衡器,该脚本在每次部署运行时都会运行。

标签: azure arm-template azure-application-gateway azure-template


【解决方案1】:

您可以使用reference() 函数、数组操作和嵌套模板来实现这一点(即使没有这些模板也可以工作,最坏的情况下您将需要它们)。示例:

"outputs": {
    "httpListeners": {
        "type": "array",
        "value": "[reference('application_gateway_id', '2018-08-01', 'Full').properties.httpListeners]"
    }
}

将返回您的数组或 httpListeners。您可以获取所有相关的应用程序网关属性并使用concat() 添加新的(附加)属性并将结果分配给属性(属性):

"httpListeners": "[concat(reference('application_gateway_id', '2018-08-01', 'Full').properties.httpListeners, variables('newListener'))]"

您只需要确保 2 个部署不会同时启动,一个可能会覆盖另一个

【讨论】:

  • 最终不得不使用嵌套模板路线,但这让我指出了正确的方向。谢谢。
【解决方案2】:

这是我最终使用 Azure CLI 的解决方案。这个脚本是幂等的,在我的发布过程中运行。

echo "Logging into AKS Cluster"
az aks get-credentials --resource-group $RESOURCEGROUP_NAME --name $AKSNAME

echo "Get the created service's ip address"
SERVICEIP=$(kubectl get service --namespace $AKSNAMESPACE $APPNAME-service -o jsonpath="{.status.loadBalancer.ingress[0].ip}")


echo "Creating backend pool - IP $SERVICEIP"
az network application-gateway address-pool create \
  --gateway-name $APPGATEWAYNAME \
  --resource-group $RESOURCEGROUP_NAME \
  --name "$APPNAME-pool" \
  --servers $SERVICEIP

echo "Creating probe"
az network application-gateway probe create \
    --gateway-name $APPGATEWAYNAME \
    --name "$APPNAME-probe" \
    --path $APPPROBE \
    --resource-group $RESOURCEGROUP_NAME \
    --protocol Http \
    --resource-group $RESOURCEGROUP_NAME \
    --host-name-from-http-settings true 

echo "Creating HTTP Settings"
az network application-gateway http-settings create \
    --gateway-name $APPGATEWAYNAME \
    --name "$APPNAME-settings" \
    --port 80 \
    --resource-group $RESOURCEGROUP_NAME \
    --host-name-from-backend-pool \
    --probe "$APPNAME-probe" \
    --protocol Http



echo "Creating URL Path Map"
az network application-gateway url-path-map rule create \
    --gateway-name $APPGATEWAYNAME \
    --name "$APPNAME-rule" \
    --paths $RULEPATH \
    --path-map-name $RULENAME \
    --resource-group $RESOURCEGROUP_NAME \
    --http-settings "$APPNAME-settings" \
    --address-pool "$APPNAME-pool"

【讨论】:

    猜你喜欢
    • 2022-10-25
    • 2018-07-20
    • 2018-12-17
    • 2020-09-29
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多