开始使用 Azure Virtual Network Manager,通过 Bicep 管理所有虚拟网络的连接。
在本快速入门中,部署三个虚拟网络,并使用 Azure Virtual Network Manager 创建网格网络拓扑。 然后验证是否应用了连接配置。
Bicep 文件模块
此示例的 Bicep 解决方案分为多个模块,用于在资源组和订阅范围内启用部署。 下面详述的文件部分是虚拟网络管理器的唯一组件。 除了下面详述的部分外,该解决方案还会部署虚拟网络、用户分配的标识和角色分配。
Virtual Network Manager、网络组、连接配置
虚拟网络管理器
@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'
}
}
部署脚本
为了将配置部署到目标网络组,需使用部署脚本调用 Deploy-AzNetworkManagerCommit
PowerShell 命令。 部署脚本需要具有足够权限的标识才能针对虚拟网络管理器执行 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
,不部署策略。
下载 Bicep 解决方案
- 通过此链接下载 MSPNP 存储库的 Zip 存档
- 提取下载的 Zip 文件,然后在终端中导航到
solutions/avnm-mesh-connected-group/bicep
目录。
或者,可以使用 git
克隆具有 git clone https://github.com/mspnp/samples.git
的存储库
连接到 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:[必需] 此参数指定将部署虚拟网络管理器和示例虚拟网络的资源组的名称。
- location:[必需] 此参数指定要部署的资源的位置。
-
networkGroupMembershipType:[可选] 此参数指定要部署的网络组成员身份的类型。 默认值为
static
,但可以通过指定dynamic
来使用动态组成员身份。
注释
选择动态组成员身份后,会部署 Azure Policy 来管理成员身份,这需要更多权限。
$templateParameterObject = @{
'location' = '<resourceLocation>'
'resourceGroupName' = '<newOrExistingResourceGroup>'
}
New-AzSubscriptionDeployment -TemplateFile ./main.bicep -Location <deploymentLocation> -TemplateParameterObject $templateParameterObject
验证配置部署
使用每个虚拟网络的“网络管理器”部分来验证是否已部署配置:
转到 vnet-learn-prod-{location}-spoke001 虚拟网络。
在“设置”下选择“网络管理器”。
在“连接配置”选项卡上,验证列表中是否显示“cc-learn-prod-{location}-mesh001”。
在 vnet-learn-prod-{location}-spoke004 中重复上述步骤,你应该会看到 vnet-learn-prod-{location}-spoke004 从连接配置中排除。
清理资源
如果你不再需要 Azure Virtual Network Manager 和关联的虚拟网络,可将其移除:只需删除资源组及其资源即可。
- 在 Azure 门户中,浏览到你的资源组 - resource-group。
- 选择“resource-group”,然后选择“删除资源组”。
- 在“删除资源组”窗口中的文本框内输入“resource-group”来确认你要删除,然后选择“删除”。
- 如果使用了动态网络组成员身份,请通过导航到门户中的订阅并选择“策略”来删除部署的 Azure Policy 定义和分配。 在“策略”中,找到名为 的
AVNM quickstart dynamic group membership Policy
并将其删除,然后对名为 的AVNM quickstart dynamic group membership Policy
执行相同的操作。
后续步骤
现在,你已创建了 Azure Virtual Network Manager 实例,请了解如何使用安全管理配置来阻止网络流量: