Goal
Build a small hub-and-spoke network: a hub VNet and a spoke VNet with address spaces that do not overlap, peering in both directions, a private DNS zone, and a network security group that allows SSH only from the hub. You prove it with two small VMs and the effective security rules. Delete everything at the end; the VMs cost only a few cents.
Before you start
- Cloud Shell (Bash) or
az login. Variables:RG=rg-lab-net,LOC=westeurope. - Plan the addresses first: hub
10.10.0.0/16(subnet10.10.1.0/24), spoke10.20.0.0/16(subnet10.20.1.0/24).
Tasks
Tick a task when it is done. Only your browser remembers it.
- Make the resource group and the two VNets with one subnet each.
- Peer the VNets in both directions with allow forwarded traffic.
- Make the private DNS zone
lab.internal, link it to both VNets with auto-registration. - Make the NSG
nsg-spokewith one inbound rule: allow TCP 22 only from10.10.0.0/16, priority 100. Attach it to the spoke subnet. - Create a small Ubuntu VM
vm-hubin the hub (with a public IP) andvm-spokein the spoke (no public IP). - From
vm-hub, SSH tovm-spokeby its DNS namevm-spoke.lab.internal. - Show the effective security rules of the NIC of
vm-spokeand explain which rule allowed your SSH. - Delete the resource group.
Check your work
az network vnet peering list -g $RG --vnet-name vnet-hub -o table
az network private-dns record-set a list -g $RG -z lab.internal -o table
az network nsg rule list -g $RG --nsg-name nsg-spoke -o table
az network nic list-effective-nsg -g $RG -n vm-spokeVMNic- Both peerings show
PeeringStateConnected. - The DNS zone has A records for
vm-hubandvm-spoke(made by auto-registration). - SSH from
vm-hubtovm-spoke.lab.internalworks; SSH from the internet tovm-spokeis impossible (no public IP). - The effective rules show your rule
allow-ssh-from-hubbefore the default ruleDenyAllInBound.
Hints
Show the hints
- Peering is made per side:
az network vnet peering createtwice, with--remote-vnetand--allow-vnet-access --allow-forwarded-traffic. az network private-dns link vnet create ... --registration-enabled true.az vm createwith--public-ip-address ""makes a VM without a public IP.--nsg ""stops it from making its own NSG on the NIC.- Use SSH agent forwarding (
ssh -A) so the hub VM can use your key to reach the spoke VM.
Solution
Show the solution
- Make the group and the VNets.
RG=rg-lab-net; LOC=westeurope az group create -n $RG -l $LOC az network vnet create -g $RG -n vnet-hub --address-prefixes 10.10.0.0/16 --subnet-name snet-hub --subnet-prefixes 10.10.1.0/24 az network vnet create -g $RG -n vnet-spoke --address-prefixes 10.20.0.0/16 --subnet-name snet-spoke --subnet-prefixes 10.20.1.0/24 - Peer both ways.
- Private DNS with auto-registration.
- NSG for the spoke subnet.
az network nsg create -g $RG -n nsg-spoke az network nsg rule create -g $RG --nsg-name nsg-spoke -n allow-ssh-from-hub --priority 100 --direction Inbound --access Allow --protocol Tcp --source-address-prefixes 10.10.0.0/16 --destination-port-ranges 22 az network vnet subnet update -g $RG --vnet-name vnet-spoke -n snet-spoke --network-security-group nsg-spoke - Two small VMs.
az vm create -g $RG -n vm-hub --image Ubuntu2204 --size Standard_B1s --vnet-name vnet-hub --subnet snet-hub --admin-username azureuser --generate-ssh-keys az vm create -g $RG -n vm-spoke --image Ubuntu2204 --size Standard_B1s --vnet-name vnet-spoke --subnet snet-spoke --public-ip-address "" --nsg "" --admin-username azureuser --generate-ssh-keys - Jump through the hub.
- Read the effective rules (back in Cloud Shell).
az network nic list-effective-nsg -g $RG -n vm-spokeVMNic --query "value[].effectiveSecurityRules[].{name:name, prio:priority, access:access, src:sourceAddressPrefix}" -o tableThe rule
allow-ssh-from-hub(priority 100) matches first; everything else from outside hitsDenyAllInBound(65500). - Clean up.
az group delete -n $RG --yes --no-wait
My notes
No notes yet.