【问题标题】:Connect Azure Web App to vNet using ARM template使用 ARM 模板将 Azure Web App 连接到 vNet
【发布时间】:2017-01-16 08:47:17
【问题描述】:

我正在尝试在 Azure 中编写一组资源的设置脚本,作为其中的一部分,我需要一个 Web 应用程序能够通过 vNet 与在 VM 上运行的服务进行通信。

我创建了一个模板,该模板似乎做了所有应该做的事情来创建连接,但由于某种原因没有建立连接。查看门户显示该站点已连接到 vNet 并且证书是同步的,但 vNet 网关上的点到站点配置显示没有活动连接。

但是,如果我断开 Web 应用与 vNet 的连接,然后使用 Azure 门户中的设置按钮重新连接到同一个 vNet,一切都会正常运行。

我的模板中一定缺少一些东西,但在过去的几个小时里我一直在寻找我无法弄清楚是什么

这是我的 ARM 模板

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {

  },
  "resources": [
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[variables('nsgName')]",
      "apiVersion": "2016-03-30",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": []
      },
      "resources": [ ],
      "dependsOn": [ ]
    },
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[variables('infrastructureNsgName')]",
      "apiVersion": "2016-03-30",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": []
      },
      "resources": [ ],
      "dependsOn": [ ]
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('vnetName')]",
      "apiVersion": "2016-03-30",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "10.1.0.0/16"
          ]
        },
        "subnets": [
          {
            "name": "default",
            "properties": {
              "addressPrefix": "10.1.0.0/17",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
              }
            }
          },
          {
            "name": "infrastructure",
            "properties": {
              "addressPrefix": "10.1.254.0/24",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('infrastructureNsgName'))]"
              }
            }
          },
          {
            "name": "GatewaySubnet",
            "properties": {
              "addressPrefix": "10.1.128.0/24"
            }
          }
        ]
      },
      "resources": [ ],
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]",
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('infrastructureNsgName'))]"
      ]
    },
    {
      "type": "Microsoft.Web/sites",
      "kind": "api",
      "name": "[variables('gatewaySiteName')]",
      "apiVersion": "2015-08-01",
      "location": "[parameters('location')]",
      "properties": {
        "name": "[variables('gatewaySiteName')]",
        "hostNames": [
          "[concat(variables('gatewaySiteName'),'.azurewebsites.net')]"
        ],
        "enabledHostNames": [
          "[concat(variables('gatewaySiteName'),'.azurewebsites.net')]",
          "[concat(variables('gatewaySiteName'),'.scm.azurewebsites.net')]"
        ],
        "hostNameSslStates": [
          {
            "name": "[concat(variables('gatewaySiteName'),'.azurewebsites.net')]",
            "sslState": 0,
            "thumbprint": null,
            "ipBasedSslState": 0
          },
          {
            "name": "[concat(variables('gatewaySiteName'),'.scm.azurewebsites.net')]",
            "sslState": 0,
            "thumbprint": null,
            "ipBasedSslState": 0
          }
        ],
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('gatewayServerFarmName'))]"
      },
      "resources": [],
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('gatewayServerFarmName'))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('vnetName'))]"
      ]
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "sku": {
        "name": "S1",
        "tier": "Standard",
        "size": "S1",
        "family": "S",
        "capacity": 1
      },
      "kind": "",
      "name": "[variables('gatewayServerFarmName')]",
      "apiVersion": "2015-08-01",
      "location": "[parameters('location')]",
      "properties": {
        "name": "[variables('gatewayServerFarmName')]",
        "numberOfWorkers": 1
      },
      "resources": [ ],
      "dependsOn": [ ]
    },
    {
      "name": "[variables('vnetGatewayIpName')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "location": "[parameters('location')]",
      "apiVersion": "2015-06-15",
      "properties": {
        "publicIPAllocationMethod": "Dynamic"
      }
    },
    {
      "name": "[variables('vnetGatewayName')]",
      "type": "Microsoft.Network/virtualNetworkGateways",
      "location": "[parameters('location')]",
      "apiVersion": "2015-06-15",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('vnetGatewayIpName'))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('vnetName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets',variables('vnetName'),'GatewaySubnet')]"
              },
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('vnetGatewayIpName'))]"
              }
            },
            "name": "vnetGatewayConfig"
          }
        ],
        "gatewayType": "Vpn",
        "vpnType": "RouteBased",
        "enableBgp": false,
        "vpnClientConfiguration": {
          "vpnClientAddressPool": {
            "addressPrefixes": [
              "172.16.201.0/24"
            ]
          },
          "vpnClientRootCertificates": [
            {
              "name": "AppServiceCertificate.cer",
              "properties": {
                "PublicCertData": "[reference(concat('Microsoft.Web/sites/', variables('gatewaySiteName'), '/virtualNetworkConnections/virtualNetworkConnections')).certBlob]"
              }
            }
          ]
        }
      }
    },
    {
      "name": "[variables('gatewayVnetConnectionName')]",
      "type": "Microsoft.Web/sites/virtualNetworkConnections",
      "location": "[parameters('location')]",
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[concat('Microsoft.Web/sites/', variables('gatewaySiteName'))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('vnetName'))]"
      ],
      "properties": {
        "vnetResourceId": "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      }
    }
  ]
}

【问题讨论】:

    标签: azure azure-resource-manager


    【解决方案1】:

    在 Azure 门户中正确配置设置后,找到正确设置的一个好方法是深入了解一下。这可以通过查看 Azure 门户中的 https://resources.azure.com/Resource Explorer 来完成。

    在这里,您将找到处于工作状态的 json,并将其与您的 ARM 模板进行比较。这些设置不能一对一地复制,但它很接近。祝你好运找到不同之处。

    【讨论】:

    • 我已经尝试过了,不幸的是我看不出两者之间有什么区别
    【解决方案2】:

    仅靠 ARM 模板我永远无法完成这项工作。 但是,如果您可以在创建后多花一个 PowerShell 命令,它就可以很好地工作:

    # Set VNET Integration for Web App
    
    $ResourceGroup = "WeMadeThatInWestEuropeDidntWe"
    $WebApp = "LearningMomentsInProduction"
    $PropertiesObject = @{
           vnetName = "JimAreYouSureThisIsTheStagingVNET";
    }
    
    Set-AzureRmResource -PropertyObject $PropertiesObject `
                        -ResourceGroupName $ResourceGroup `
                        -ResourceType Microsoft.Web/sites/config `
                        -ResourceName $WebApp/web `
                        -ApiVersion 2015-08-01 -Force -Verbose |
                            Select -expand Properties |
                            Select VnetName
    
    # Expected output:
    #
    #  VnetName                                   
    #  --------                                   
    #  JimAreYouSureThisIsTheStagingVNET
    #
    # At this point your Web App is hooked up to the VNET
    

    编辑:

    这和我想的不一样。

    重新同步点到站点证书:

    $ResourceGroup = "WeMadeThatInWestEuropeDidntWe"
    # VNET Name or Gateway name, try with gateway name!
    $vnetName = "JimAreYouSureThisIsTheStagingVNET";
    
    $PropertiesObject = @{
      resyncRequired = "true"
    }
    
    Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $ResourceGroup `
                        -ResourceType Microsoft.Web/sites/virtualNetworkConnections `
                        -ResourceName $VnetName
                        -ApiVersion 2015-08-01 `
                        -Force -Verbose
    

    【讨论】:

    • 这似乎不起作用。在resources.azure.com中可以看到网站上的vnetName已经设置好了,但是还是没有连接到vnet
    • 确保您的模板没有尝试为 Web 应用设置 VNET 集成。
    • 我如何将证书复制到 VNET 网关?因为这现在创建了连接,但声明证书不同步
    • 你不需要这样做。当您调用上述 PowerShell 时,ARM 堆栈将自动生成证书。
    • 是的,它确实在网站端创建了证书,但没有添加到 vnet 网关。因此,当您查看网站上的网络连接时,它会显示“证书不同步”
    【解决方案3】:

    您将其作为站点内的嵌套资源:

    (假设所有内容都在同一个订阅和资源组下,否则,您需要修改resourceId() 的参数)

    "properties":[],
    "resources": [
                    {
                        "name": "[concat(variables('webappSiteName'), '/', variables('webappSiteName'), '-vnetIntegration')]",
                        "type": "Microsoft.Web/sites/virtualNetworkConnections",
                        "apiVersion": "2018-02-01",
                        "properties": {
                            "vnetResourceId": "[resourceId('Microsoft.Network/virtualNetworks', 'vnetname')]"
                        },
                        "dependsOn": [
                            "[resourceId('Microsoft.Web/sites', variables('webappSiteName'))]"
                        ]
                    }
    ]
    

    至少,这是必需的,但是,Microsoft.Web/sites/virtualNetworkConnections 资源必须提供客户端证书数据(必须在 vnet 网关的 P2S 连接中配置),这是属性 certBlob,根据它是文档:

    一个证书文件 (.cer) blob,其中包含用于验证点到站点 VPN 连接的私钥的公钥。

    如果该子节点为我工作,如果您不指定证书信息,您的 Web 应用程序将显示错误,指出证书不同步。

    参考virtualNetworkConnectionsdocumentation online

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      相关资源
      最近更新 更多