SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

View All Comments

vmaffinity

VM Affinity Rules

If a service request regarding VM affinity is received you should follow this procedure to implement the service request:

  1. Check validity of service request
  2. Create groups if required
  3. Create affinity rule
  4. Update documentation

Check validity of service request

VMs can only have a (anti-)affinity if they are part of the same cluster. So always check first if the VMs are in the same cluster. If not, deny the request, if they are, continue.

Create Groups If Required

If the request if to host a VM onto specific hosts you first need to create groups, even if it's just one VM of just one host. VM-Host rules only work with groups.

To create a group follow these steps:

  • Right-click the specific cluster and select “Edit Settings”
  • Go to “VMware DRS” → DRS Groups Manager
  • Click Add on the Virtual Machines DRS groups of Host DRS Group section
  • Enter a name and select the VMs/Hosts that should be in the group
  • Click OK to create the group

Create Affinity Rule

To create the actual affinity rule follow these steps:

  • Right-click the specific cluster and select “Edit Settings”
  • Go to “VMware DRS” → Rules
  • Click Add
  • Enter a name and select the type:
    • Keep Virtual Machines together
    • Separate Virtual Machines
    • Virtual Machines to Hosts
  • Select the required VMs, or if selected the Virtual Machines to Hosts option the required VM and Hosts group you just created.
  • Note that in the last case you also have these options:
    • Must run on hosts in group
    • Should run on hosts in group
    • Must not run on hosts in group
    • Should not run on hosts in group
    • Save the rule by clicking OK

Update Documentation

Because DRS rules can get lost when DRS gets disabled you should document the rules very precise. Luckily there are export and import scrips:

Note: These scripts are originally created by LucD

Export Script

$outfile = "C:\rules.txt"
Remove-Item $outfile
$clusterName = <cluster-name>
$rules = get-cluster -Name $clusterName | Get-DrsRule
 
foreach($rule in $rules){
  $line = (Get-View -Id $rule.ClusterId).Name
  $line += ("," + $rule.Name + "," + $rule.Enabled + "," + $rule.KeepTogether)
  foreach($vmId in $rule.VMIds){
    $line += ("," + (Get-View -Id $vmId).Name)
  }
  $line | Out-File -Append $outfile 
}

Import Script

$file = "C:\rules.txt"
$rules = Get-Content $file
 
foreach($rule in $rules){
  $ruleArr = $rule.Split(",")
  if($ruleArr[2] -eq "True"){$rEnabled = $true} else {$rEnabled = $false}
  if($ruleArr[3] -eq "True"){$rTogether = $true} else {$rTogether = $false}
  get-cluster $ruleArr[0] | `
    New-DrsRule -Name $ruleArr[1] -Enabled $rEnabled -KeepTogether $rTogether -VM (Get-VM -Name ($ruleArr[4..($ruleArr.Count - 1)])) 
}
You could leave a comment if you were logged in.
vmaffinity.txt · Last modified: 2021/09/24 00:25 (external edit)