AutoScale your Deployments in vRA with PowerVRA and PowerShell

AutoScale your Deployments in vRA with PowerVRA and PowerShell

AutoScale your Deployments in vRA with PowerVRA and PowerShell

Hello Folks!, Today i wanted to share with you something amazing i was able to get done using PowervRA and vRealize Automation 7.3.

If you have not used it before i would like to tell you its a Gem of a technology built by two amazing gentlemen and PowerShell MVP’s – Jon and Craig , Thanks Guys! for making this and thanks for the all support you extend to integrate PowerVRA with the upcoming releases of vRealize Automation.

This module allows you to automate almost anything in vRA by accessing its Rest API’s.

The module has about 117 cmdlets as of now (PowervRA 2.2.0) which allow you to do this.

Well moving on, you might have noticed i have been hooked onto exploring API’s for vRA with vRO and that did involve me going deep into Java Scripting and lots of learning! to upgrade my skillset as i am basically a PowerShell guy ๐Ÿ™‚ .

Based on the learning posted a series of Blogs on how to invoke AutoScale action’s for a deployment resource in vRA in an automated way, if you have not looked at it till now, please have a look at the set of links posted here which showcase this.

Auto-Scale vRA workloads with vROPS,vRO and NSX

AutoScale vSphere Workloads with vRA and PowerShell based NetScaler NITRO APIโ€™s

VMware WebHook Shim and AutoScale with vRO

Ok so the question which i asked myself is why dont’t i used my existing skills and try to build a solution using PowerShell itself, i tried invoking the REST API’s for vRA but that was a lot of research and to save time i used PowervRA to build this solution.

The solution i built for Scale-Out and Scale-In with PowerShell fully utilizes the cmd’s present in PowerVRA module and a bit of trickery too ๐Ÿ˜‰ to get the data in the right format, you would see this as you read on.

Ok, so let me start with the AutoScale Action

I would be walking you through the script step by step and also give a link to the entire script too – so you can just copy paste it into your Code / ISE environment, make necessary modifications and re-use it.

First connect to your vRA environment with PowerVRA using the below command

Connect-vRAServer -Server 10.110.66.120 -Credential (Get-Credential) -Tenant vsphere.local -IgnoreCertRequirements

Next lets extract the REST API call to get the Parent Resource of the destination machine which needs an auto scale out due to the increase load, we would be extracting the Parent Resources ID.

((Get-vRAResource -Name Web-Tier-137).links | ?{$_.rel -match "GET: Parent Resource"}).href -match "(?:.*?\/){3}(.*)"

$Matches

$Matches[1]

$uri = "/"+$Matches[1]
    
$parresid = (Invoke-vRARestMethod -Method GET -URI $uri).resourceId

Once we extract the Parent Resource ID, lets extract the list of Resource Actions which can be performed on this deployment.

Get-vRAResourceAction -ResourceId $parresid

As you can see below we get a list of all actions which can be performed on the Parent Deployment

Here in our example ๐Ÿ™‚ , we are interested in the Scale-Out Action, so lets extract the resource ID for the same.

Get-vRAResourceAction -ResourceId $parresid | ?{$_.name -match "Scale Out"}

$soutid = (Get-vRAResourceAction -ResourceId $parresid | ?{$_.name -match "Scale Out"}).id

Ok great!, so now that we have the Scale Out ID, lets have a look at how this Resource Action Template for Scaleout looks like, as this is the same format vRA expects to invoke the scale out operations.

$vratemp = Get-vRAResourceActionRequestTemplate -ActionId $soutid -ResourceId $parresid

$vrat = $vratemp | ConvertFrom-Json

Great!, so we have all the details now, ok so in this example we are specifically scaling out the Web-Tier so i would do a bit of data manipulation to clear out the Data format for App and DB Tier as this is something which does not need a scale-out currently, also along with this we need the cluster size to be incremented by 1 so that we get a new Web-Server in the Web-Tier.

$NNCOUNT = ($vrat.data."web-tier".data._cluster)+1

 $vrat.data."web-tier".data._cluster= $NNCOUNT
 
 $vrat.data.'DB-Tier'.data = @{}

 $vrat.data.'App-Tier'.data = @{}

 $vrjson = $vrat | ConvertTo-Json -Depth 3

 $vrjson

Voila!, as you can see now i have the cluster node count for Web-Tier increased to 4 hence we would get a new web-server

Ok so now we have the data in the proper format, lets invoke the rest call to Request a vRA resource action as below. Also as you can see below, i gave a sleep for just 3 seconds so that the request ID is picked up by Get-vRARequest and i placed a while loop so that we wait for the successful completion of the request.

Request-vRAResourceAction -JSON $vrjson -Confirm:$false

Start-sleep 3

$rn = (Get-vRARequest -RequestedBy Administrator@vmwareebc.in |  Sort-Object requestnumber -Descending | select -First 1).RequestNumber

while ((Get-vRARequest -RequestNumber $rn).state -ne "SUCCESSFUL") {Write-Host "Sleeping till Scale-Out Request finishes"; start-sleep 5};Write-Host "Request Succesfuly completed Scaled-Out Deployment"

As you can see below the request has been submitted and we are waiting for it to complete.

Now if i login to my vRA portal we would see that yes!! the Scale-Out Request is indeed in progress

Amazing! isn’t it ๐Ÿ™‚

We would wait now for the Request to complete, once its successful – lets say you want the name of the newly provisioned VM. what if you want to do some day-2 tasks on it lets say update the details in a CMDB etc.

The vRA view

Now what we would do is again head over to the parent resource, then Invoke the vRA REST method to get the child objects, extract only what is required lets say Web-Tier, sort it in a descending order to extract the name of the newly created VM !!

$a=Get-vRAResource -id $parresid

($a.links | ?{$_.rel -match "GET: Child Resources"}).href -match "(?:.*?\/){3}(.*)"

$Matches

$Matches[1]

$uri = "/"+$Matches[1]

$a=(Invoke-vRARestMethod -Method GET -URI $uri)

($a.content | ?{$_.name -match "Web-Tier"} | select name |Sort-Object name -Descending).name[0]

And as you can see the VM Name Matches the above screenshot!

Woah!! now that was one of the most exciting post i have written so far!, and it had been a long dream to do this with PowerShell!

Ok so now for the Scale In Part it Remains the same, the only change we make is to get the Resource ID for the Scale-IN action and along with this reduce the cluster count by 1.

Get-vRAResourceAction -ResourceId $parresid | ?{$_.name -match "Scale In"}

$sinid = (Get-vRAResourceAction -ResourceId $parresid | ?{$_.name -match "Scale In"}).id

Get-vRAResourceActionRequestTemplate -ActionId $sinid -ResourceId $parresid


 $vratemp = Get-vRAResourceActionRequestTemplate -ActionId $sinid -ResourceId $parresid

 $vrat = $vratemp | ConvertFrom-Json

 $NNCOUNT = ($vrat.data."web-tier".data._cluster)-1

 $vrat.data."web-tier".data._cluster= $NNCOUNT
 
 $vrat.data.'DB-Tier'.data = @{}

 $vrat.data.'App-Tier'.data = @{}

 $vrat | ConvertTo-Json -Depth 3

 $vrjson = $vrat | ConvertTo-Json -Depth 3

 $vrjson

 Request-vRAResourceAction -JSON $vrjson -Confirm:$false


$rn = (Get-vRARequest -RequestedBy Administrator@vmwareebc.in |  Sort-Object requestnumber -Descending | select -First 1).RequestNumber

while ((Get-vRARequest -RequestNumber $rn).state -ne "SUCCESSFUL") {Write-Host "Sleeping till Scale-In Request finishes"; start-sleep 5};Write-Host "Request Succesfuly completed Scaled-In Deployment"

Here’s a screenshot from the execution view

If i login to the vRA portal, i can see now the Scale-IN request is in progress.

Wow!, so now you can see the True Power of Automation, if you have access to the REST API’s almost anything is possible, Just imagine if its not only Scale action you can perform on a deployment, it can be used to invoke any resource action which the deployment provides.

Well now if you want to automate it with vRO and vROPS , you can have a look at some of my previous posts on how to invoke these scripts via Web-Hook Shims

I Hope You Enjoyed this blogpost and found the information useful.

Here is the Link to the PowerShell Script in its Raw Format

Share this post

Post Comment