Using Affinity and Anti-Affinity Rules in Azure Stack HCI and Windows Server Hyper-V

Network Security

In this article, I will explain what are affinity and anti-affinity rules and how to use them with Azure Stack HCI and Windows Server Hyper-V. If you’ve been using other hypervisors, you may already be familiar with affinity and anti-affinity rules, which are used to define the relationship between virtual machines and hosts.

What are affinity and anti-affinity rules?

Affinity and anti-affinity rules describe how virtual machines on a hypervisor are placed next to each other when deployed and in operation. Let’s dive into details about how they work.

Affinity Rules

When configuring the relationship between two virtual machines with an affinity rule, you want to keep them as close as possible. In the best case, you want to keep them on the same hypervisor host.

You can use an affinity rule to keep two VMs as close as possible
You can use an affinity rule to keep two VMs as close as possible (Image credit: Flo Fox/Petri)

Normally, you use affinity rules to keep systems close to each other. That is often required for virtual machines that need very fast and reliable communication with each other.

A good use case could be a high-performance compute cluster where very fast communication for calculations is more important than redundancy. Another example could be frontend and backend systems where the frontend needs very fast communication with the backend system.

An Azure Stack HCI cluster designed for high performance
An affinity rule can ensure very fast communication in a high-performance compute cluster (Image credit: Flo Fox/Petri)

Anti-affinity rules

Anti-affinity rules are the direct opposite of affinity rules: Those rules are used to ensure that virtual machines are never running on the same hypervisor host.

Anti-affinity rules are used to ensure that VMs are never running on the same hypervisor host
Anti-affinity rules are used to ensure that VMs are never running on the same hypervisor host (Image credit: Flo Fox/Petri)

You may want to use anti-affinity rules for virtual machines that are, for example, running as a cluster or hosting the same service and never should fail at the same time if the host fails. A good example of such a service would be a Windows Server Active Directory domain controller: You should normally have a minimum of two domain controllers, though you don’t want to have them running on the same host to prevent any outages of your directory service.

In any case, clustered systems are always a good use case for using anti-affinity rules.

Clustered systems are always a good use case
Clustered systems are always a good use case for using anti-affinity rules (Image credit: Flo Fox/Petri)

Why combine affinity and anti-affinity rules in Azure Stack HCI

In more complex environments with larger application systems depending on several frontend and backend application servers, you may want to combine affinity and anti-affinity rules to improve performance while keeping availability as high as possible.

In such a case, you would for example put frontend and backend application services in an affinity group and the backend systems in a separate anti-affinity group. 

You may want to combine the two types of rules to improve performance in complex systems
You may want to combine the two types of rules to improve performance in complex systems (Image credit: Flo Fox/Petri)

In Azure Stack HCI, you can create server and site-based affinity and anti-affinity rules. You have the option to create those via PowerShell and the Windows Admin Center. To learn more, Microsoft created a great guide for creating server and site affinity rules for virtual machines.

What could impact the placement of virtual machines?

Normally, an Azure Stack HCI cluster should be perfectly able to place your virtual machines using the affinity rules you defined. However, the following are factors that could negatively impact that placement:

  • Maintenance: During maintenance, you may have not enough host resources or servers available. That could either result in virtual machines running in a not desired host combination, or your automated maintenance stopping because it cannot maintain the availability.
  • Host outages: You may not have enough host resources or servers available. That could result in virtual machines running in a not desired host combination.
  • Contradictory rules: There’s always the possibility that the rules you created are contradictory, and that could impact your placement. Every time you’re planning your rules, think them through a few more times to make sure you don’t miss anything.
  • Virtual machine load balancing: This feature allowing you to optimize server utilization in your clusters may also impact the placement of virtual machines.

Depending on your virtual machine load balancing configuration, your virtual machines could move around and may not run optimally for your application systems and services. It is normally a good thing to keep load balancing aggressiveness medium to low. That helps to prevent unpredicted moving around of virtual machines.

Conclusion

Affinity and anti-affinity rules are very important tools in your toolset for Windows Server Hyper-V and Azure Stack HCI. It helps you increase your virtual machine performance and availably of critical workloads. In any case, you should always try to use them to optimize your environment.