設定虛擬網路對等互連
在此程式中,您會使用 Windows PowerShell 來建立兩個虛擬網路,每個虛擬網路各有一個子網路。 然後,您會設定兩個虛擬網路之間的對等互連,以啟用它們之間的連線。
重要
請記得更新環境的屬性。
步驟 1:建立第一個虛擬網路
在此步驟中,您會使用 Windows PowerShell 尋找 HNV 提供者邏輯網路,以建立具有一個子網路的第一個虛擬網路。 下列範例指令碼會建立具有一個子網路的 Contoso 虛擬網路。
#Find the HNV Provider Logical Network
$logicalnetworks = Get-NetworkControllerLogicalNetwork -ConnectionUri $uri
foreach ($ln in $logicalnetworks) {
if ($ln.Properties.NetworkVirtualizationEnabled -eq "True") {
$HNVProviderLogicalNetwork = $ln
}
}
#Create the Virtual Subnet
$vsubnet = new-object Microsoft.Windows.NetworkController.VirtualSubnet
$vsubnet.ResourceId = "Contoso"
$vsubnet.Properties = new-object Microsoft.Windows.NetworkController.VirtualSubnetProperties
$vsubnet.Properties.AddressPrefix = "24.30.1.0/24"
$uri=”https://restserver”
#Create the Virtual Network
$vnetproperties = new-object Microsoft.Windows.NetworkController.VirtualNetworkProperties
$vnetproperties.AddressSpace = new-object Microsoft.Windows.NetworkController.AddressSpace
$vnetproperties.AddressSpace.AddressPrefixes = @("24.30.1.0/24")
$vnetproperties.LogicalNetwork = $HNVProviderLogicalNetwork
$vnetproperties.Subnets = @($vsubnet)
New-NetworkControllerVirtualNetwork -ResourceId "Contoso_VNet1" -ConnectionUri $uri -Properties $vnetproperties
步驟 2:建立第二個虛擬網路
在此步驟中,您會建立具有一個子網路的第二個虛擬網路。 下列範例指令碼會建立具有一個子網路的 Woodgrove 虛擬網路。
#Create the Virtual Subnet
$vsubnet = new-object Microsoft.Windows.NetworkController.VirtualSubnet
$vsubnet.ResourceId = "Woodgrove"
$vsubnet.Properties = new-object Microsoft.Windows.NetworkController.VirtualSubnetProperties
$vsubnet.Properties.AddressPrefix = "24.30.2.0/24"
$uri=”https://restserver”
#Create the Virtual Network
$vnetproperties = new-object Microsoft.Windows.NetworkController.VirtualNetworkProperties
$vnetproperties.AddressSpace = new-object Microsoft.Windows.NetworkController.AddressSpace
$vnetproperties.AddressSpace.AddressPrefixes = @("24.30.2.0/24")
$vnetproperties.LogicalNetwork = $HNVProviderLogicalNetwork
$vnetproperties.Subnets = @($vsubnet)
New-NetworkControllerVirtualNetwork -ResourceId "Woodgrove_VNet1" -ConnectionUri $uri -Properties $vnetproperties
步驟 3:從第一個虛擬網路到第二個虛擬網路設置相連
在此步驟中,您會設定第一個虛擬網路與您在前兩個步驟中建立的第二個虛擬網路之間的對等互連。 下列範例指令碼會建立從 Contoso_vnet1 到 Woodgrove_vnet1 的虛擬網路對等互連。
$peeringProperties = New-Object Microsoft.Windows.NetworkController.VirtualNetworkPeeringProperties
$vnet2 = Get-NetworkControllerVirtualNetwork -ConnectionUri $uri -ResourceId "Woodgrove_VNet1"
$peeringProperties.remoteVirtualNetwork = $vnet2
#Indicate whether communication between the two virtual networks
$peeringProperties.allowVirtualnetworkAccess = $true
#Indicates whether forwarded traffic is allowed across the vnets
$peeringProperties.allowForwardedTraffic = $true
#Indicates whether the peer virtual network can access this virtual networks gateway
$peeringProperties.allowGatewayTransit = $false
#Indicates whether this virtual network uses peer virtual networks gateway
$peeringProperties.useRemoteGateways =$false
New-NetworkControllerVirtualNetworkPeering -ConnectionUri $uri -VirtualNetworkId “Contoso_vnet1” -ResourceId “ContosotoWoodgrove” -Properties $peeringProperties
重要
建立此對等互連之後,vnet 狀態會顯示 [已起始]。
步驟 4:從第二個虛擬網路到第一個虛擬網路設置相連
在此步驟中,您會設定第二個虛擬網路與您在上述步驟 1 和 2 中建立的第一個虛擬網路之間的對等互連。 下列範例指令碼會建立從 Woodgrove_vnet1 到 Contoso_vnet1 的虛擬網路對等互連。
$peeringProperties = New-Object Microsoft.Windows.NetworkController.VirtualNetworkPeeringProperties
$vnet2=Get-NetworkControllerVirtualNetwork -ConnectionUri $uri -ResourceId "Contoso_VNet1"
$peeringProperties.remoteVirtualNetwork = $vnet2
# Indicates whether communication between the two virtual networks is allowed
$peeringProperties.allowVirtualnetworkAccess = $true
# Indicates whether forwarded traffic will be allowed across the vnets
$peeringProperties.allowForwardedTraffic = $true
# Indicates whether the peer virtual network can access this virtual network's gateway
$peeringProperties.allowGatewayTransit = $false
# Indicates whether this virtual network will use peer virtual network's gateway
$peeringProperties.useRemoteGateways =$false
New-NetworkControllerVirtualNetworkPeering -ConnectionUri $uri -VirtualNetworkId “Woodgrove_vnet1” -ResourceId “WoodgrovetoContoso” -Properties $peeringProperties
建立此對等互連之後,vnet 對等互連狀態會顯示這兩個對等的 [已連線]。 現在,一個虛擬網路中的虛擬機器可以與對等互連虛擬網路中的虛擬機器通訊。