0

我一直在尝试将 Azure 应用程序网关部署到现有 VM 上的前端应用程序,并使用主机名进行池选择。我根据文章https://github.com/Azure/azure-从 git https://github.com/Azure/azure-quickstart-templates/tree/master/201-application-gateway-multihosting开始使用此模板内容/blob/master/articles/application-gateway/application-gateway-multi-site-overview.md

这是我使用的修改后的模板

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "vnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.0.0/16",
        "metadata": {
            "description": "Address prefix for the Virtual Network"
        }
    },
    "subnetPrefix": {
        "type": "string",
        "defaultValue": "10.0.0.0/28",
        "metadata": {
            "description": "Gateway Subnet prefix"
        }
    },
    "skuName": {
        "type": "string",
        "allowedValues": [
            "Standard_Small",
            "Standard_Medium",
            "Standard_Large"
        ],
        "defaultValue": "Standard_Small",
        "metadata": {
            "description": "Sku Name"
        }
    },
    "capacity": {
        "type": "int",
        "defaultValue": 4,
        "metadata": {
            "description": "Number of instances"
        }
    },
    "backendIpAddress1": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 1"
        }
    },
    "backendIpAddress2": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 2"
        }
    },
    "backendIpAddress3": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 3"
        }
    },
    "backendIpAddress4": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 4"
        }
    },
    "backendIpAddress5": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 5"
        }
    },
    "backendIpAddress6": {
        "type": "string",
        "metadata": {
            "description": "IP Address for Backend Server 6"
        }
    },
    "hostName1": {
        "type": "string",
        "metadata": {
            "description": "HostName for listener 1"
        }
    },
    "hostName2": {
        "type": "string",
        "metadata": {
            "description": "HostName for listener 2"
        }
    },
    "certData1": {
        "type": "securestring",
        "metadata": {
            "description": "Base-64 encoded form of the .pfx file"
        }
    },
    "certPassword1": {
        "type": "securestring",
        "metadata": {
            "description": "Password for .pfx certificate"
        }
    }
},
"variables": {
    "applicationGatewayName": "PortalGateway",
    "publicIPAddressName": "PortalGatewayFrontendIP",
    "virtualNetworkName": "PalitonNetworks-East-VirtualNetwork",
    "subnetName": "GWSubnet1",
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
    "publicIPRef": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
    "applicationGatewayID": "[resourceId('Microsoft.Network/applicationGateways',variables('applicationGatewayName'))]",
    "apiVersion": "2015-06-15"
},
"resources": [
    {
        "apiVersion": "[variables('apiVersion')]",
        "type": "Microsoft.Network/publicIPAddresses",
        "name": "[variables('publicIPAddressName')]",
        "location": "[resourceGroup().location]",
        "properties": {
            "publicIPAllocationMethod": "Dynamic"
        }
    },
    {
        "apiVersion": "[variables('apiVersion')]",
        "type": "Microsoft.Network/virtualNetworks",
        "name": "[variables('virtualNetworkName')]",
        "location": "[resourceGroup().location]",
        "properties": {
            "addressSpace": {
                "addressPrefixes": [
                    "[parameters('vnetAddressPrefix')]"
                ]
            },
            "subnets": [
                {
                    "name": "[variables('subnetName')]",
                    "properties": {
                        "addressPrefix": "[parameters('subnetPrefix')]"
                    }
                }
            ]
        }
    },
    {
        "apiVersion": "[variables('apiVersion')]",
        "name": "[variables('applicationGatewayName')]",
        "type": "Microsoft.Network/applicationGateways",
        "location": "[resourceGroup().location]",
        "dependsOn": [
            "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
            "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
        ],
        "properties": {
            "sku": {
                "name": "[parameters('skuName')]",
                "tier": "Standard",
                "capacity": "[parameters('capacity')]"
            },
            "sslCertificates": [
                {
                    "name": "appGatewaySslCert1",
                    "properties": {
                        "data": "[parameters('certData1')]",
                        "password": "[parameters('certPassword1')]"
                    }
                }

            ],
            "gatewayIPConfigurations": [
                {
                    "name": "appGatewayIpConfig",
                    "properties": {
                        "subnet": {
                            "id": "[variables('subnetRef')]"
                        }
                    }
                }
            ],
            "frontendIPConfigurations": [
                {
                    "name": "appGatewayFrontendIP",
                    "properties": {
                        "PublicIPAddress": {
                            "id": "[variables('publicIPRef')]"
                        }
                    }
                }
            ],
            "frontendPorts": [
                {
                    "name": "appGatewayFrontendPort1",
                    "properties": {
                        "Port": 443
                    }
                },
                {
                    "name": "appGatewayFrontendPort2",
                    "properties": {
                        "Port": 80
                    }
                }
            ],
            "backendAddressPools": [
                {
                    "name": "appGatewayBackendPool1",
                    "properties": {
                        "BackendAddresses": [
                            {
                                "IpAddress": "[parameters('backendIpAddress1')]"
                            },
                            {
                                "IpAddress": "[parameters('backendIpAddress2')]"
                            },
                            {
                                "IpAddress": "[parameters('backendIpAddress3')]"
                            }
                        ]
                    }
                },
                {
                    "name": "appGatewayBackendPool2",
                    "properties": {
                        "BackendAddresses": [
                            {
                                "IpAddress": "[parameters('backendIpAddress4')]"
                            },
                            {
                                "IpAddress": "[parameters('backendIpAddress5')]"
                            },
                            {
                                "IpAddress": "[parameters('backendIpAddress6')]"
                            }
                        ]
                    }
                }
            ],
            "backendHttpSettingsCollection": [
                {
                    "name": "appGatewayBackendHttpSettings",
                    "properties": {
                        "Port": 80,
                        "Protocol": "Http",
                        "CookieBasedAffinity": "Disabled"
                    }
                },
                {
                    "name": "appGatewayBackendHttpsSettings",
                    "properties": {
                        "Port": 443,
                        "Protocol": "Https",
                        "CookieBasedAffinity": "Disabled"
                    }
                }
            ],
            "httpListeners": [
                {
                    "name": "appGatewayHttpsListener-Group1",
                    "properties": {
                        "FrontendIPConfiguration": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendIPConfigurations/appGatewayFrontendIP')]"
                        },
                        "FrontendPort": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendPorts/appGatewayFrontendPort1')]"
                        },
                        "Protocol": "Https",
                        "SslCertificate": {
                            "Id": "[concat(variables('applicationGatewayID'), '/sslCertificates/appGatewaySslCert1')]"
                        },
                        "HostName": "[parameters('hostName1')]",
                        "RequireServerNameIndication": "false"
                    }
                },
                {
                    "name": "appGatewayHttpsListener-Group2",
                    "properties": {
                        "FrontendIPConfiguration": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendIPConfigurations/appGatewayFrontendIP')]"
                        },
                        "FrontendPort": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendPorts/appGatewayFrontendPort1')]"
                        },
                        "Protocol": "Https",
                        "SslCertificate": {
                            "Id": "[concat(variables('applicationGatewayID'), '/sslCertificates/appGatewaySslCert1')]"
                        },
                        "HostName": "[parameters('hostName2')]",
                        "RequireServerNameIndication": "false"
                    }
                },
        {
                    "name": "appGatewayHttpListener-Group1",
                    "properties": {
                        "FrontendIPConfiguration": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendIPConfigurations/appGatewayFrontendIP')]"
                        },
                        "FrontendPort": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendPorts/appGatewayFrontendPort2')]"
                        },
                        "Protocol": "Http",
                        "SslCertificate": null,
                        "HostName": "[parameters('hostName1')]",
                        "RequireServerNameIndication": "false"
                    }
                },
        {
                    "name": "appGatewayHttpListener-Group2",
                    "properties": {
                        "FrontendIPConfiguration": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendIPConfigurations/appGatewayFrontendIP')]"
                        },
                        "FrontendPort": {
                            "Id": "[concat(variables('applicationGatewayID'), '/frontendPorts/appGatewayFrontendPort2')]"
                        },
                        "Protocol": "Http",
                        "SslCertificate": null,
                        "HostName": "[parameters('hostName2')]",
                        "RequireServerNameIndication": "false"
                    }
                }
            ],
            "requestRoutingRules": [
                {
                    "Name": "Group1-SSL",
                    "properties": {
                        "RuleType": "Basic",
                        "httpListener": {
                            "id": "[concat(variables('applicationGatewayID'), '/httpListeners/appGatewayHttpsListener-Group1')]"
                        },
                        "backendAddressPool": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendAddressPools/appGatewayBackendPool1')]"
                        },
                        "backendHttpSettings": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendHttpSettingsCollection/appGatewayBackendHttpSettings')]"
                        }
                    }
                },
                {
                    "Name": "Group2-SSL",
                    "properties": {
                        "RuleType": "Basic",
                        "httpListener": {
                            "id": "[concat(variables('applicationGatewayID'), '/httpListeners/appGatewayHttpsListener-Group2')]"
                        },
                        "backendAddressPool": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendAddressPools/appGatewayBackendPool2')]"
                        },
                        "backendHttpSettings": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendHttpSettingsCollection/appGatewayBackendHttpSettings')]"
                        }
                    }
                },
        {
                    "Name": "Group2-www",
                    "properties": {
                        "RuleType": "Basic",
                        "httpListener": {
                            "id": "[concat(variables('applicationGatewayID'), '/httpListeners/appGatewayHttpListener-Group1')]"
                        },
                        "backendAddressPool": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendAddressPools/appGatewayBackendPool1')]"
                        },
                        "backendHttpSettings": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendHttpSettingsCollection/appGatewayBackendHttpSettings')]"
                        }
                    }
                },
        {
                    "Name": "Group1-www",
                    "properties": {
                        "RuleType": "Basic",
                        "httpListener": {
                            "id": "[concat(variables('applicationGatewayID'), '/httpListeners/appGatewayHttpListener-Group2')]"
                        },
                        "backendAddressPool": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendAddressPools/appGatewayBackendPool2')]"
                        },
                        "backendHttpSettings": {
                            "id": "[concat(variables('applicationGatewayID'), '/backendHttpSettingsCollection/appGatewayBackendHttpSettings')]"
                        }
                    }
                }
            ]
        }
    }
]
}

如您所见,我将 GWSubnet1 指定为 App Gateway 子网。我的后端 IP 位于同一虚拟网络下的 VMnet1 子网中。当我部署它失败说它不能删除 VMnet1。VMNet1 只是间接引用为后端 IP,所以它为什么要尝试删除它。根据 Azure 的部署规则,GWSubnet1 是一个未使用的空子网。

如果我使用 GUI,我可以创建网关并选择 GWSubnet1。然而,使用 GUI 将主机名放入列表器的高级功能不是一个选项,因此不会让您使用相同的前端端口创建多个列表器。我尝试使用 GUI,然后使用以下命令通过 Poweshell(版本 3.0.0)添加侦听器

$hostname = "example1.foo.com"
$listnername = "group2-az"
$appgwname = "PortalGateway"
$rmname = "myrmg"
$feipname = "appGatewayFrontendIP" 
$fepname = "appGatewayFrontendPort"
$behttpname = "appGatewayBackendHttpSettings"


$appgw = Get-AzureRmApplicationGateway -Name $appgwname -ResourceGroupName      $rmname
$bepool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $appgw -Name "appGatewayBackendPool"
$behttp = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $appgw -Name $behttpname



$fipc = Get-AzureRmApplicationGatewayFrontendIPConfig -Name $feipname -ApplicationGateway $appgw
$fep = Get-AzureRmApplicationGatewayFrontendPort -Name $fepname -ApplicationGateway $appgw
 $result = Add-AzureRmApplicationGatewayHttpListener -ApplicationGateway $appgw -Name "appGatewayHttpListenerGroup1" -Protocol Http -FrontendIPConfiguration $fipc -FrontendPort $fep -HostName $hostname -RequireServerNameIndication false

然而,似乎发生的是它没有添加侦听器,它只是修改了在您通过 GUI 创建应用程序网关时创建的现有默认侦听器。无论我选择什么名字作为听众,它都会这样做。

我知道部署模板有效,因为我可以创建一个新的空资源组并将其部署在其中并进行部署。我似乎无法将其部署在现有虚拟机的地方。这样做的正确方法是什么?

4

1 回答 1

1

ARM 模板是声明性的,在您的模板中只有一个子网。如果您部署该模板,ARM 将尝试完全按照您的定义来制作它 = 它会尝试删除该子网中未自行定义的任何子网。这就是你错误的原因。ARM 尝试删除您的 VMnet1,但只要它有与之关联的 NIC,它就无法执行此操作。

在此处查看文档: 使用资源管理器模板和 Azure PowerShell 部署资源

对您来说有趣的部分是:

增量和完整部署

部署资源时,您可以指定部署是增量更新还是完整更新。默认情况下,资源管理器将部署作为资源组的增量更新来处理。

通过增量部署,资源管理器:

  • 保留资源组中存在但未在模板中指定的未更改资源
  • 添加模板中指定但资源组中不存在的资源
  • 不会以模板中定义的相同条件重新配置资源组中存在的资源
  • 重新配置已更新模板中设置的现有资源

完成部署后,资源管理器:

  • 删除资源组中存在但未在模板中指定的资源
  • 添加模板中指定但资源组中不存在的资源
  • 不会以模板中定义的相同条件重新配置资源组中存在的资源
  • 重新配置已更新模板中设置的现有资源

要解决您的问题,您需要使子网配置完全代表您现有的设置,或者您手动创建新子网并且不要在模板中定义 vnet。

如果您手动创建子网,您可以在模板中引用现有的 vnet 和子网,如下所示:

"parameters": {
    "existingVirtualNetworkName": {
        "type": "string"
    },
    "existingVirtualNetworkResourceGroup": {
        "type": "string"
    },
    "existingSubnet1Name": {
        "type": "string"
    },
    "existingSubnet2Name": {
        "type": "string"
    },
}
"variables": {
    "existingVnetID": "[resourceId(parameters('existingVirtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks', parameters('existingVirtualNetworkName'))]",
    "existingSubnet1Ref": "[concat(variables('existingVnetID'),'/subnets/', parameters('existingSubnet1Name'))]",
    "existingSubnet2Ref": "[concat(variables('existingVnetID'),'/subnets/', parameters('existingSubnet2Name'))]",
}

通过参数传递现有 RessourceGroup、Vnet 和 Subnetnames 后,您可以使用变量“existingSubnet1Name”指向正确的 ID。

神奇之处在于 [resourceId()] 函数的可选参数:[subscriptionId]、[resourceGroupName]。

resourceId ([subscriptionId], [resourceGroupName], resourceType, resourceName1, [resourceName2]...)

文档:模板函数

于 2017-02-17T13:14:44.493 回答