Hi All,
I'm very new to PowerCLI and looking to achieve something a little out of my depth.
I work for a cloud provider, we host lots of virtual machines on shared ESX clusters.
These virtual machines are deployed using our own orchestration tools.
Our orchastration tool automatically deploys the VMs to different hosts at creation time but by default, there is nothing preventing DRS from moving them to the same host at some point in the future.
Each set of VMs is typically assigned to a port group specific the client who owns those VMs (VLAN).
Would it be possible to write a powercli script that queried the virtual machines associated with a given portgroup.
Then take that list of virtual machines and built an Anti-Affinity DRS rule which contains that list of servers to keep them apart.
I would like to run this script on a daily basis to ensure that newly deployed servers servers are kept apart automatically.
If this is even possible, any tips on how I might achieve this would be greatly recieved....
Thanks in advance,
Mitch
There seems to be a problem with the New-DrsRule cmdlet in PowerCLI 5.0.1 when creating anti-affinity rules.
As a workaround the following script uses the ReconfigureCluster_Task method
$tgtPg = "MyPG"
$clusterName = "MyCluster"
$cluster = Get-Cluster -Name $clusterName
$vms = Get-VM -Location $cluster | where {$_.NetworkAdapters | where {$_.NetworkName -eq $tgtPg}}
$spec = New-Object VMware.Vim.ClusterConfigSpec
foreach($i in 0..($vms.Count -2)){
foreach($j in (($i + 1)..($vms.Count - 1))){
$rule = New-Object VMware.Vim.ClusterRuleSpec
$rule.Info = New-Object VMware.Vim.ClusterAntiAffinityRuleSpec
$rule.Info.Enabled = $true
$rule.Info.Name = "PGRule" + $i + $j
$rule.Info.Vm += $vms[$i].ExtensionData.MoRef
$rule.Info.Vm += $vms[$j].ExtensionData.MoRef
$spec.rulesSpec += $rule
}
}
$cluster.ExtensionData.ReconfigureCluster($spec,$true)
PS: I don't see why you would need to run this on a daily basis.
If you run this when the VMs are created, the rules will keep those VMs on seperate hosts in the cluster.
Just watch out if you have more VMs than there are nodes in the cluster, then this setup will not work.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks so much LucD, this looks fantastic.
One question before I take it for a spin, I am running vSphere 4.1 at the moment.
Will this script function from powercli 4.1?
Thanks again,
Mitch
PS - I am in awe of the speed at which you were able to knock this together for me.
The anti-affinity rules are available in vSphere 4.1 and since we are using the SDK method, this should work.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc, I saw this post w/ Affinity Rules: http://psvmware.wordpress.com/2012/05/14/new-drsrule-unable-to-cast-object-of-type-vmware-vim-cluste...
And messed around with it a bit. It seems to can do > 2 VMs with Anti-Affinity Rules w/ powershell using this method (don't need the multiple rules for > 2 VMs):
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.rulesSpec = New-Object VMware.Vim.ClusterRuleSpec[] (2)
$spec.rulesSpec[0] = New-Object VMware.Vim.ClusterRuleSpec
$spec.rulesSpec[0].operation = "add"
$spec.rulesSpec[0].info = New-Object VMware.Vim.ClusterAntiAffinityRuleSpec
$spec.rulesSpec[0].info.enabled = $true
$spec.rulesSpec[0].info.name = "AA-vm1-vm2" $spec.rulesSpec[0].info.vm = (get-vm vm1,vm2|get-view) |% {$_.moref}
(Get-View -Id (get-cluster xyz).id).ReconfigureComputeResource_Task($spec, $true)
Jatwell,
im glad that it helped ![]()
Regards,
Greg
