Enable and Use Nested Virtualization on Azure Virtual Machine

Nested Virtualization is one of the cool new features in Windows Server 2016 that allows you to install hyper-v, create and run virtual machines inside a hyper-v virtual machine itself. In other words, a hyper-v virtual machine can act as a virtual host server. A great benefit of nested Hyper-V virtualization is for labs and training scenarios where you can, for instance, build a cluster of several virtual Hyper-V hosts on a single physical computer. This also allows one to use hyper-v containers and is required by Docker.

Also, provided you have required resource capacity, there are no depths of this feature. That means, you can create a virtual machine, inside a virtual machine, install hyper-v on guest virtual machine and then create virtual machines inside it. Well if you have seen inception, its something like it.  In this blog post, we will learn how to do the same for Azure Virtual Machine. 

Create a Azure Virtual Machine

First thing first, we need to create a Azure virtual machine with Windows Server 2016 as Operating system installed on it. Also it is to be noted that the Azure Virtual Machine size should be from Dv3 or Ev3 Series only, otherwise it would not work. This is because it requires specific hardware capabilities to enable the same.

You can choose the size of the virtual machine as per your requirement.  Once Virtual Machine is created, login inside the same using administrative credentials.

Install Hyper-V on the Virtual Machine

Open a PowerShell prompt in elevated mode. Run below command to install Hyper-V:

Write-Host "Installing and Enabling Hyper-V..."
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools

We can then restart computer by using below command:

Write-Host "Proceeding to restart computer..."
Restart-Computer
install hyper-v on virtual machine
Install hyper-v on virtual machine

In case, we already have a virtual machine and we want to check if hyper-v is installed or not, we can use below logic:

$hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V -Online
if($hyperv.State -eq "Disabled"){
    Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart
    Restart-Computer
} 
else{
    Write-Host "Hyper-V is already Installed and Enabled. (Skipping Hyper-V installation and Restart)`n"
}

Create Virtual Switch inside Hyper-V

If we need to provide internet connectivity to virtual machines, we can do that using NAT network. In order to provide NAT  network, we first need to create a virtual switch for same.

Again, we need to open PowerShell prompt in elevated mode and run below command:

New-VMSwitch -Name "InternalNATSwitch" -SwitchType Internal

We can then observe the network adapter created or not by using below cmdlet:

Get-NetAdapter

If the first command ran successfully, you can view the  network adapter created with the interface description as ‘Hyper-V Virtual Machine…’:

Create virtual switch and get list of network adapters
Create virtual switch and get list of network adapters

Notice the interface index for network adapter created, which is 13 in our case. Now, we need to create an IP address for NAT gateway. We can do by using below command:

New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 13
Create NAT gateway
Create NAT gateway

Now, we can create NAT network using below command:

New-NetNat -Name "InternalNat" -InternalIPInterfaceAddressPrefix 192.168.0.0/24

Create NAT network

Create Nested Virtual Machines

We can now create guest virtual machines using either the hyper-v console which provides a GUI interface to do the same. We can also choose to create the virtual machines using PowerShell to do the same.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s