快速入门:使用 Bicep 通过 Azure 虚拟网络管理器创建网状网络拓扑

开始使用 Azure Virtual Network Manager,通过 Bicep 管理所有虚拟网络的连接。

在本快速入门中,部署三个虚拟网络,并使用 Azure Virtual Network Manager 创建网格网络拓扑。 然后验证是否应用了连接配置。

该示意图显示了使用 Azure Virtual Network Manager 为网状虚拟网络拓扑部署的资源。

Bicep 文件模块

此示例的 Bicep 解决方案被拆分为多个模块,以支持在资源组和订阅两个作用域级别进行部署。 以下列表中详述的文件部分是虚拟网络管理器的唯一组件。 除了以下列表中详述的部分外,解决方案还部署虚拟网络、用户分配的标识和角色分配。

虚拟网络管理器、网络组、连接配置

虚拟网络管理器

@description('This is the Azure Virtual Network Manager which will be used to implement the connected group for inter-vnet connectivity.')
resource networkManager 'Microsoft.Network/networkManagers@2022-09-01' = {
  name: 'vnm-learn-prod-${location}-001'
  location: location
  properties: {
    networkManagerScopeAccesses: [
      'Connectivity'
    ]
    networkManagerScopes: {
      subscriptions: [
        '/subscriptions/${subscription().subscriptionId}'
      ]
      managementGroups: []
    }
  }
}

网络组

此解决方案支持创建静态成员身份网络组或动态成员身份网络组。 静态成员身份网络组按虚拟网络 ID 指定其成员。

静态成员身份网络组

@description('This is the static network group for the all VNETs.')
resource networkGroupSpokesStatic 'Microsoft.Network/networkManagers/networkGroups@2022-09-01' = if (networkGroupMembershipType == 'static') {
  name: 'ng-learn-prod-${location}-static001'
  parent: networkManager
  properties: {
    description: 'Network Group - Static'
  }

  // add spoke vnets A, B, and C to the static network group
  resource staticMemberSpoke 'staticMembers@2022-09-01' = [for spokeMember in spokeNetworkGroupMembers: if (contains(groupedVNETs,last(split(spokeMember,'/')))) {
    name: 'sm-${(last(split(spokeMember, '/')))}'
    properties: {
      resourceId: spokeMember
    }
  }]

  resource staticMemberHub 'staticMembers@2022-09-01' = {
    name: 'sm-${(toLower(last(split(hubVnetId, '/'))))}'
    properties: {
      resourceId: hubVnetId
    }
  }
}

动态成员身份网络组

@description('This is the dynamic group for all VNETs.')
resource networkGroupSpokesDynamic 'Microsoft.Network/networkManagers/networkGroups@2022-09-01' = if (networkGroupMembershipType == 'dynamic') {
  name: 'ng-learn-prod-${location}-dynamic001'
  parent: networkManager
  properties: {
    description: 'Network Group - Dynamic'
  }
}

连接配置

连接配置将网络组与指定的网络拓扑相关联。

@description('This connectivity configuration defines the connectivity between VNETs using Direct Connection. The hub will be part of the mesh, but gateway routes from the hub will not propagate to spokes.')
resource connectivityConfigurationMesh 'Microsoft.Network/networkManagers/connectivityConfigurations@2022-09-01' = {
  name: 'cc-learn-prod-${location}-mesh001'
  parent: networkManager
  properties: {
    description: 'Mesh connectivity configuration'
    appliesToGroups: [
      {
        networkGroupId: (networkGroupMembershipType == 'static') ? networkGroupSpokesStatic.id : networkGroupSpokesDynamic.id
        isGlobal: 'False'
        useHubGateway: 'False'
        groupConnectivity: 'DirectlyConnected'
      }
    ]
    connectivityTopology: 'Mesh'
    deleteExistingPeering: 'True'
    hubs: []
    isGlobal: 'False'
  }
}

部署脚本

若要将配置部署到目标网络组,请使用调用 PowerShell 命令的 Deploy-AzNetworkManagerCommit部署脚本。 部署脚本需要一个具有足够权限的身份,才能对虚拟网络管理器执行 PowerShell 脚本。 Bicep文件创建用户托管标识,并向其授予目标资源组的“参与者”角色。 有关部署脚本和关联标识的详细信息,请参阅 在 ARM 模板中使用部署脚本

@description('Create a Deployment Script resource to perform the commit/deployment of the Network Manager connectivity configuration.')
resource deploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: deploymentScriptName
  location: location
  kind: 'AzurePowerShell'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${userAssignedIdentityId}': {}
    }
  }
  properties: {
    azPowerShellVersion: '8.3'
    retentionInterval: 'PT1H'
    timeout: 'PT1H'
    arguments: '-networkManagerName "${networkManagerName}" -targetLocations ${location} -configIds ${configurationId} -subscriptionId ${subscription().subscriptionId} -configType ${configType} -resourceGroupName ${resourceGroup().name}'
    scriptContent: '''
    param (
      # AVNM subscription id
      [parameter(mandatory=$true)][string]$subscriptionId,

      # AVNM resource name
      [parameter(mandatory=$true)][string]$networkManagerName,

      # string with comma-separated list of config ids to deploy. ids must be of the same config type
      [parameter(mandatory=$true)][string[]]$configIds,

      # string with comma-separated list of deployment target regions
      [parameter(mandatory=$true)][string[]]$targetLocations,

      # configuration type to deploy. must be either connectivity or securityadmin
      [parameter(mandatory=$true)][ValidateSet('Connectivity','SecurityAdmin')][string]$configType,

      # AVNM resource group name
      [parameter(mandatory=$true)][string]$resourceGroupName
    )
  
    $null = Login-AzAccount -Identity -Subscription $subscriptionId
  
    [System.Collections.Generic.List[string]]$configIdList = @()  
    $configIdList.addRange($configIds) 
    [System.Collections.Generic.List[string]]$targetLocationList = @() # target locations for deployment
    $targetLocationList.addRange($targetLocations)     
    
    $deployment = @{
        Name = $networkManagerName
        ResourceGroupName = $resourceGroupName
        ConfigurationId = $configIdList
        TargetLocation = $targetLocationList
        CommitType = $configType
    }
  
    try {
      Deploy-AzNetworkManagerCommit @deployment -ErrorAction Stop
    }
    catch {
      Write-Error "Deployment failed with error: $_"
      throw "Deployment failed with error: $_"
    }
    '''
    }
}

动态网络组成员身份策略

将部署配置为使用dynamic网络组成员资格时,该解决方案还会部署 Azure Policy 定义和分配。 以下示例显示了策略定义。

@description('This is a Policy definition for dynamic group membership')
resource policyDefinition 'Microsoft.Authorization/policyDefinitions@2021-06-01' = {
  name: uniqueString(networkGroupId)
  properties: {
    description: 'AVNM quickstart dynamic group membership Policy'
    displayName: 'AVNM quickstart dynamic group membership Policy'
    mode: 'Microsoft.Network.Data'
    policyRule: {
      if: {
        allof: [
          {
            field: 'type'
            equals: 'Microsoft.Network/virtualNetworks'
          }
          {
            // virtual networks must have a tag where the key is '_avnm_quickstart_deployment'
            field: 'tags[_avnm_quickstart_deployment]'
            exists: true
          }
          {
            // virtual network ids must include this sample's resource group ID - limiting the chance that dynamic membership impacts other vnets in your subscriptions
            field: 'id'
            like: '${subscription().id}/resourcegroups/${resourceGroupName}/*'
          }
        ]
      }
      then: {
        // 'addToNetworkGroup' is a special effect used by AVNM network groups
        effect: 'addToNetworkGroup'
        details: {
          networkGroupId: networkGroupId
        }
      }
    }
  }
}

部署 Bicep 解决方案

部署先决条件

  • 拥有有效订阅的 Azure 帐户。 创建账户
  • 在目标订阅范围内创建策略定义和策略分配的权限。 使用部署参数 networkGroupMembershipType=Dynamic 部署网络组成员身份所需的策略资源时,需要具备这些权限。 默认值为 static,这不会部署任何策略。
  • 此解决方案中的所有资源均在Azure示例GitHub存储库中提供。 可以从存储库下载Bicep解决方案,或将存储库克隆到本地计算机。

下载 Bicep 解决方案

  1. 在此 链接中下载示例存储库的 ZIP 存档。
  2. 提取下载的 ZIP 文件。 在终端中,进入已解压的 avnm-mesh-connected-group 目录。 此解决方案的Bicep文件位于bicep子目录中。

或者,你可以使用 git 克隆仓库:

git clone https://github.com/Azure-Samples/avnm-mesh-connected-group
cd avnm-mesh-connected-group

连接到 Azure

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

要开始配置,请登录到 Azure 帐户:

Connect-AzAccount -Environment AzureChinaCloud

然后,连接到你的订阅:

Set-AzContext -Subscription <subscription name or id>
安装 Azure PowerShell 模块

使用此命令安装最新的 Az.Network Azure PowerShell 模块:

 Install-Module -Name Az.Network -RequiredVersion 5.3.0

部署参数

  • resourceGroupName:[必需] 要在其中部署虚拟网络管理器和示例虚拟网络的资源组的名称。
  • 位置:[必需] 要部署的资源的位置。
  • networkGroupMembershipType:[可选] 要部署的网络组成员身份的类型。 默认值为 static,但对于动态组成员身份,可以使用 dynamic

注释

选择动态组成员身份会部署Azure Policy来管理成员身份,这需要更多的权限

具有静态网络组成员身份的默认部署

New-AzSubscriptionDeployment -Name avnm-mesh-connected-group -Location <deploymentLocation> -TemplateFile ./bicep/main.bicep -resourceGroupName <newOrExistingResourceGroup>

使用动态网络组成员身份进行部署

若要使用 Azure Policy 动态管理网络组成员身份,请包含值为 networkGroupMembershipType 的部署参数 dynamic

New-AzSubscriptionDeployment -Name avnm-mesh-connected-group -Location <deploymentLocation> -TemplateFile ./bicep/main.bicep -resourceGroupName <newOrExistingResourceGroup> -networkGroupMembershipType dynamic

验证配置部署情况

使用每个虚拟网络的“网络管理器”部分来验证是否已部署配置:

  1. 转到 vnet-learn-prod-{location}-spoke001 虚拟网络。

  2. 在“设置”下选择“网络管理器”。

  3. 在“连接配置”选项卡上,验证列表中是否显示“cc-learn-prod-{location}-mesh001”。

    虚拟网络中列出的连接配置的屏幕截图。

  4. vnet-learn-prod-{location}-spoke004 中重复上述步骤,你应该会看到 vnet-learn-prod-{location}-spoke004 从连接配置中排除。

清理资源

如果不再需要Azure Virtual Network Manager和关联的虚拟网络,请通过删除资源组及其资源将其删除。

  1. Azure 门户中,浏览至你的资源组 - resource-group
  2. 选择“resource-group”,然后选择“删除资源组”
  3. 删除资源组中,通过在文本框中输入 resource-group 确认要删除,然后选择 删除
  4. 如果您使用了动态网络组成员资格,请在门户中转到“订阅”,然后选择策略,删除已部署的 Azure Policy 定义和分配。 在“策略”中,找到名为AVNM quickstart dynamic group membership Policy并将其删除,然后对名为AVNM quickstart dynamic group membership Policy执行相同的操作。

后续步骤

创建Azure Virtual Network Manager实例后,了解如何使用安全管理员配置阻止网络流量: