VMware vCloud Director vApp and DRS cluster affinity with PowerCLI
To fully utilize the performance in a vApp in vCloud Director I got the task to create an affinity rule based on the VMs in the vApp. This can be the case for example when you have VMs in an vApp that exchange high loads of data. In our case we have virtual ESXi Servers that have a vsa for shared storage and need good performance when deploying VM´s and storage vMotion etc.
When deploying several vApps from the same template it is not just to run Get-CIVM and then use the VM name returned to run Get-VM for the correlation between the VM in VCD and in vCenter, as you can see in the screendumps these two have the same name but followed with an identifier from vCD in the vSphere Client. This is also described in the vSphere blog.
The fine part is that the MoRef is unique (in the relation one vcd <-> one vcenter) so I can check on that which VMs in the vCenter belongs to the same vCloud Director vApp.
I got the code for the MoRef in this community post.
And here is my script
<# .Synopsis Add an affinity rule for a vCloud Director vAPP .DESCRIPTION This function takes a vApp as parameter and creates an affinity rule for them to keep them together .EXAMPLE Add-CIVAppAffinity -CIVApp Green01 .NOTES Author: Niklas Akerlund / RTS Date: 2012-05-03 #> function Add-CIVAppAffinity { Param ( # Parameter for the vAPP [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $CIVApp, # If the rule should apply on a different cluster $Cluster = "vCD-Cluster" ) $pod = Get-CIVapp $CIVApp if ($pod){ $PodVMs = Get-CIVM -VApp $pod $VMs = @() $Cluster = Get-Cluster $Cluster Foreach ($PodVM in $PodVMs) { $VMname = $PodVM.Name + "*" $VM = Get-VM $VMname | where {$_.ExtensionData.MoRef.Value -eq $PodVM.ExtensionData.VCloudExtension[0].Any[0].VmVimObjectRef.MoRef} $VMs +=$VM } if (!(Get-DrsRule -Cluster $Cluster -Name $pod.Name -ErrorAction SilentlyContinue)){ New-DrsRule -Name $pod.Name -Cluster $Cluster -KeepTogether $true -VM $VMs }else{ Remove-DrsRule -Rule (Get-DrsRule -Cluster $Cluster -Name $pod.Name) -Confirm:$false New-DrsRule -Name $pod.Name -Cluster $Cluster -KeepTogether $true -VM $VMs } } }
And when checking the cluster i can see that my DRS affinity rule has been created and the VMs have been migrated to the same host
It is not so extensive but it helps us with the case to create DRS affinity on the VMs in a particular vApp 🙂 , The Rule is deleted when the vApp is removed, I have an extra check in the script and remove it if it still exist of some reason.
Comments
Great work Niklas as usual!
Is our deployment script updated to use this now?
[…] made a blog post some time ago, where i created a powerCLI function for creating a VM affinity rule that would keep […]
Mit diesem anhang bekommst du automatisch das vC Cluster object das dem PvDC entspricht.
#$pvdc = Provider vCD reference
$rps=Get-ResourcePool
$cluster_moref=$pvdc.ExtensionData.ResourcePoolRefs.VimObjectRef[0].MoRef
foreach ($rp in $rps) {
if ($rp.Id -eq (“ResourcePool-{0}” -f $cluster_moref)) {
$cluster=$rp.Parent.Parent
}
}
Perfect example…:)
Did you maybe have a chance to work with vApps which use vShield Edge Device (for example NAT’ed vApp Network) and that vse device should be also placed into the DRS rule…??
Thanks,
Greg