简体   繁体   English

如何更改现有 Azure 应用程序网关的 VNet 和子网?

[英]How to change VNet and Subnet of an existing Azure Application Gateway?

Is it possible to move an already setup app gateway from one subnet to another?是否可以将已设置的应用程序网关从一个子网移动到另一个子网?

As of now haven't seen any way from the portal to do so.截至目前还没有看到任何从门户网站这样做的方法。

You can use this script to change the VNet or Subnet.您可以使用此脚本更改 VNet 或子网。 Please test it to see if it meets your needs, before applying it to a production gateway.在将其应用于生产网关之前,请对其进行测试以查看它是否满足您的需求。 Also, take into account that there will be some downtime during the change.此外,请考虑到更改期间会有一些停机时间。

#Login to Azure RM
Login-AzureRmAccount

#Get the Application Gateway config
$gw=Get-AzureRmApplicationGateway -Name GatewayName -ResourceGroupName RGName

#Set the new virtual network and store the config into a new variable
$gw2=Set-AzureRmApplicationGatewayIPConfiguration -SubnetId "/subscriptions/999999-9915-4b1c-accf-0c984bed2311/resourceGroups/RGName/providers/Microsoft.Network/virtualNetworks/NewVirtualNetwork/subnets/default" -ApplicationGateway $gw -Name $gw.GatewayIPConfigurations.name

#Stop the Gateway (you can't change the virtual network / subnet if the Gateway is running)
Stop-AzureRmApplicationGateway -ApplicationGateway $gw

#Set the new config
Set-AzureRmApplicationGateway -ApplicationGateway $gw2

The accepted answer by andresm53 is excellent. andresm53 接受的答案非常好。
However, as the PowerShell AzureRm module is being phased out in favor of the newer Az module, here is an Az version (with a slight improvement to save from having to look up the subnet id in order to paste it into the code).但是,由于 PowerShell AzureRm 模块正在逐步淘汰,取而代之的是更新的 Az 模块,这里有一个 Az 版本(略有改进,无需查找子网 ID 以将其粘贴到代码中)。
This is based, in addition to andresm53's code, also on an example in the MS docs .这不仅基于 andresm53 的代码,还基于MS docs 中的示例。

### Fill in your values ###
$GatewayResourceGroupName = "MyRG1"
$GatewayName = "MyGw"
$VnetResourceGroupName = "MyRG2"  #may or may not be the same as $GatewayResourceGroupName
$VNetName = "MyVNet"
$SubnetName = "Subnet1"
###########################

$AppGw = Get-AzApplicationGateway -Name $GatewayName -ResourceGroupName $GatewayResourceGroupName
Stop-AzApplicationGateway -ApplicationGateway $AppGw
$VNet = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $VnetResourceGroupName
$Subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $VNet
$AppGw = Set-AzApplicationGatewayIPConfiguration -ApplicationGateway $AppGw -Name  $AppGw.GatewayIPConfigurations[0].Name -Subnet $Subnet
Set-AzApplicationGateway -ApplicationGateway $AppGw
Start-AzApplicationGateway -ApplicationGateway $AppGw

I did it using azure cli, it's necessary to perform some steps:我是用 azure cli 做的,有必要执行一些步骤:

  1. Stop the application gateway停止应用程序网关
  2. Change the subnet更改子网
  3. Start the application gateway (this will take some minutes)启动应用程序网关(这将需要几分钟时间)

Using azure cli:使用 azure cli:

1. stopping application gateway 1.停止应用网关

az network application-gateway stop --subscription YOUR_SUBSCRIPTION_NAME --resource-group YOUR_APP_GATEWAY_RESOURCE_GROUP --name YOUR_APP_GATEWAY_NAME

2. Change the subnet. 2. 更改子网。

2.1 At this point, you need to know your current vnet data, given by next command 2.1 此时,你需要知道你当前的vnet数据,由next命令给出

az network application-gateway show \
  --subscription YOUR_SUBSCRIPTION_NAME \
  --resource-group YOUR_APP_GATEWAY_RESOURCE_GROUP \
  --name YOUR_APP_GATEWAY_NAME

The output we need is at JSON section gatewayIpConfigurations我们需要的输出在 JSON 部分 gatewayIpConfigurations

[
    {
      "etag": "REDACTED",
      "id": "REDACTED",
      "name": "REDACTED",
      "provisioningState": "REDACTED",
      "resourceGroup": "REDACTED",
      "subnet": {
        "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/virtualNetworks/YOUR_CURRENT_VNET/subnets/YOUR_CURRENT_SUBNET",
        "resourceGroup": "REDACTED"
      },
      "type": "Microsoft.Network/applicationGateways/gatewayIPConfigurations"
    }
  ]

2.2 To change the subnet, you need to modify YOUR_CURRENT_SUBNET by your new subnet 2.2 要更改子网,您需要通过您的新子网修改YOUR_CURRENT_SUBNET

[
    {
      "etag": "REDACTED",
      "id": "REDACTED",
      "name": "REDACTED",
      "provisioningState": "REDACTED",
      "resourceGroup": "REDACTED",
      "subnet": {
        "id": "/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/virtualNetworks/YOUR_CURRENT_VNET/subnets/YOUR_NEW_SUBNET",
        "resourceGroup": "REDACTED"
      },
      "type": "Microsoft.Network/applicationGateways/gatewayIPConfigurations"
    }
  ]

2.3 Copy the previous subnet id, put the proper subnet name you want now, and update it 2.3 复制之前的子网id,输入你现在想要的正确子网名,并更新

az network application-gateway update \
  --subscription YOUR_SUBSCRIPTION_NAME \
  --resource-group YOUR_APP_GATEWAY_RESOURCE_GROUP \
  --name YOUR_APP_GATEWAY_NAME \
  --set gatewayIpConfigurations[0].subnet.id='/subscriptions/REDACTED/resourceGroups/REDACTED/providers/Microsoft.Network/virtualNetworks/YOUR_CURRENT_VNET/subnets/YOUR_NEW_SUBNET'

3. Start the application gateway 3.启动应用网关

az network application-gateway start \
  --subscription YOUR_SUBSCRIPTION_NAME \
  --resource-group YOUR_APP_GATEWAY_RESOURCE_GROUP \
  --name YOUR_APP_GATEWAY_NAME

You cannot change Subnet/VNet association on a running Gateway.您无法更改正在运行的网关上的子网/VNet 关联。 It needs to be in stopped state first.它需要先处于停止状态。 Also the VIP on the Gateway would change once it is started post update.此外,网关上的 VIP 会在更新后启动后更改。 Subnet move can be done via PowerShell/CLI and is not supported in portal currently.子网移动可以通过 PowerShell/CLI 完成,目前在门户中不受支持。

It will affect the external IP address.它会影响外部 IP 地址。 since the app gateway have to use dynamic ip address.因为应用程序网关必须使用动态 IP 地址。 once the app gateway stop.一旦应用程序网关停止。 the external IP will release and you will have new one after it's start up.外部IP将释放,启动后您将拥有新的IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM