1. Azure Virtual Network

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:

  • Creating a virtual network in the Azure portal
  • Creating a virtual network with PowerShell
  • Adding a subnet in the Azure portal
  • Adding a subnet with PowerShell
  • Changing the address space size
  • Changing the subnet size

Technical requirements

For this chapter, the following is required:

  • An Azure subscription
  • Azure PowerShell

The code samples can be found at https://github.com/PacktPublishing/Azure-Networking-Cookbook-Second-Edition/tree/master/Chapter01.

Creating a virtual network in the Azure portal

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.

Getting ready

Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.

How to do it…

In order to create a new virtual network using the Azure portal, take the following steps:

  1. In the Azure portal, select Create a resource and choose Virtual network under Networking (or search for virtual network in the search bar). A new pane will open, where we need to provide information for the virtual network. First, select the Subscription option we want to use and the Resource group option for where the virtual network will be deployed. Then, include a name and select a region (of the Azure datacenter) for where the virtual network will be deployed. An example is shown in Figure 1.1:
    Creating a virtual network in the Basics pane
    Figure 1.1: Creating an Azure virtual network
  2. In the next pane, we first need to define the address space and define the Subnet name and Subnet address range values for the first subnet. After the address space is defined, as shown in Figure 1.2, we will receive a message stating that This virtual network doesn't have any subnets. Therefore, we need to select the Add subnet option:
    Configuring a virtual network address space and subnet
    Figure 1.2: Configuring a virtual network address space and subnet
  3. In the Add subnet pane, we need to define Subnet name and Subnet address range. Optionally, we can add service endpoints we want to connect to the virtual network. Service endpoints allow us to connect to Azure services in a secure way, over Azure backbone infrastructure, without needing a public IP address. An example is shown in Figure 1.3:
    Adding a subnet by providing various information in the Add subnet pane
    Figure 1.3: Adding a subnet
  4. After we have added the first subnet, in our case, FrontEnd, we can add more subnets to the virtual network or proceed to the Security section, as shown in Figure 1.4:
    Adding the FrontEnd subnet in the IP Addresses pane
    Figure 1.4: Adding the FrontEnd subnet
  5. In the Security section, we can choose whether we want to enable Bastion Host, DDoS protection, and Firewall. If any of these options are enabled, we need to provide additional information for that service. Afterward, we can optionally add tags, or skip that and create the service. An example is shown in Figure 1.5:
    Toggling security options in the Security pane
    Figure 1.5: Toggling security options
  6. Creating a virtual network usually does not take much time and should be completed in under two minutes. Once the deployment is finished, we can start using the virtual network.

How it works…

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).

Creating a virtual network with PowerShell

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.

Getting ready

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:

Output displaying the connection to an Azure subscription from PowerShell
Figure 1.6: Connecting to an Azure subscription from PowerShell

How to do it…

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:

Output displaying the deployment of an Azure virtual network
Figure 1.7: Deploying an Azure virtual network using a script

How it works…

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.

Adding a subnet in the Azure portal

In addition to adding subnets while creating a virtual network, we can add additional subnets to our network at any time.

Getting ready

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.

How to do it…

In order to add a subnet to a virtual network using the Azure portal, we must take the following steps:

  1. In the Virtual network pane, go to the Subnets section.
  2. Select the Add subnet option.
  3. A new pane will open. We need to provide information for the subnet, including the Name value and the Address range value in CIDR format. The Address range value must be in the range limit of the virtual network address range and cannot overlap with the address range of other subnets in the virtual network. Optionally, we can add information for Network security group, Route table, Service endpoints, and Subnet delegation. These options will be covered in later recipes:
    Providing various information for the subnet in the Add subnet pane
    Figure 1.8: Adding the address range
  4. We can also add a gateway subnet in the same pane. To add a gateway subnet, select the Gateway subnet option.

    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:

    Adding a gateway subnet for a virtual network by defining the address range
    Figure 1.9: Adding a gateway subnet for a virtual network
  5. After the subnets are added, we can see the newly created subnets in the Subnets pane under the virtual network:
    Viewing newly created subnets in the Subnets pane
Figure 1.10: Viewing newly created subnets in the Subnets pane

How it works…

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.

Adding a subnet with 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.

Getting ready

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'

How to do it…

  1. To add a subnet to the virtual network using PowerShell, we need to execute a command and provide the name and address prefix. The address prefix is again in CIDR format:

    Add-AzVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix 10.11.0.0/24 -VirtualNetwork $VirtualNetwork

  2. We need to confirm these changes by executing the following command:

    $VirtualNetwork | Set-AzVirtualNetwork

  3. We can add an additional subnet by running all commands in a single step, as follows:

    $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

How it works…

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.

There's more…

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

Changing the address space size

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.

Getting ready

Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.

How to do it…

In order to change the address space size for a virtual network using the Azure portal, we must observe the following steps:

  1. In the Virtual network pane, locate Address space under Settings.
  2. Next, click on Address space and change the value to the desired range. An example is shown in Figure 1.11:
    Changing the address space value to the desired range in the Address space pane
    Figure 1.11: Changing the address space range
  3. After you have entered a new value for Address space, click Save to apply the changes.

How it works…

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.

Changing the subnet size

Similar to the virtual network address space, we can change the size of a subnet at any time.

Getting ready

Before you start, open a web browser and go to the Azure portal at https://portal.azure.com.

How to do it…

In order to change the subnet size using the Azure portal, we must take the following steps:

  1. In the Virtual network pane, select the Subnets option.
  2. Select the subnet you want to change. In the Subnets option, enter a new value for the subnet size under Address range. An example of how to do this is shown in Figure 1.12:
    Entering the new value in the address range of the FrontEnd pane
    Figure 1.12: Changing the subnet size using the Azure portal
  3. After entering a new address range value, click Save.
  4. In the Subnets list, you can see that the changes have been applied and the address space has changed, as shown in Figure 1.13:
    Viewing the changes that are applied in the address space
Figure 1.13: Viewing changes made in the subnet address range

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.1.232