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 🙂

Comments

Timo Sugliani
Reply

Great script idea !

I think you are still missing some crucial checks, as checking the name unicity.
You would need to get your vApp from the vCloud API, then use the admin extension api to fetch the underlying VM moref, to ensure you are creating a rule on the correct VMs.

Example, if you have 2 vApps with the same name, in 2 different organizations, you will probably set the affinity rules to both of them right now, is that correct ?
You can also automate the cluster stuff with the vCloud API / VIM API 🙂

But I’m sure you will enhance this script to take care of this and add another nifty options, like separate vms, depending on some Custom Properties or Metadata 🙂

Great work !

Niklas
Reply

Yes you are right, as you might have seen, I made a script that actually checked the moref of the vms from the vCloud with the vm´s in the vCenter in an earlier post..

This script was a bit simplified, and in our environment it works as we do not have two vApps with the same name 🙂

Probably when I get some time I might extend the functionality

Leave a Reply to VCAP-CID Study Notes: Objective 2.4 - VMiceCancel reply

name*

email* (not published)

website