affinity group for a virtual machine, use cluster.exe or PowerShell:
(Get-ClusterGroup "<VM>").AntiAffinityClassNames = "
<AntiAffinityGroupName>"
cluster.exe group "<VM>" /prop AntiAffinityClassNames="
<AntiAffinityGroupName>"
The cluster affinity can be set graphically by using SCVMM, as shown in Figure 7.13.
SCVMM uses availability set as the nomenclature instead of anti-affinity group. Open
the properties of the virtual machine in SCVMM, navigate to the Hardware
Configuration tab, and select Availability under the Advanced section. Use the Manage
Availability Sets button to create new sets and then add them to the virtual machine. A
single virtual machine can be a member of multiple availability sets.
Figure 7.13 Setting affinity by using SCVMM
By default, this anti-affinity solution is a soft enforcement: Clustering will do its best
to keep virtual machines in the same anti-affinity group on separate hosts, but if it has
no choice, it will place instances on the same host. This enforcement can be set to
hard by setting the cluster ClusterEnforcedAntiAffinity attribute to 1, but this may
mean that virtual machines may not be able to be started.
For virtual machines that are clustered, it is possible to set the preferred owners for
each virtual machine and set the order of their preference. However, it’s important to
realize that just because a host is not set as a preferred owner for a resource (virtual
machine), that doesn’t mean that the host can’t still run that resource if none of the
preferred owners are available. To set the preferred owners, right-click a VM resource
and select Properties, and in the General tab, set the preferred owners and the order as
required.