VMware Cloud Community
drwoodberry
Contributor
Contributor

Randomizing datastore selection.

I create virtual machines via a script that randomizes the datastore the guest is put on. The -Datastore parameter I am using to do this is "

-Datastore ($myds.getvalue($rnd.next(0,$myds.count)))" where $rnd is the random number generator "

$rnd = New-Object System.Random" and $myds is "$myds = Get-datastore | where {$_.Name -match "DS"}" that pertains to how a name my datastores.

The randomization works great to a point. Sometimes the guest machine is attempted to be placed in a datastore that has no more availble space. The question is, would it be best to round robin the guest machines so that they are evenly distributed. This decreases the chance that one datastore will hold so many more VM's over the others, and therefore reduce the risk of running out of space? Should I put this into a try-catch block to catch errors.

Just trying to figure out the best way to do this without having to worry about not having enough space. Error checking is obviously needed.

Thanks in advance.

Reply
0 Kudos
2 Replies
bulletprooffool
Champion
Champion

Personally, I'd take free space into account, and number of VMs on the DS.

It is possible for a random number generator to get the same Datastore several times in a row, so I'd go with something that  does the following

  • Query all Datastores, to find any that have enough space for the VM and will still have 20%(or whatever you favour) available free
  • From this selection, calculate the DS with the fewest running VMDKs

Of course if you are feeling really clever, you could interrogate perfromance stats for the past month and go with the least loaded one? - either way, random number generator is not ideal

One day I will virtualise myself . . .
Reply
0 Kudos
bulletprooffool
Champion
Champion

Also, try-catch is not the way to do it, rather a manual check (intelligent decision)

get-datastore returns FreeSpaceMB, so the calculation is very simple (just extend your where statement)

One day I will virtualise myself . . .
Reply
0 Kudos