in dev

Script: criando vm’s no Azure a partir de um vhd

Recentemente participei de um Codificando Live motrando um pouco do que fizemos para escalar a infraestrutura que temos no Dieta e Saúde no Azure. O projeto foi necessário pois teriamos um destaque na TV e nosso volume de usuários aumentaria em 30x, com a ajuda do Azure e da Azion (CDN que utilizamos) replicamos o ambiente do datacenter no Azure e balanceamos a carga entre o Azure e o Datacenter.

Este foi o script de exemplo que utilizei para criar as vm’s com base em um vhd que fizemos upload. Lembrando que tive que baixar os módulos de PowerShell para o uso das apis do Azure (https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/)

Login-AzureRmAccount

Select-AzureRmSubscription -SubscriptionId "xxxx-xxxx-xxx-xxxx-xxx"
$rgName = "resourcegrup"
$location = "brazilsouth"
$storageName="storage"


New-AzureRmResourceGroup -Name $rgName -Location $location

New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $storageName -Location $location -SkuName "Standard_LRS" -Kind "Storage"

$urlOfUploadedImageVhd="https://"+$storageName+".blob.core.windows.net/vhds/web.vhd"

Add-AzureRmVhd -ResourceGroupName $rgName -Destination $urlOfUploadedImageVhd -LocalFilePath "E:\VHD\web.vhd"

$avSet =New-AzureRmAvailabilitySet -Location $location -Name "dsavaliability" -ResourceGroupName $rgName -PlatformUpdateDomainCount 4 -PlatformFaultDomainCount 1


$AsObject = New-Object Microsoft.Azure.Management.Compute.Models.SubResource
$AsObject.Id = $avSet.Id


$cred = Get-Credential

$rule1 = New-AzureRmNetworkSecurityRuleConfig -Name rdp-rule -Description "Allow RDP" `
    -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 `
    -SourceAddressPrefix Internet -SourcePortRange * `
    -DestinationAddressPrefix * -DestinationPortRange 3389

$rule2 = New-AzureRmNetworkSecurityRuleConfig -Name web-rule -Description "Allow HTTP" `
    -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 `
    -SourceAddressPrefix Internet -SourcePortRange * `
    -DestinationAddressPrefix * -DestinationPortRange 80
$nsgName="NSG-FrontEnd"

$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location -Name $nsgname `
    -SecurityRules $rule1,$rule2

$subnetName = "subnet"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix "192.168.32.0/25" 

$vnetName = "vnet"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location -AddressPrefix "192.168.32.0/25" -Subnet $singleSubnet 

for($i=0; $i -le 10; $i++){
    

	$indice=$i.ToString()
	$ipName = "ip0"+$indice
	$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic

	$nicName = "nic0"+$indice
	$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id


	# Name of the storage account where the VHD file is and where the OS disk will be created
	$storageAccName = $storageName

	# Name of the virtual machine
	$vmName = "web0"+$indice

	# Size of the virtual machine. See the VM sizes documentation for more information: https://azure.microsoft.com/documentation/articles/virtual-machines-windows-sizes/
	$vmSize = "Standard_A3"

	# Computer name for the VM
	$computerName = "web0"+$indice

	# Name of the disk that holds the OS
	$osDiskName = "webv20"+$indice

	$storageAcc = Get-A zureRmStorageAccount -ResourceGroupName $rgName -AccountName $storageAccName

	$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize

	$vm = Set-AzureRmVMOperatingSystem -VM $vmConfig -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

	$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

	$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f $storageAcc.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $osDiskName

	$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $urlOfUploadedImageVhd -Windows
	$vm.AvailabilitySetReference=$AsObject 


	New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm

}

https://gist.github.com/rodolfofadino/46456febe76c83943d1051322820ea06

https://www.youtube.com/watch?v=3PlGH8cM3ds

Espero que este post seja útil estou a disposição para dúvida criticas e sugestões.

abs

Rodolfo