Azure Batch VNet Connection

A recent update to Azure Batch added the ability to join a batch pool to a virtual network. By doing so it is possible for batch compute nodes to access resources inside a vNet (file servers, SQL servers etc.).

vNet Requirements

There are some limitations on the vNet configuration if you wish to do this:

  • Only Cloud Services Configuration pools can be assigned a VNet. This is no longer the case
  • The VNet must be: -  In the same Azure region as the Azure Batch account.
  • In the same subscription as the Azure Batch account.
  • A classic VNet.
  • The VNet should have enough free IP addresses to accommodate the size of the pool
  • The MicrosoftAzureBatch service principal must have the Classic Virtual Machine Contributor Role-Based Access Control (RBAC) role for the specified VNet.
  • The batch service needs to be able to communicate with the pool, ideally this means putting batch nodes in their own subnet with no NSG.
  • This presented me with a few issues, mainly with need for a classic vNet, given that all my resources are in an ARM vNet. Fortunately vNet peering allows us to join a classic vNet to an ARM one with limited ease.

Pool Creation

The joining of a vNet occurs at the time of creating a pool. This can be done using the using the REST API, but we will look at using PowerShell, which isn’t terribly well documented.

The initial part of the PowerShell is fairly straight forward, connect to Azure, select the subscription adn get a batch context for your batch account.

Add-AzureRmAccount 
Select-AzureRmSubscription SubscriptionName "<name of subscription>" 
$batchcontext = Get-AzureRmBatchAccountKeys AccountName

The next part is where we actually configure the vNet using the new PSNetworkConfiguration element of the batch configuration, which we set to the resource ID of the subnet we want to use for batch VM’s.

$vnetconf = New-Object TypeName Microsoft.Azure.Commands.Batch.Models.PSNetworkConfiguration 
$vnetconf.SubnetId = "/subscriptions/4cffbd13-xxxxx-xxxxxx-xxxx/resourceGroups//providers/Microsoft.ClassicNetwork/virtualNetworks//subnets/"

Finally we create a new Cloud Service configuraiton and use that, and the network configuration to create a pool.

$configuration = New-Object TypeName "Microsoft.Azure.Commands.Batch.Models.PSCloudServiceConfiguration" ArgumentList @(4,"*") 
New-AzureBatchPool -Id "" VirtualMachineSize "Standard_D3_v2" TargetDedicated 1 BatchContext $batchcontext NetworkConfiguration $vnetconf CloudServiceConfiguration $configuration

Here’s the script in full, this will create a pool with a dedicated single D3v2 VM, obviously if you wanted to add auto scaling or change machine sizes you would amend the configuration to include this.

Add-AzureRmAccount 
Select-AzureRmSubscription SubscriptionName "<subscription>" 
$batchcontext = Get-AzureRmBatchAccountKeys AccountName $vnetconf = New-Object TypeName Microsoft.Azure.Commands.Batch.Models.PSNetworkConfiguration 
$vnetconf.SubnetId = "/subscriptions/4cffbd13-xxxxx-xxxxxx-xxxx/resourceGroups//providers/Microsoft.ClassicNetwork/virtualNetworks//subnets/" 
$configuration = New-Object TypeName "Microsoft.Azure.Commands.Batch.Models.PSCloudServiceConfiguration" ArgumentList @(4,"*") 
New-AzureBatchPool -Id "" VirtualMachineSize "Standard_D3_v2" TargetDedicated 1 BatchContext $batchcontext NetworkConfiguration $vnetconf CloudServiceConfiguration $configuration

 

Further Reading :
Batch Documents

Batch REST API

Getting Started with Batch Powershell