使用 Azure IPAM 池自动执行虚拟网络 IPAM

Important

目前,Azure Virtual Network Manager中的 IP 地址管理(IPAM)功能已在Azure Virtual Network Manager可用的所有区域中正式发布。

Azure Virtual Network Manager中的IPAM池帮助你管理虚拟网络的IP地址空间。 此功能有助于避免地址空间重叠,并确保使用正确的 IP 地址范围创建 VNet。

本文提供了一个示例PowerShell脚本,演示如何创建多个VNet,将现有VNet与IPAM池关联,以及将VNet与IPAM池解除关联。

先决条件

  • 拥有有效订阅的 Azure 帐户。 创建账户
  • Azure PowerShell 本地安装的。
  • 创建 IPAM 池的虚拟网络管理器实例。 有关详细信息,请参阅 创建虚拟网络管理器创建 IPAM 池
  • 要在其中创建 VNet 的现有资源组。 建议使用与虚拟网络管理器实例相同的资源组,以便更好地组织和管理。

查看示例脚本

该脚本位于 GitHub 上的 Azure 示例存储库中。 可以从以下链接查看和下载脚本: automate-vnet-ip-address-management.ps1

在脚本顶部设置位置、资源组、订阅、IPAM 资源池 ID 和 IP 地址数量后,它依次执行三项操作:

  1. 批量从池中创建虚拟网络。 对于10个虚拟网络中的每个,脚本会用 New-AzVirtualNetworkSubnetConfig 构建子网配置,并用 New-AzVirtualNetwork创建虚拟网络,并将IPAM池的引用传递给 -IpamPoolPrefixAllocation 两者的参数。 池分配地址空间。
  2. 批量将现有的虚拟网络从池中分离出来。 该脚本通过 Get-AzVirtualNetwork 检索资源组中的虚拟网络,清除 IpamPoolPrefixAllocations 每个虚拟网络的地址空间及其子网,并用 Set-AzVirtualNetwork 保存变更。
  3. 批量将这些虚拟网络重新关联到池中。 脚本会在相同的地址空间和子网上恢复IPAM池引用,并保存变更。Set-AzVirtualNetwork

每个操作都以作业形式运行更新,等待每个作业完成后再开始下一个,这样API调用才能按顺序完成,无需重试。

示例脚本

# Set the variables for the script to your environment

$location = "<your resource location>" # e.g. "East US", "West Europe", etc.
$rgname = "<your resource group>" # use RG name as "*" to fetch all VNets from all RGs within subscription
$sub = "<your subscription id>" # use subscription id as "*" to fetch all VNets from all subscriptions within tenant
$ipamPoolARMId = "<your ipam pool ARM ID>" # e.g. "/subscriptions/<your subscription id>/resourceGroups/<your resource group>/providers/Microsoft.Network/ipamPools/<your ipam pool name>"
$numberIPaddresses = "8" # Number of IP addresses to allocate from the IPAM Pool. This should be a valid number based on your IPAM Pool configuration.

# Select your subscription
Set-AzContext -Subscription $sub

# Set the 
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = [PSCustomObject]@{
    Id = $ipamPoolARMId 
    NumberOfIpAddresses = $numberIPaddresses 
}

# Create 10 VNets using ipamPool reference - Change the number of VNets to create as needed in the for loop below
for ($i = 0; $i -lt 10; $i++) {
    $subnetName = "defaultSubnet"
    $vnetName = "bulk-ipam-vnet-$i"
    $subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -DefaultOutboundAccess $false
    $job = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -Subnet $subnet -AsJob
    $job | Wait-Job
    $actual = $job | Receive-Job
}
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")

# fetch all virtual networks from a resource group
$vnetList = Get-AzVirtualNetwork -ResourceGroupName $rgname

# bulk disassociation update
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = $null
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
    $vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
    foreach ($subnet in $vnetList[$i].Subnets) {
        $subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
    }
    $job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
    $job | Wait-Job
    $actual = $job | Receive-Job
}
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")

# bulk association update
Write-Output "Starting bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = [PSCustomObject]@{
    Id = $ipamPoolARMId
    NumberOfIpAddresses = $numberIPaddresses
}
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
    $vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
    foreach ($subnet in $vnetList[$i].Subnets) {
        $subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
    }
    $job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
    $job | Wait-Job
    $actual = $job | Receive-Job
}
Write-Output "Finished bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")

登录到 Azure 帐户,然后选择订阅

如果在本地使用 Azure PowerShell,请登录到 Azure 帐户:

# Sign in to your Azure account
Connect-AzAccount -Environment AzureChinaCloud

# Select your subscription
Set-AzContext -Subscription <subscriptionId>

下载脚本

将脚本下载到本地目录或首选 PowerShell 环境。 可以使用以下命令直接从 Azure 示例存储库下载脚本:

# Download the script
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/Azure-Samples/azure-docs-powershell-samples/main/virtual-network-manager/automate-vnet-ip-address-management.ps1" -OutFile "automate-vnet-ip-address-management.ps1"

更新脚本变量

下载脚本后,在首选 PowerShell 编辑器中打开该脚本,并更新以下变量以匹配环境:

变量 说明
$location 输入要在其中创建 VNet(如 美国东部)的 Azure 区域。
$rgname 输入要在其中创建 VNet 的资源组的名称。 可以使用 "*" 提取订阅中所有资源组的所有 VNet。
$sub 输入要在其中创建 VNet 的订阅 ID。 可以使用 "*" 提取租户中所有订阅的所有 VNet。
$ipamPoolARMId 你想要用于 VNet 的 IPAM 池的 Azure 资源管理器 ID,如 "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Network/networkManagers/<networkManagerName>/ipamPools/<ipAddressPoolName>"。 你可以从 Azure 门户中池的 JSON 视图复制此资源 ID。
$numberIPaddresses 要从 IPAM 池分配的 IP 地址数。 这应该是基于 IPAM 池配置的有效数字。

对于 Visual Studio Code,请输入以下命令在编辑器中打开脚本:

# Open the script in Visual Studio Code
code ./automate-vnet-ip-address-management.ps1

在运行脚本之前,请记得保存脚本。

运行脚本

更新脚本变量后,在你的 PowerShell 环境中运行脚本。 脚本会用IPAM池引用创建10个VNet,将现有VNet与IPAM池分离,然后将它们重新关联到IPAM池。

# Run the script
./automate-vnet-ip-address-management.ps1

示例输出


PS /home/michael/clouddrive/avnm-script> ./automate-vnet-ip-address-management.ps1

   Tenant: aaaabbbb-0000-cccc-1111-dddd2222eeee

SubscriptionName      SubscriptionId                       Account   Environment
----------------      --------------                       -------   -----------
Azure Subscription    aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e user@azure AzureCloud
Starting creation of new VNets with IpamPool reference at: 
18:49:06

Starting creation of new VNets with IpamPool reference at: 
18:49:37
Starting bulk disassociation for existing VNets at: 
18:49:37

Starting bulk disassociation for existing VNets at: 
18:49:59
Starting bulk association for existing VNets at: 
18:49:59

Finished bulk association for existing VNets at: 
18:50:32

PS /home/michael/clouddrive/avnm-script> 

注释

该脚本以同步方式运行,以确保没有 API 调用失败。 因此,脚本可能需要一些时间才能完成,具体取决于要创建的和管理 VNet 的数量。

验证虚拟网络

若要验证 VNet 是否已创建并与 IPAM 池关联,可以使用以下命令:

# List all VNets in the specified resource group
Get-AzVirtualNetwork -ResourceGroupName $rgname | Select-Object Name, Location, AddressSpace, @{Name = "IpamPoolPrefixAllocations"; Expression = { $_.AddressSpace.IpamPoolPrefixAllocations }}

此命令显示指定资源组中每个虚拟网络的名称、位置、地址空间和 IPAM 池前缀分配。 应会看到使用 IPAM 池引用创建的 VNet。

后续步骤