2016-10-03 166 views
0

我一直在嘗試將Azure應用程序網關部署到前端應用程序,我在現有的虛擬機上並將主機名用於池選擇。我開始從混帳https://github.com/Azure/azure-quickstart-templates/tree/master/201-application-gateway-multihosting該模板基礎上,文章https://github.com/Azure/azure-content/blob/master/articles/application-gateway/application-gateway-multi-site-overview.md如何部署帶有虛擬機的應用程序網關

這裏是體改tempate我用

{ 
"$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作爲應用網關子網。我的後端IP位於同一虛擬網絡下的VMnet1子網中。當我部署失敗時說它不能刪除VMnet1。 VMNet1僅被間接引用爲後端IP,爲什麼它會嘗試刪除它。根據Azure的部署規則,GWSubnet1是未使用的空子網。

如果我使用GUI,我可以創建網關並選擇GWSubnet1。但是,使用GUI將主機名放入列表器的高級功能不是一個選項,因此不會讓您使用相同的前端端口創建多個列表器。我嘗試使用GUI和,然後通過Poweshell加入listners(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創建appgateway時創建的。無論我選擇什麼名字作爲聽衆,它都會這樣做。

我知道部署模板工程,我可以創造一個新的空資源組,並在那裏部署它,並將其部署。我似乎無法讓它部署在有現有VM的地方。什麼是正確的方法來做到這一點?

+0

你嘗試到你的後端子網添加到模板?我有同樣的問題。據我瞭解,VNET需要在模板中包含所有子網。 –

回答

1

ARM模板是聲明性的,在您的模板中只有一個子網。如果您部署該模板,ARM將嘗試完全按照您的定義進行設置=它會嘗試刪除該子網中未使用自身定義的任何子網。 這就是你錯誤的原因。 ARM試圖刪除你的VMnet1,只要它有與之關聯的NIC,它就不能這麼做。

檢查這裏的文檔: Deploy resources with Resource Manager templates and Azure PowerShell

你最有趣的部分是:

增量和完整部署

在部署你的資源,你指定的部署或者是一個增量更新或完整更新。默認情況下,資源管理器將部署處理爲資源組的增量更新。

隨着增量部署,資源管理器:

  • 留下存在的資源組中,但在模板
  • 沒有規定不變資源補充說,在模板中指定的,但做資源不存在於資源組中
  • 不會根據模板中定義的相同條件重新配置存在於資源組中的資源
  • reprovisions,在模板

有了完整的部署已更新的設置,資源管理器現有資源:

  • 刪除存在的資源組中,但在未指定資源模板
  • 增加在模板中指定但資源中不存在的資源組
  • 不另配資源存在資源組在模板中定義相同的條件下
  • reprovisions,在模板

已經更新設置現有資源來解決你的問題,你需要使子網配置完全代表您的現有設置,或者手動創建新的子網,並且不要在模板中定義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,互聯星空和Subnetnames通過參數,你可以只使用變量「existingSubnet1Name後「指向正確的ID。

神奇的是[resourceId()]函數可選參數:[subscriptionId],[resourceGroupName]。

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

文檔:Template functions