Here are the steps and explanations for ensuring that hosts on VNET2 can access hosts on both VNET1 and VNET3, but hosts on VNET1 and VNET3 cannot communicate through VNET2:
To connect different virtual networks in Azure, you need to use virtual network peering. Virtual network peering allows you to create low-latency, high-bandwidth connections between virtual networks without using gateways or the internet1.
To create a virtual network peering, you need to go to the Azure portal and select your virtual network. Then select Peerings under Settings and select + Add2.
On the Add peering page, enter or select the following information:
Name: Type a unique name for the peering from the source virtual network to the destination virtual network.
Virtual network deployment model: Select Resource manager.
Subscription: Select the subscription that contains the destination virtual network.
Virtual network: Select the destination virtual network from the list or enter its resource ID.
Name of the peering from [destination virtual network] to [source virtual network]: Type a unique name for the peering from the destination virtual network to the source virtual network.
Configure virtual network access settings: Select Enabled to allow resources in both virtual networks to communicate with each other.
Allow forwarded traffic: Select Disabled to prevent traffic that originates from outside either of the peered virtual networks from being forwarded through either of them.
Allow gateway transit: Select Disabled to prevent either of the peered virtual networks from using a gateway in the other virtual network.
Use remote gateways: Select Disabled to prevent either of the peered virtual networks from using a gateway in the other virtual network as a transit point to another network.
Select Add to create the peering2.
Repeat the previous steps to create peerings between VNET2 and VNET1, and between VNET2 and VNET3. This will allow hosts on VNET2 to access hosts on both VNET1 and VNET3.
To prevent hosts on VNET1 and VNET3 from communicating through VNET2, you need to use network security groups (NSGs) to filter traffic between subnets. NSGs are rules that allow or deny inbound or outbound traffic based on source or destination IP address, port, or protocol3.
To create an NSG, you need to go to the Azure portal and select Create a resource. Search for network security group and select Network security group. Then select Create4.
On the Create a network security group page, enter or select the following information:
Subscription: Select your subscription name.
Resource group: Select your resource group name.
Name: Type a unique name for your NSG.
Region: Select the same region as your virtual networks.
Select Review + create and then select Create to create your NSG4.
To add rules to your NSG, you need to go to the Network security groups service in the Azure portal and select your NSG. Then select Inbound security rules or Outbound security rules under Settings and select + Add4.
On the Add inbound security rule page or Add outbound security rule page, enter or select the following information:
Source or Destination: Select CIDR block.
Source CIDR blocks or Destination CIDR blocks: Enter the IP address range of the source or destination subnet that you want to filter. For example, 10.0.1.0/24 for VNET1 subnet 1, 10.0.2.0/24 for VNET2 subnet 1, and 10.0.3.0/24 for VNET3 subnet 1.
Protocol: Select Any to apply the rule to any protocol.
Action: Select Deny to block traffic from or to the source or destination subnet.
Priority: Enter a number between 100 and 4096 that indicates the order of evaluation for this rule. Lower numbers have higher priority than higher numbers.
Name: Type a unique name for your rule.
Select Add to create your rule4.
Repeat the previous steps to create inbound and outbound rules for your NSG that deny traffic between VNET1 and VNET3 subnets. For example, you can create an inbound rule that denies traffic from 10.0.1.0/24 (VNET1 subnet 1) to 10.0.3.0/24 (VNET3 subnet 1), and an outbound rule that denies traffic from 10.0.3.0/24 (VNET3 subnet 1) to 10.0.1.0/24 (VNET1 subnet 1).
To associate your NSG with a subnet, you need to go to the Virtual networks service in the Azure portal and select your virtual network. Then select Subnets under Settings and select the subnet that you want to associate with your NSG5.
On the Edit subnet page, under Network security group, select your NSG from the drop-down list. Then select Save5.
Repeat the previous steps to associate your NSG with the subnets in VNET1 and VNET3 that you want to isolate from each other.