VM affinity when using vCloud Director and vApps
I made a blog post some time ago, where i created a powerCLI function for creating a VM affinity rule that would keep my VM´s in a vApp together in the cluster for better VM-VM performance. I then checked for MoRef to get the VM correlated between vCloud Director and vCenter.
The answer to my problem was much closer than I thought, the vCloud director creates a folder in the “VM´s and Template view” for each vApp and guess what, all VM´s are in there.. so I do not need to ask the vCloud Director anymore (well if I want the Cloud vApp name I do need to ask it..) but ironically I had not looked there and found out that the folders actually where created for each deployed vCloud vApp :-/
So the code is a bit simpler and to get all VM´s from a folder I just type this, the asterisk is because the folder also gets a unique id
Get-Folder | where {$_.Name -like "ProdvCPod*"} | Get-VM
And then I just need to put those VM´s into an array and create a DRS rule, and here is the function that does this
<# .Synopsis Add an affinity rule for a vCloud Director vAPP .DESCRIPTION This function takes a vApp name (string) as parameter and creates an affinity rule for them to keep them together .EXAMPLE Add-vAppAffinity -CIVApp ProdCust01 .NOTES Author: Niklas Akerlund Date: 2012-06-21 #> function Add-vAppAffinity { Param ( # Parameter for the vAPP [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $CIVApp, # If the rule should apply on a different cluster $Cluster = "Cluster01" ) if ($CIVApp -ne " " -or $CIVApp -ne $null -or $CIVApp.length -ne 0 ){ $Cluster = Get-Cluster $Cluster $VAppname = $CIVApp + "*" $VMs = Get-Folder | where {$_.Name -like $VAppname} | Get-VM if (!(Get-DrsRule -Cluster $Cluster -Name $CIVApp -ErrorAction SilentlyContinue)){ New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs }else{ Remove-DrsRule -Rule (Get-DrsRule -Cluster $Cluster -Name $CIVApp) -Confirm:$false New-DrsRule -Name $CIVApp -Cluster $Cluster -KeepTogether $true -VM $VMs } } }
To run it:
and it looks something like this if I check the cluster settings
preferably you have set your DRS cluster to fully automated or not so much will happen 🙂