In this very first chapter, we will learn about the basics of Azure networking, including creating Azure virtual networks and designing address spaces and subnets. This will lay the foundation for all future recipes that will be covered in this chapter.
We will cover the following recipes in this chapter:
For this chapter, the following is required:
The code samples can be found at https://github.com/PacktPublishing/Azure-Networking-Cookbook-Second-Edition/tree/master/Chapter01.
Azure Virtual Network represents your local network in the cloud. It enables other Azure resources to communicate over a secure private network without exposing endpoints over the internet.
Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.
In order to create a new virtual network using the Azure portal, take the following steps:
We deploy virtual networks to Resource group under Subscription in the Azure datacenter that we choose. Region and Subscription are important parameters; we will only be able to attach Azure resources to this virtual network if they are in the same subscription and region as the Azure datacenter. The address space option defines the number of IP addresses that will be available for our network. It uses the Classless Inter-Domain Routing (CIDR) format and the largest range we can choose is /8. In the portal, we need to create an initial subnet and define the subnet address range. The smallest subnet allowed is /29 and the largest is /8 (however, this cannot be larger than the virtual network range). For reference, the range 10.0.0.0/8 (in CIDR format) will create an address range of 167772115 IP addresses (from 10.0.0.0 to 10.255.255.255) and 10.0.0.0/29 will create a range of 8 IP addresses (from 10.0.0.0 to 10.0.0.7).
PowerShell is a command-line shell and scripting language based on .NET Framework. It's often used by system administrators to automate tasks and manage operating systems. Azure PowerShell Az is a PowerShell module that allows us to automate and manage Azure resources. Az is also very often used to automate deployment tasks and can also be used to deploy a new Azure virtual network.
Before we start, we need to make sure that we have the latest Az modules installed. To install Az modules, we need to run this command in the PowerShell console:
Install-Module -Name Az -AllowClobber -Scope CurrentUser
For more information, you can visit https://docs.microsoft.com/powershell/azure/install-az-ps?view=azps-4.5.0.
Before we start, we need to connect to the Azure subscription from a PowerShell console. Here's the command to do this:
Connect-AzAccountAzAccount
This will open a pop-up window where we need to input the credentials for the Azure subscription.
Afterward, we need to create a resource group where our virtual network will be deployed:
New-AzResourceGroup -name 'Packt-Networking-Script' -Location 'westeurope'
The output should be similar to that shown in Figure 1.6:
Deploying an Azure virtual network is done in a single script. We need to define the parameters for the name, resource group, location, and address range. Here is an example script:
New-AzVirtualNetwork -ResourceGroupName 'Packt-Networking-Script' -Location 'westeurope' -Name 'Packt-Script' -AddressPrefix 10.11.0.0/16
You should receive the following output:
The difference between deploying a virtual network from the portal and using PowerShell is that no subnet needs to be defined in PowerShell. The subnet is deployed in a separate command that can be executed either when you are deploying a virtual network, or later on. We are going to see this command in the Adding a subnet with PowerShell recipe later in this chapter.
In addition to adding subnets while creating a virtual network, we can add additional subnets to our network at any time.
Before you start, open a web browser and go to the Azure portal at https://portal.azure.com. Here, locate the previously created virtual network.
In order to add a subnet to a virtual network using the Azure portal, we must take the following steps:
For a gateway subnet, the only parameter we need to define is Address range. The same rules apply as for adding a regular subnet. This time, we don't have to provide a name, as it's already defined. You can add only one gateway subnet per virtual network. Service endpoints are not allowed in the gateway subnet:
A single virtual network can have multiple subnets defined. Subnets cannot overlap and must be in the range of the virtual network address range. For each subnet, four IP addresses are saved for Azure management and cannot be used. Depending on the network settings, we can define the communication rules between subnets in the virtual network. A gateway subnet is used for Virtual Private Network (VPN) connections, and this will be covered later in the cookbook.
Now, let's learn how to add a subnet using PowerShell.
When creating an Azure virtual network with PowerShell, a subnet is not created in the same step and requires an additional command to be executed separately.
Before creating a subnet, we need to collect information about the virtual network that the new subnet will be associated with. The parameters that need to be provided are the name of the virtual network and the resource group that the virtual network is located in:
$VirtualNetwork = Get-AzVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'
Add-AzVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix 10.11.0.0/24 -VirtualNetwork $VirtualNetwork
$VirtualNetwork | Set-AzVirtualNetwork
$VirtualNetwork = Get-AzVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'
Add-AzVirtualNetworkSubnetConfig -Name BackEnd -AddressPrefix 10.11.1.0/24 -VirtualNetwork $VirtualNetwork
$VirtualNetwork | Set-AzVirtualNetwork
The subnet is created and added to the virtual network, but we need to confirm the changes before they can become effective. When it comes to size, all the rules for creating or adding a subnet using the Azure portal, apply here as well; the subnet must be within the virtual network's address space and cannot overlap with other subnets in the virtual network. The smallest subnet allowed is /29, and the largest is /8, provided the value is within the virtual network's address space. For example, if you are creating a /16 network, the largest value for the subnet will be /16 only, as we cannot include a /8 subnet in a /16 address space.
We can create and add multiple subnets with a single script, as follows:
$VirtualNetwork = Get-AzVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'
$FrontEnd = Add-AzVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix 10.11.0.0/24 -VirtualNetwork $VirtualNetwork
$BackEnd = Add-AzVirtualNetworkSubnetConfig -Name BackEnd -AddressPrefix 10.11.1.0/24 -VirtualNetwork $VirtualNetwork
$VirtualNetwork | Set-AzVirtualNetwork
After the initial address space is defined during the creation of a virtual network, we can still change the address space size as needed. We can either increase or decrease the size of the address space or change the address space completely by using a new address range.
Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.
In order to change the address space size for a virtual network using the Azure portal, we must observe the following steps:
Although you can change the address space at any time, there are some rules that determine what you can and cannot do. The address space cannot be decreased if you have subnets defined in the address space that would not be covered by the new address space. For example, if the address space were in the range of 10.0.0.0/16, it would cover addresses from 10.0.0.1 to 10.0.255.254. If one of the subnets was defined as 10.0.255.0/24, we wouldn't be able to change the virtual network to 10.0.0.0/17, as this would leave the subnet outside the new space.
The address space can't be changed to a new address space if you have subnets defined. In order to completely change the address space, you need to remove all subnets first. For example, if we had the address space defined as 10.0.0.0/16, we would not be able to change it to 10.1.0.0/16, since having any subnets in the old space would leave them in an undefined address range.
Let's see how to change the size of the newly created subnets.
Similar to the virtual network address space, we can change the size of a subnet at any time.
Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.
In order to change the subnet size using the Azure portal, we must take the following steps:
When changing the subnet size, there are some rules that must be followed. We cannot change the address space if it is not within the virtual network address space range, and the subnet range cannot overlap with other subnets in a virtual network. If devices are assigned to this subnet, we cannot change the subnet to exclude the addresses that these devices are already assigned to.
18.118.1.232