Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 57 ยท Networking

Virtual Networks and Subnets

Learn Azure Virtual Networks: address spaces, CIDR, subnets, reserved addresses, system routes, VNet peering and service endpoints, with a Python subnet planner.

An Azure Virtual Network (VNet) is your own private network inside Azure. Virtual machines, App Service integrations, container hosts and databases with private access all attach to a VNet, and traffic between them stays on Microsoft's backbone instead of the public internet. A VNet is the foundation of almost every other networking feature in this chapter, so a good plan here saves painful rework later.

Every VNet is regional (it lives in one region and one subscription) and has one or more address spaces written in CIDR notation, such as 10.10.0.0/16. You carve that space into subnets, and each resource gets a private IP from the subnet it is placed in. The real service needs an account, so the commands below are read-only; the runnable examples use Python's ipaddress module to practice the arithmetic you need when planning.

Address spaces and CIDR

CIDR notation combines a network address and a prefix length. The prefix says how many leading bits are fixed. A /16 leaves 16 bits for hosts (65,536 addresses), a /24 leaves 8 bits (256 addresses), and a /28 leaves 4 bits (16 addresses). Each step of one in the prefix halves or doubles the size.

Use the private ranges reserved for internal networks (RFC 1918): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Azure also accepts others, but private ranges avoid clashing with public addresses. The one rule you must remember: address spaces of networks you connect must not overlap. Peering and VPN connections need distinct ranges. If every team picks 10.0.0.0/16 by default, you cannot connect any two of them later, so keep a central register of who owns which range.

Subnets and the five reserved addresses

A subnet is a slice of the VNet's address space. Subnets let you apply different security rules and routes to different tiers, for example web, app and data. In every subnet Azure reserves five addresses: the network address, the broadcast address, and three more for the default gateway and Azure DNS. A /24 therefore gives 251 usable addresses, and the smallest allowed subnet is a /29 (8 addresses, 3 usable).

Some services demand a dedicated subnet with a fixed name or minimum size. Examples include GatewaySubnet for VPN and ExpressRoute gateways, AzureFirewallSubnet and AzureBastionSubnet (both at least /26), and delegated subnets for services such as App Service VNet integration. Leave spare room for them.

flowchart TB
    V["VNet vnet-app<br/>10.10.0.0/16"]
    V --> W["snet-web<br/>10.10.1.0/24"]
    V --> A["snet-app<br/>10.10.2.0/24"]
    V --> D["snet-data<br/>10.10.3.0/24"]
    V --> G["GatewaySubnet<br/>10.10.255.0/27"]

One address space, split into tier subnets and a gateway subnet.

Creating a VNet with the CLI

This is read-only code; you need a subscription to run it:

Terminal
az group create --name rg-net --location westeurope

az network vnet create \
  --name vnet-app \
  --resource-group rg-net \
  --address-prefixes 10.10.0.0/16 \
  --subnet-name snet-web \
  --subnet-prefixes 10.10.1.0/24

az network vnet subnet create \
  --name snet-data \
  --resource-group rg-net \
  --vnet-name vnet-app \
  --address-prefixes 10.10.3.0/24

Subnets can be added later, but changing a subnet's range while resources are in it is restricted, which is another reason to plan with spare space.

Planning with Python's ipaddress module

The standard library already knows CIDR math, so a few lines can validate a plan before you deploy it. This is not Azure code; it is a stand-in that reproduces the arithmetic. It runs in real Python in your browser.

Python Check a subnet plan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same idea scales up: a script can read the ranges of every VNet in a company and report conflicts. The next example does that for peering decisions.

System routes and custom routes

You do not configure routing to get started. Azure adds system routes to every subnet: traffic to the VNet's own address space stays local, traffic to the internet (0.0.0.0/0) goes out to the internet, and traffic to some private ranges that are not part of your VNet is dropped. When you peer VNets or add a gateway, Azure adds more routes automatically.

To change the path, you create a route table with user-defined routes (UDRs) and attach it to a subnet. The most common use is forcing outbound traffic through a firewall: a route for 0.0.0.0/0 with next hop "virtual appliance" pointing at the firewall's private IP. The most specific prefix wins, and a user-defined route beats a system route with the same prefix.

VNet peering

Peering connects two VNets so their resources can talk using private IP addresses, with low latency and no gateway in between. It works across regions too (global peering) and across subscriptions. Peering is created in both directions, and it is not transitive: if A is peered with B and B with C, A cannot reach C through B unless you add extra routing such as a firewall in B or a gateway with transit enabled. Hub-and-spoke designs rely on this and are covered in Private Endpoints, VPN and ExpressRoute.

Terminal
az network vnet peering create \
  --name hub-to-spoke1 \
  --resource-group rg-net \
  --vnet-name vnet-hub \
  --remote-vnet vnet-spoke1 \
  --allow-vnet-access

# and the reverse direction
az network vnet peering create \
  --name spoke1-to-hub \
  --resource-group rg-net \
  --vnet-name vnet-spoke1 \
  --remote-vnet vnet-hub \
  --allow-vnet-access
Python Peering: overlap check and non-transitivity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Service endpoints

Many Azure services (Storage, SQL, Key Vault) have public endpoints. A service endpoint extends your subnet's identity to that service, so the service firewall can say "accept only traffic from this subnet". The traffic then takes the Azure backbone, but the service keeps its public IP address; nothing gets a private IP. You enable it per subnet:

Terminal
az network vnet subnet update \
  --name snet-app --resource-group rg-net --vnet-name vnet-app \
  --service-endpoints Microsoft.Storage

Service endpoints are simple and free. When you need a real private IP for the service, or access from on-premises, use a Private Endpoint instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many usable addresses does a /24 subnet have in Azure?

    A /24 has 256 addresses, and Azure reserves five in every subnet, which leaves 251.
  2. 2. Why do two VNets with address spaces 10.1.0.0/16 and 10.1.128.0/17 cause trouble?

    The /17 sits inside the /16. VNets you connect must have non-overlapping address spaces.
  3. 3. VNet A is peered with VNet B, and VNet B is peered with VNet C. Can A reach C through B by default?

    Peering is not transitive. Hub-and-spoke designs add a firewall or gateway in the hub to allow spoke-to-spoke traffic.
  4. 4. What does a service endpoint give you?

    Service endpoints let a service accept traffic from specific subnets, but they do not create a private IP. Private Endpoints do.

Summary

  • A VNet is a regional private network defined by one or more CIDR address spaces; subnets divide it by tier.
  • Azure reserves five addresses per subnet, and some services need dedicated, named subnets.
  • Address spaces you connect must never overlap, so plan ranges centrally.
  • System routes work out of the box; route tables with user-defined routes change the path, for example through a firewall.
  • Peering is private and fast but not transitive; service endpoints add subnet identity to public service endpoints.

Next: Network Security Groups and Firewalls.