Tutorial: Connect virtual networks with virtual network peering
You can connect virtual networks to each other with virtual network peering. These virtual networks can be in the same region or different regions (also known as global virtual network peering). Once virtual networks are peered, resources in both virtual networks can communicate with each other over a low-latency, high-bandwidth connection using Microsoft backbone network.
In this tutorial, you learn how to:
- Create virtual networks
- Connect two virtual networks with a virtual network peering
- Deploy a virtual machine (VM) into each virtual network
- Communicate between VMs
Prerequisites
- An Azure account with an active subscription. You can create an account for free.
Create a virtual network and an Azure Bastion host
The following procedure creates a virtual network with a resource subnet, an Azure Bastion subnet, and a Bastion host:
In the portal, search for and select Virtual networks.
On the Virtual networks page, select + Create.
On the Basics tab of Create virtual network, enter, or select the following information:
Setting Value Project details Subscription Select your subscription. Resource group Select Create new.
Enter test-rg for the name.
Select OK.Instance details Name Enter vnet-1. Region Select East US 2. Select Next to proceed to the Security tab.
In the Azure Bastion section, select Enable Azure Bastion.
Bastion uses your browser to connect to VMs in your virtual network over Secure Shell (SSH) or Remote Desktop Protocol (RDP) by using their private IP addresses. The VMs don't need public IP addresses, client software, or special configuration. For more information, see What is Azure Bastion?.
In Azure Bastion, enter or select the following information:
Setting Value Azure Bastion host name Enter bastion. Azure Bastion public IP address Select Create a public IP address.
Enter public-ip-bastion in Name.
Select OK.Select Next to proceed to the IP Addresses tab.
In the address space box in Subnets, select the default subnet.
In Edit subnet, enter or select the following information:
Setting Value Subnet purpose Leave the default of Default. Name Enter subnet-1. IPv4 IPv4 address range Leave the default of 10.0.0.0/16. Starting address Leave the default of 10.0.0.0. Size Leave the default of /24 (256 addresses). Select Save.
Select Review + create at the bottom of the window. When validation passes, select Create.
Repeat the previous steps to create a second virtual network with the following values:
Note
The second virtual network can be in the same region as the first virtual network or in a different region. You can skip the Security tab and the Bastion deployment for the second virtual network. After the network peer, you can connect to both virtual machines with the same Bastion deployment.
Setting | Value |
---|---|
Name | vnet-2 |
Address space | 10.1.0.0/16 |
Resource group | test-rg |
Subnet name | subnet-1 |
Subnet address range | 10.1.0.0/24 |
Create virtual network peer
Use the following steps to create a two way network peer between vnet1 and vnet2.
In the search box at the top of the portal, enter Virtual network. Select Virtual networks in the search results.
Select vnet-1.
In Settings select Peerings.
Select + Add.
Enter or select the following information in Add peering:
Setting Value Remote virtual network summary Peering link name Enter vnet-2-to-vnet-1. Virtual network deployment model Leave the default of Resource Manager. Subscription Select your subscription. Virtual network Select vnet-2. Remote virtual network peering settings Allow 'vnet-2' to access 'vnet-1' Leave the default of selected. Allow 'vnet-2' to receive forwarded traffic from 'vnet-1' Select the checkbox. Allow gateway or route server in 'vnet-2' to forward traffic to 'vnet-1' Leave the default of cleared. Enable 'vnet-2' to use 'vnet-1's' remote gateway or route server Leave the default of cleared. Local virtual network peering summary Peering link name Enter vnet-1-to-vnet-2. Local virtual network peering settings Allow 'vnet-1' to access 'vnet-2' Leave the default of selected. Allow 'vnet-1' to receive forwarded traffic from 'vnet-2' Select the checkbox. Allow gateway or route server in 'vnet-1' to forward traffic to 'vnet-2' Leave the default of cleared. Enable 'vnet-1' to use 'vnet-2's' remote gateway or route server Leave the default of cleared. Select Add.
Create virtual machines
Test the communication between the virtual machines by creating a virtual machine in each virtual network. The virtual machines can communicate with each other over the virtual network peering.
Create test virtual machine
The following procedure creates a test virtual machine (VM) named vm-1 in the virtual network.
In the portal, search for and select Virtual machines.
In Virtual machines, select + Create, then Azure virtual machine.
On the Basics tab of Create a virtual machine, enter or select the following information:
Setting Value Project details Subscription Select your subscription. Resource group Select test-rg. Instance details Virtual machine name Enter vm-1. Region Select East US 2. Availability options Select No infrastructure redundancy required. Security type Leave the default of Standard. Image Select Ubuntu Server 22.04 LTS - x64 Gen2. VM architecture Leave the default of x64. Size Select a size. Administrator account Authentication type Select Password. Username Enter azureuser. Password Enter a password. Confirm password Reenter the password. Inbound port rules Public inbound ports Select None. Select the Networking tab at the top of the page.
Enter or select the following information in the Networking tab:
Setting Value Network interface Virtual network Select vnet-1. Subnet Select subnet-1 (10.0.0.0/24). Public IP Select None. NIC network security group Select Advanced. Configure network security group Select Create new.
Enter nsg-1 for the name.
Leave the rest at the defaults and select OK.Leave the rest of the settings at the defaults and select Review + create.
Review the settings and select Create.
Note
Virtual machines in a virtual network with a bastion host don't need public IP addresses. Bastion provides the public IP, and the VMs use private IPs to communicate within the network. You can remove the public IPs from any VMs in bastion hosted virtual networks. For more information, see Dissociate a public IP address from an Azure VM.
Note
Azure provides a default outbound access IP for VMs that either aren't assigned a public IP address or are in the backend pool of an internal basic Azure load balancer. The default outbound access IP mechanism provides an outbound IP address that isn't configurable.
The default outbound access IP is disabled when one of the following events happens:
- A public IP address is assigned to the VM.
- The VM is placed in the backend pool of a standard load balancer, with or without outbound rules.
- An Azure NAT Gateway resource is assigned to the subnet of the VM.
VMs that you create by using virtual machine scale sets in flexible orchestration mode don't have default outbound access.
For more information about outbound connections in Azure, see Default outbound access in Azure and Use Source Network Address Translation (SNAT) for outbound connections.
Repeat the previous steps to create a second virtual machine in the second virtual network with the following values:
Setting | Value |
---|---|
Virtual machine name | vm-2 |
Region | East US 2 or same region as vnet-2. |
Virtual network | Select vnet-2. |
Subnet | Select subnet-1 (10.1.0.0/24). |
Public IP | None |
Network security group name | nsg-2 |
Wait for the virtual machines to be created before continuing with the next steps.
Connect to a virtual machine
Use ping
to test the communication between the virtual machines. Sign-in to the Azure portal to complete the following steps.
In the portal, search for and select Virtual machines.
On the Virtual machines page, select vm-1.
In the Overview of vm-1, select Connect.
In the Connect to virtual machine page, select the Bastion tab.
Select Use Bastion.
Enter the username and password you created when you created the VM, and then select Connect.
Communicate between VMs
At the bash prompt for vm-1, enter
ping -c 4 10.1.0.4
.You get a reply similar to the following message:
azureuser@vm-1:~$ ping -c 4 10.1.0.4 PING 10.1.0.4 (10.1.0.4) 56(84) bytes of data. 64 bytes from 10.1.0.4: icmp_seq=1 ttl=64 time=2.29 ms 64 bytes from 10.1.0.4: icmp_seq=2 ttl=64 time=1.06 ms 64 bytes from 10.1.0.4: icmp_seq=3 ttl=64 time=1.30 ms 64 bytes from 10.1.0.4: icmp_seq=4 ttl=64 time=0.998 ms --- 10.1.0.4 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3004ms rtt min/avg/max/mdev = 0.998/1.411/2.292/0.520 ms
Close the Bastion connection to vm-1.
Repeat the steps in Connect to a virtual machine to connect to vm-2.
At the bash prompt for vm-2, enter
ping -c 4 10.0.0.4
.You get a reply similar to the following message:
azureuser@vm-2:~$ ping -c 4 10.0.0.4 PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data. 64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=1.81 ms 64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=3.35 ms 64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.811 ms 64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=1.28 ms
Close the Bastion connection to vm-2.
When you finish using the resources that you created, you can delete the resource group and all its resources.
In the Azure portal, search for and select Resource groups.
On the Resource groups page, select the test-rg resource group.
On the test-rg page, select Delete resource group.
Enter test-rg in Enter resource group name to confirm deletion, and then select Delete.
Next steps
In this tutorial, you:
Created virtual network peering between two virtual networks.
Tested the communication between two virtual machines over the virtual network peering with
ping
.
To learn more about a virtual network peering: