Edit

Tutorial: Route network traffic with a route table

Azure routes traffic between all subnets within a virtual network by default. You can create your own routes to override Azure's default routing. Custom routes are helpful when, for example, you want to route traffic between subnets through a network virtual appliance (NVA).

Diagram of Azure resources created in tutorial.

In this tutorial, you learn how to:

  • Create a virtual network and subnets
  • Create an NVA that routes traffic
  • Deploy virtual machines (VMs) into different subnets
  • Create a route table
  • Create a route
  • Associate a route table to a subnet
  • Route traffic from one subnet to another through an NVA

Prerequisites

Create subnets

A DMZ and Private subnet are needed for this tutorial. The DMZ subnet is where you deploy the NVA and the Private subnet is where you deploy the private virtual machines you want to route traffic to. In the diagram, subnet-1 is the Public subnet used for the public virtual machine.

Create a resource group

  1. Sign in to the Azure portal.

  2. In the search box at the top of the portal, enter Resource group. Select Resource groups in the search results.

  3. Select + Create.

  4. In the Basics tab of Create a resource group, enter or select the following information:

    Setting Value
    Subscription Select your subscription.
    Resource group Enter test-rg.
    Region Select East US 2.
  5. Select Review + create.

  6. Select Create.

Create a virtual network

  1. In the search box at the top of the portal, enter Virtual network. Select Virtual networks in the search results.

  2. Select + Create.

  3. 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 test-rg.
    Instance details
    Name Enter vnet-1.
    Region Select East US 2.
  4. Select Next to proceed to the Security tab.

  5. Select Next to proceed to the IP Addresses tab.

  6. In the address space box in Subnets, select the default subnet.

  7. In Edit subnet, enter or select the following information:

    Setting Value
    Subnet details
    Subnet template Leave the default Default.
    Name Enter subnet-1.
    Starting address Leave the default of 10.0.0.0.
    Subnet size Leave the default of /24 (256 addresses).
  8. Select Save.

  9. Select + Add a subnet.

  10. In Add a subnet, enter or select the following information:

    Setting Value
    Subnet details
    Subnet template Leave the default Default.
    Name Enter subnet-private.
    Starting address Enter 10.0.2.0.
    Subnet size Leave the default of /24 (256 addresses).
  11. Select Add.

  12. Select + Add a subnet.

  13. In Add a subnet, enter or select the following information:

    Setting Value
    Subnet details
    Subnet template Leave the default Default.
    Name Enter subnet-dmz.
    Starting address Enter 10.0.3.0.
    Subnet size Leave the default of /24 (256 addresses).
  14. Select Add.

  15. Select Review + create at the bottom of the screen, and when validation passes, select Create.

Deploy Azure Bastion

Azure 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 about Azure Bastion, see Azure Bastion.

Note

Hourly pricing starts from the moment that Bastion is deployed, regardless of outbound data usage. For more information, see Pricing and SKUs. If you're deploying Bastion as part of a tutorial or test, we recommend that you delete this resource after you finish using it.

  1. In the search box at the top of the portal, enter Bastion. Select Bastions in the search results.

  2. Select + Create.

  3. In the Basics tab of Create a Bastion, enter or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Name Enter bastion.
    Region Select East US 2.
    Tier Select Developer.
    Configure virtual networks
    Virtual network Select vnet-1.
    Subnet The AzureBastionSubnet is created automatically with an address space of /26 or larger.
  4. Select Review + create.

  5. Select Create.

Create an NVA virtual machine

Network virtual appliances (NVAs) are virtual machines that help with network functions, such as routing and firewall optimization. In this section, create an NVA using an Ubuntu 24.04 virtual machine.

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. Select + Create then Azure virtual machine.

  3. In Create a virtual machine enter or select the following information in the Basics tab:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Virtual machine name Enter vm-nva.
    Region Select (US) East US 2.
    Availability options Select No infrastructure redundancy required.
    Security type Select Standard.
    Image Select Ubuntu Server 24.04 LTS - x64 Gen2.
    VM architecture Leave the default of x64.
    Size Select a size.
    Administrator account
    Authentication type Select SSH public key.
    Username Enter a username.
    SSH public key source Select Generate new key pair.
    Key pair name Enter vm-nva-key.
    Inbound port rules
    Public inbound ports Select None.
  4. Select Next: Disks then Next: Networking.

  5. In the Networking tab, enter or select the following information:

    Setting Value
    Network interface
    Virtual network Select vnet-1.
    Subnet Select subnet-dmz (10.0.3.0/24).
    Public IP Select None.
    NIC network security group Select Advanced.
    Configure network security group Select Create new.
    In Name enter nsg-nva.
    Select OK.
  6. Leave the rest of the options at the defaults and select Review + create.

  7. Select Create.

Create public and private virtual machines

Create two virtual machines in the vnet-1 virtual network. One virtual machine is in the subnet-1 subnet and the other virtual machine is in the subnet-private subnet. Use the same virtual machine image for both virtual machines.

Create public virtual machine

The public virtual machine is used to simulate a machine in the public internet. The public and private virtual machines are used to test the routing of network traffic through the NVA virtual machine.

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. Select + Create then Azure virtual machine.

  3. In Create a virtual machine enter or select the following information in the Basics tab:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Virtual machine name Enter vm-public.
    Region Select (US) East US 2.
    Availability options Select No infrastructure redundancy required.
    Security type Select Standard.
    Image Select Ubuntu Server 24.04 LTS - x64 Gen2.
    VM architecture Leave the default of x64.
    Size Select a size.
    Administrator account
    Authentication type Select SSH public key.
    Username Enter a username.
    SSH public key source Select Generate new key pair.
    Key pair name Enter vm-public-key.
    Inbound port rules
    Public inbound ports Select None.
  4. Select Next: Disks then Next: Networking.

  5. In the Networking tab, enter or select the following information:

    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 None.
  6. Leave the rest of the options at the defaults and select Review + create.

  7. Select Create.

Create private virtual machine

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. Select + Create then Azure virtual machine.

  3. In Create a virtual machine enter or select the following information in the Basics tab:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Virtual machine name Enter vm-private.
    Region Select (US) East US 2.
    Availability options Select No infrastructure redundancy required.
    Security type Select Standard.
    Image Select Ubuntu Server 24.04 LTS - x64 Gen2.
    VM architecture Leave the default of x64.
    Size Select a size.
    Administrator account
    Authentication type Select SSH public key.
    Username Enter a username.
    SSH public key source Select Generate new key pair.
    Key pair name Enter vm-private-key.
    Inbound port rules
    Public inbound ports Select None.
  4. Select Next: Disks then Next: Networking.

  5. In the Networking tab, enter or select the following information:

    Setting Value
    Network interface
    Virtual network Select vnet-1.
    Subnet Select subnet-private (10.0.2.0/24).
    Public IP Select None.
    NIC network security group Select None.
  6. Leave the rest of the options at the defaults and select Review + create.

  7. Select Create.

Enable IP forwarding

To route traffic through the NVA, turn on IP forwarding in Azure and in the operating system of vm-nva. When IP forwarding is enabled, any traffic received by vm-nva destined for a different IP address isn't dropped and is forwarded to the correct destination.

Enable IP forwarding in Azure

In this section, you turn on IP forwarding for the network interface of the vm-nva virtual machine.

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. In Virtual machines, select vm-nva.

  3. In vm-nva, expand Networking then select Network settings.

  4. Select the name of the interface next to Network Interface:. The name begins with vm-nva and has a random number assigned to the interface. The name of the interface in this example is vm-nva313.

    Screenshot of network interface of NVA virtual machine.

  5. In the network interface overview page, select IP configurations from the Settings section.

  6. In IP configurations, select the box next to Enable IP forwarding.

    Screenshot of enablement of IP forwarding.

  7. Select Apply.

Enable IP forwarding in the operating system

In this section, turn on IP forwarding for the operating system of the vm-nva virtual machine to forward network traffic. Use the Run Command feature to execute a script on the virtual machine.

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. In Virtual machines, select vm-nva.

  3. Expand Operations then select Run command.

  4. Select RunShellScript.

  5. Enter the following script in the Run Command Script window:

    sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
    sudo sysctl -p
    
  6. Select Run.

  7. Wait for the script to complete. The output shows the IP forwarding setting has been enabled.

  8. Return to the Overview page of vm-nva and select Restart to restart the virtual machine.

Create a route table

In this section, create a route table to define the route of the traffic through the NVA virtual machine. The route table is associated to the subnet-1 subnet where the vm-public virtual machine is deployed.

  1. In the search box at the top of the portal, enter Route table. Select Route tables in the search results.

  2. Select + Create.

  3. In Create Route table enter or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Region Select East US 2.
    Name Enter route-table-public.
    Propagate gateway routes Leave the default of Yes.
  4. Select Review + create.

  5. Select Create.

Create a route

In this section, create a route in the route table that you created in the previous steps.

  1. In the search box at the top of the portal, enter Route table. Select Route tables in the search results.

  2. Select route-table-public.

  3. Expand Settings then select Routes.

  4. Select + Add in Routes.

  5. Enter or select the following information in Add route:

    Setting Value
    Route name Enter to-private-subnet.
    Destination type Select IP Addresses.
    Destination IP addresses/CIDR ranges Enter 10.0.2.0/24.
    Next hop type Select Virtual appliance.
    Next hop address Enter 10.0.3.4.
    This is the IP address of the vm-nva you created in the earlier steps..
  6. Select Add.

  7. Select Subnets in Settings.

  8. Select + Associate.

  9. Enter or select the following information in Associate subnet:

    Setting Value
    Virtual network Select vnet-1 (test-rg).
    Subnet Select subnet-1.
  10. Select OK.

Test the routing of network traffic

Test routing of network traffic from vm-public to vm-private. Test routing of network traffic from vm-private to vm-public.

Test network traffic from vm-public to vm-private

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. In Virtual machines, select vm-public.

  3. Select Connect then Connect via Bastion in the Overview section.

  4. In the Bastion connection page, enter or select the following information:

    Setting Value
    Authentication Type Select SSH Private Key from Local File.
    Username Enter the username you created.
    Local File Select the vm-public-key private key file you downloaded.
  5. Select Connect.

  6. In the prompt, enter the following command to trace the routing of network traffic from vm-public to vm-private:

    tracepath vm-private
    

    The response is similar to the following example:

    azureuser@vm-public:~$ tracepath vm-private
     1?: [LOCALHOST]                      pmtu 1500
     1:  vm-nva.internal.cloudapp.net                          1.766ms 
     1:  vm-nva.internal.cloudapp.net                          1.259ms 
     2:  vm-private.internal.cloudapp.net                      2.202ms reached
     Resume: pmtu 1500 hops 2 back 1 
    

    You can see that there are two hops in this response for tracepath ICMP traffic from vm-public to vm-private. The first hop is vm-nva. The second hop is the destination vm-private.

    Azure sent the traffic from subnet-1 through the NVA and not directly to subnet-private because you previously added the to-private-subnet route to route-table-public and associated it to subnet-1.

  7. Close the Bastion session.

Test network traffic from vm-private to vm-public

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. In Virtual machines, select vm-private.

  3. Select Connect then Connect via Bastion in the Overview section.

  4. In the Bastion connection page, enter or select the following information:

    Setting Value
    Authentication Type Select SSH Private Key from Local File.
    Username Enter the username you created.
    Local File Select the vm-private-key private key file you downloaded.
  5. Select Connect.

  6. In the prompt, enter the following command to trace the routing of network traffic from vm-private to vm-public:

    tracepath vm-public
    

    The response is similar to the following example:

    azureuser@vm-private:~$ tracepath vm-public
     1?: [LOCALHOST]                      pmtu 1500
     1:  vm-public.internal.cloudapp.net                       2.584ms reached
     1:  vm-public.internal.cloudapp.net                       2.147ms reached
     Resume: pmtu 1500 hops 1 back 2 
    

    You can see there's one hop in this response, which is the destination vm-public.

    Azure sent the traffic directly from subnet-private to subnet-1. By default, Azure routes traffic directly between subnets.

  7. Close the Bastion session.

Configure ECMP routing (optional)

Equal-cost multipath (ECMP) routing lets you route traffic through multiple network virtual appliances (NVAs). An ECMP route uses the VirtualApplianceEcmp next hop type and supports 2 to 16 next hop IP addresses.

In this section, you update the to-private-subnet route in route-table-public to use two NVAs. The destination address prefix remains 10.0.2.0/24, and the route table remains associated with subnet-1.

Before you begin, complete the routing tests with the single NVA.

For requirements and routing considerations, see Equal-cost multipath routing for user-defined routes.

Prepare a second appliance

  1. Repeat the steps in Create an NVA virtual machine to create a second NVA. Use the following values:

    Setting Value
    Resource group test-rg
    Virtual machine name vm-nva-2
    Virtual network vnet-1
    Subnet subnet-dmz

    Use a unique name for the network interface and SSH key.

  2. Enable IP forwarding on the network interface and in the operating system, as you did for vm-nva.

  3. Verify that both NVAs can forward traffic to subnet-private and that their security policies allow the test traffic.

  4. Record the private IP address of each NVA.

The examples in this section use 10.0.3.4 and 10.0.3.5. Replace these values with the private IP addresses of your NVAs. The second NVA might have a different address.

Important

Use a version of Azure CLI or the Az.Network PowerShell module that supports VirtualApplianceEcmp and multiple next hop IP addresses. The minimum versions listed earlier in this tutorial might not support ECMP. For more information, see az network route-table route create and New-AzRouteConfig.

Register the ECMP feature

Register the AllowEcmpInRoute feature for Microsoft.Network in the subscription that contains route-table-public. To register the feature, you need the appropriate Microsoft.Features/* permissions in addition to permissions to manage network resources.

Select the Azure portal, PowerShell, or Azure CLI tab to register the feature. Use the same tab to update the route in the next section.

Use PowerShell in Azure Cloud Shell to register the feature from the Azure portal.

  1. Sign in to the Azure portal.

  2. Select Cloud Shell on the menu bar at the top of the portal, and then select PowerShell. If prompted, complete the Cloud Shell setup.

  3. Select the subscription that contains the tutorial resources and register the feature:

    Set-AzContext -SubscriptionId "<subscription-id>" -ErrorAction Stop
    Register-AzProviderFeature `
        -ProviderNamespace "Microsoft.Network" `
        -FeatureName "AllowEcmpInRoute" `
        -ErrorAction Stop
    
  4. Check the feature state:

    Get-AzProviderFeature `
        -ProviderNamespace "Microsoft.Network" `
        -FeatureName "AllowEcmpInRoute" `
        -ErrorAction Stop |
        Select-Object FeatureName, RegistrationState
    
  5. When the feature state is Registered, register the Microsoft.Network resource provider again:

    Register-AzResourceProvider -ProviderNamespace "Microsoft.Network" -ErrorAction Stop
    Get-AzResourceProvider -ProviderNamespace "Microsoft.Network" -ErrorAction Stop |
        Select-Object ProviderNamespace, RegistrationState
    
  6. Wait until the resource provider registration state is Registered before you continue.

Note

Feature registration can take time. Wait until the state changes from Registering or Pending to Registered before you continue. If the feature requires approval, follow the service's feature access process. For more information, see Set up features in an Azure subscription.

Change the route to use ECMP

Warning

Updating the route changes the path for traffic from subnet-1 to subnet-private. Verify that both NVAs can forward traffic before you continue. Update the existing route instead of adding a second route with the same destination address prefix.

In the PowerShell Cloud Shell session, update to-private-subnet to use both NVAs. The following commands update the existing route without changing the route table association.

Get-AzRouteTable -ResourceGroupName "test-rg" -Name "route-table-public" -ErrorAction Stop |
    Set-AzRouteConfig `
        -Name "to-private-subnet" `
        -AddressPrefix "10.0.2.0/24" `
        -NextHopType "VirtualApplianceEcmp" `
        -NextHopIpAddress @("10.0.3.4", "10.0.3.5") `
        -ErrorAction Stop |
    Set-AzRouteTable -ErrorAction Stop

Retrieve the route to verify the saved configuration.

Get-AzRouteTable -ResourceGroupName "test-rg" -Name "route-table-public" -ErrorAction Stop |
    Get-AzRouteConfig -Name "to-private-subnet" -ErrorAction Stop |
    Select-Object Name, AddressPrefix, NextHopType, NextHopIpAddress, ProvisioningState

Verify the subnet association in the Azure portal:

  1. In the search box at the top of the portal, enter Route tables. Select Route tables in the search results.
  2. Select route-table-public.
  3. Under Settings, select Subnets.
  4. Verify that subnet-1 in vnet-1 is associated with the route table. You don't need to associate the subnet again.

Note

These steps use Azure Cloud Shell to configure the ECMP route, rather than a route configuration form in the portal.

Verify that the saved route has the following values:

Setting Value
Next hop type VirtualApplianceEcmp
Next hop IP addresses The private IP addresses of both NVAs
Provisioning state Succeeded

Azure PowerShell returns the IP addresses in NextHopIpAddress. Azure CLI returns them in nextHop.nextHopIpAddresses.

Test ECMP routing

  1. Review the effective routes for the network interface of vm-public. Verify that traffic destined for 10.0.2.0/24 uses the user-defined route.
  2. Repeat the connectivity test from vm-public to vm-private. The first NVA hop might differ from the single-NVA test.
  3. Create multiple independent connections. Check the forwarding counters or capture traffic on both NVAs to verify that traffic uses both next hops. A single tracepath result doesn't show traffic distribution across both NVAs.
  4. Verify that response traffic reaches vm-public. The return route can remain direct between subnets. ECMP doesn't ensure that traffic uses the same NVA in both directions. If you use stateful NVAs, check the return-path requirements with the appliance vendor.

Note

A provisioning state of Succeeded confirms that Azure provisioned the route resource. It doesn't verify NVA health, equal traffic distribution, session-state synchronization, or successful failover.

When you no longer need the resources, continue to Clean up resources in this tutorial. Deleting test-rg removes both NVAs, including vm-nva-2. Deleting the resource group doesn't unregister the subscription-level ECMP feature.

Clean up resources

When you finish using the resources that you created, you can delete the resource group and all its resources.

  1. In the Azure portal, search for and select Resource groups.

  2. On the Resource groups page, select the test-rg resource group.

  3. On the test-rg page, select Delete resource group.

  4. Enter test-rg in Enter resource group name to confirm deletion, and then select Delete.

Next steps

In this tutorial, you:

  • Created a route table and associated it to a subnet.

  • Created a simple NVA that routed traffic from a public subnet to a private subnet.

You can deploy different preconfigured NVAs from the Azure Marketplace, which provide many useful network functions.

To learn more about routing, see Routing overview and Manage a route table. Routing can also be automatically configured at scale with Azure Virtual Network Manager's user-defined route (UDR) management feature.

To learn how to restrict network access to PaaS resources with virtual network service endpoints, advance to the next tutorial.