仮想ネットワーク ピアリングを構成する

適用対象: Windows Server 2022、Windows Server 2019、Windows Server 2016、Azure Stack HCI バージョン 21H2 および 20H2

この手順では、Windows PowerShell を使用して、それぞれが 1 つのサブネットを持つ 2 つの仮想ネットワークを作成します。 次に、2 つの仮想ネットワーク間のピアリングを構成して、両者の間の接続を有効にします。

重要

必ず、環境に合わせてプロパティを更新してください。

ステップ 1: 最初の仮想マシンを作成する

この手順では、Windows PowerShell 使用して、HNV プロバイダーの論理ネットワークを見つけて、1 つのサブネットを持つ最初の仮想ネットワークを作成します。 次のスクリプト例では、1 つのサブネットを含む 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: 2 番目の仮想ネットワークを作成する

この手順では、1 つのサブネットを含む 2 番目の仮想ネットワークを作成します。 次のスクリプト例では、1 つのサブネットを含む 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: 最初の仮想ネットワークから 2 番目の仮想ネットワークへのピアリングを構成する

この手順では、最初の仮想ネットワークと、前の 2 つの手順で作成した 2 番目の仮想ネットワークとの間のピアリングを構成します。 次のスクリプト例では、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: 2 番目の仮想ネットワークから最初の仮想ネットワークへのピアリングを構成する

この手順では、2 番目の仮想ネットワークと、上記の手順 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 ピアリングの状態が、両方のピアで [接続済み] と表示されます。 これで、1 つの仮想ネットワーク内の仮想マシンが、ピアリングされた仮想ネットワーク内の仮想マシンと通信できます。