DXfoto.com - Fotolia

Manage Learn to apply best practices and optimize your operations.

Use affinity and anti-affinity rules to guide VM behavior

Affinity rules define the relationships between VMs and hosts. You can create them in DRS, and then use them to improve failover and optimize VM performance.

Employing affinity and anti-affinity rules can help you manage data center operations by guiding interactions between VMs and hosts. You can use them with Distributed Resource Scheduler or High Availability to ensure availability, add resiliency, optimize VM performance and improve failover or apply them to vCPUs to hyper-thread your processors.

An affinity or anti-affinity rule establishes a relationship between a VM and a host by telling the hypervisor to keep specific entities together or separate to keep traffic and workloads balanced on available hosts.

You can easily apply affinity rules through the vSphere Web Client. When affinity and anti-affinity rules conflict, you can edit or disable a rule.

What are affinity and anti-affinity rules?

VM-host affinity and anti-affinity rules define the relationship between a VM group and a host group. VM-host rules are cluster-based, which means they require VMs and hosts to be in the same cluster. A VM-host affinity rule means VMs from a specific Distributed Resource Scheduler group must run on members of a host DRS group. A VM-host anti-affinity rule, on the other hand, stipulates that certain VMs of a DRS group can't run on members of a particular host DRS group.

VM-VM rules, meanwhile, define affinity between specific VMs. A VM-VM affinity rule stipulates that certain VMs should share the same host, whereas a VM-VM anti-affinity rule should prevent specific VMs from running on the same host. Running multiple VMs on the same host can enhance their performance, but separating VMs onto separate hosts increases resiliency.

Keep in mind that affinity and anti-affinity, despite the names, aren't mutually exclusive. An affinity for one host isn't automatic anti-affinity for all other hosts; anti-affinity to a certain host doesn't necessarily specify affinity for another.

Applying affinity rules to DRS

To create a VM-host affinity rule, select the desired cluster in the vSphere Web Client. Click through Manage > Settings > DRS Rules > Add. Name your new rule, and then select Virtual Machines to Hosts from the Type drop-down menu. Select the desired VM DRS group to which you want to apply your rule. Then, select an affinity or anti-affinity type, save your changes and implement the rule.

You can create and apply a VM-VM affinity or anti-affinity rule to DRS in a similar fashion. Much like creating a VM-host rule, select your desired cluster in the vSphere Web Client to start, then click through the same Manage > Settings> DRS Rules > Add selections and enter a name for your rule. In the Type drop-down menu, select either Keep Virtual Machines Together to create an affinity rule or Separate Virtual Machines to create an anti-affinity rule. Click Add and select two or more VMs to which you want to apply the rule.

Affinity rules and VM failover

When you apply affinity rules, consider where a VM should or should not restart in your data center.

DRS emphasizes the use of rules for VM placement. High Availability (HA) prioritizes the maintenance of availability. HA failover events might not always obey affinity and anti-affinity rules, but DRS obeys rules as strictly as possible.

You can use affinity and anti-affinity rules to improve VM failover. VMs rarely operate alone in modern data centers, and you can improve resiliency by organizing multiple instances of a VM into a cluster. That way, when a failover event occurs, other nodes in the cluster can pick up the slack. They can continue to support workload availability as the VM in question restarts.

When you apply affinity rules, consider where a VM should or shouldn't restart in your data center. Keep in mind a VM's specific storage, network and compute requirements, as well as its access to other related VMs. Apply rules that enforce VM placement behavior.

Using affinity and anti-affinity rules with vCPUs

Hypervisors enable IT administrators to employ affinity between vCPUs and VMs. VCPU affinity relates to hyper-threading your processors, which keeps CPUs busy when they might otherwise be idle. You can use affinity and anti-affinity to manage vCPU distribution.

Proper vCPU distribution alleviates potential problems and optimizes VM performance by ensuring specific CPUs aren't encumbered with workloads they can't handle. VCPUs don't require affinity and anti-affinity rules, but they can help optimize compute-intensive workloads.

Violating affinity or anti-affinity rules

When a conflict occurs between VM-VM affinity rules, the older rule takes precedence over newer rules. Anti-affinity rules typically take precedence over affinity rules. All VM-host rules are applied equally whether affinity or anti-affinity, but older rules still take precedence in the case of a rule conflict.

When a rule conflict arises, it can cause performance problems. DRS or HA might not work properly. For example, DRS will refuse to migrate VMs for host maintenance if the migration violates a rule and HA won't fail over VMs.

When rules conflict, you can simply edit or disable a conflicting rule to keep your environment running smoothly. Keep in mind limitations and issues when setting up affinity and anti-affinity rules.

Dig Deeper on VMware infrastructure management services