DRS affinity and anti-affinity rules help Distributed Resource Scheduler (DRS) perform better placements of virtual machines by understanding the application dependencies and availability. In this post, I will show you how to create affinity and anti-affinity rules in the vSphere Web Client, and I will give some real-world examples.

Mohammed Raffic

Mohammed Raffic Kajamoideen is a subject matter expert in VMware and Microsoft virtualization, a vExpert 2014-2017, VCIX-NV, VCAP4/5-DCA, VCP, CCA, and MCSA. He blogs at vmwarearena.com and can be found on Twitter at @vmwarearena.

The Distributed Resource Scheduler (DRS) will automatically balance the virtual machines (VMs) in the vSphere cluster based on resource usage. Most of the time, DRS does its job perfectly. But because DRS is not aware of applications running on the VM and its dependencies, we need to help DRS optimize the placement of VMs on ESXi hosts in some cases. Affinity and anti-affinity rules can help define the limitations and constraints when it comes to moving VMs.

Creating a VM affinity rule ^

You can create a VM affinity rule to place a specific group of VMs on a single ESXi host. When you are running a multi-tier application, for instance a web server, an application server, and a backend database server, those three tiers communicate intensely. If the tiers are on different ESXi hosts, this will result in heavy network traffic between the hosts.

To improve performance, we can create an affinity rule to ensure the multi-tier application always resides on the same ESXi host. If you manually move a VM, DRS considers the affinity rule and migrates the other two VMs to the same host, or it moves the manually moved VM back to the original host. When providing recommendations, vSphere DRS considers performance optimization.

DRS migrates the VM according to affinity rules if it is in automated mode. When DRS is in partially automated or manual mode, it only offers recommendations to migrate a VM to comply with the affinity rules.

Log in to vCenter Server using the vSphere Web Client. Select the vSphere cluster where you want to create VM affinity rules and then click Configure -> VM/Host Rules -> Add.

Create a VM affinity rule in the DRS Cluster

Create a VM affinity rule in the DRS Cluster

Specify the name for the anti-affinity rule, select Keep Virtual Machines Together from the Type drop-down, and click Add.

Select the rule type Keep Virtual Machines Together

Select the rule type Keep Virtual Machines Together

Select the VMs from the list, click OK to add VMs to the affinity rules, and click OK to create the affinity rule.

Add VMs to the DRS affinity rule

Add VMs to the DRS affinity rule

Ensure the Enable Rule checkbox is selected. Click on OK.

Enable the affinity rule in the DRS Cluster

Enable the affinity rule in the DRS Cluster

This will create the affinity rule. You will be able to see the created rules under the VM/Host Rules section of the vSphere cluster.

View affinity rules under the DRS cluster

View affinity rules under the DRS cluster

After creating the affinity rule, I noticed DRS migrated all three VMs that were part of the affinity rule into a single ESXi host to comply with the affinity rule.

VM placement on ESXi hosts after the DRS affinity rule

VM placement on ESXi hosts after the DRS affinity rule

I tried to migrate one of the multi-tiered application VMs (App-1 in the screenshot above) to a different ESXi host manually. DRS automatically started the migration of two other VMs (DB-1 and Web-1) to the same host to comply with the affinity rules. Even in a second attempt, DRS migrated back the manually vMotioned VM to the same host.

VMware vMotion initiated by DRS affinity rule

VMware vMotion initiated by DRS affinity rule

I changed the DRS mode to partially automated to understand the DRS recommendations for affinity rules. The partially automated DRS mode will display the migration recommendation and perform the initial placement when powering on the VM. After changing DRS to partially automated, I migrated the database VM (DB-1) to a different ESXi host.

Let's see what DRS recommends in partially automated mode. To see the DRS recommendations, select the ESXi cluster -> Monitor -> vSphere DRS -> Run DRS Now, which will manually run DRS immediately. Otherwise, DRS will run as per its time interval.

DRS displays the recommendation to migrate the VM DB-1 back to the ESXi host where other two VMs are running. You can click on Apply Recommendation to apply the DRS recommendation.

View and apply DRS recommendations

View and apply DRS recommendations

Creating DRS anti-affinity rules ^

Anti-affinity rules are just the opposite of affinity rules. You can create an anti-affinity rule to place a specific group of VMs across multiple hosts by separating each VM in the group to run on a different ESXi host. This will improve redundancy.

You can create anti-affinity rules to separate VMs. The simple use case for anti-affinity is when you run a cluster with applications such as Microsoft SQL or Exchange to ensure high availability (HA). If the application cluster nodes are running on same ESXi host, you will lose the entire application cluster when the host fails.

HA will restart all the application node VMs on a different ESXi host. This means downtime to the application even though you are running a cluster. To avoid this failure, we can create anti-affinity rules to separate VMs in the clustered group from each other. This will place each node of the application cluster VMs on different ESXi hosts in the vSphere cluster. As a result, this provides additional availability along with the application level cluster.

Creating anti-affinity rules works the same way as with affinity rules. The only difference is to select the type as Separate Virtual Machines instead of Keep Virtual Machines Together.

Specify the name for the anti-affinity rule, select Separate Virtual Machines from the Type drop-down, and then click add. Select the VMs to add to the anti-affinity rule and click OK.

Create a VM anti affinity rule in the DRS Cluster

Create a VM anti affinity rule in the DRS Cluster

After creation of the anti-affinity rule, DRS migrates the VMs to separate ESXi hosts to comply with the rule.

Figure 10. VM placement on the ESXi host after anti-affinity rule creation

Note that if your configuration violates DRS rules—say you have three nodes in your cluster but only two ESXi hosts—an error message will appear under DRS Faults.

View DRS faults in vSphere Web Client

View DRS faults in vSphere Web Client

Maintenance mode and anti-affinity rules ^

We have created anti-affinity rules to separate VMs from each other. We now need to understand what will happen when we place the ESXi host in maintenance mode. I created an anti-affinity rule to separate two VMs (Node-1 and Node -2) from each other in a two-node ESXi cluster.

VM and host view in the DRS cluster

VM and host view in the DRS cluster

When I try to place the ESXi host (esxic-lab-1) into maintenance mode, vSphere displays a warning message:

The following hosts are in a DRS cluster: … One or more virtual machines might need to be migrated to another host in the cluster, or powered off, before the requested operation can proceed.

ESXi host maintenance mode warning message

ESXi host maintenance mode warning message

The ESXi host is not entering maintenance mode even though it is in fully automated DRS mode. It displays another error message:

Unable to automatically migrate the virtual machine Node-1 from the ESXi host esxic-lab-1.

When this happens, you don't get any warning or dialog pop-up. You need to go to the Tasks & Events of the ESXi host manually to understand the reason the host did not enter into maintenance mode.

Related events in ESXi host maintenance mode

Related events in ESXi host maintenance mode

In this case, we have to disable the anti-affinity rule temporarily or manually migrate the VM to a different ESXi host, thus allowing the host (esxic-lab-1) to enter maintenance mode. As a permanent solution, we can add an additional host to the cluster.

DRS rules in conflict ^

We can create multiple VM affinity and anti-affinity rules. If two rules are in conflict, you can't enable them. For example, I created an anti-affinity rule to separate the VMs "Loginsight-1" and "Loginsight-2," and I am trying to create an affinity rule to keep the same VMs together. This produces an error message:

DRS conflicting rules

DRS conflicting rules

The DRS rule is in conflict with the existing DRS rules in the cluster. It will be disabled. The rule can be enabled manually when the conflicts are resolved.

View conflicting rules under VM:Host Rules in the DRS Cluster

View conflicting rules under VM:Host Rules in the DRS Cluster

When two rules conflict, the older one takes precedence, and DRS will disable the newer rule. DRS will satisfy the enabled rules and will ignore the disabled rules. It gives higher precedence to preventing violations of anti-affinity rules than violations of affinity rules.

Win the monthly 4sysops member prize for IT pros

9+

Users who have LIKED this post:

  • avatar
Share
4 Comments
  1. Mike 1 year ago

    Mohammed,

    Thank you very much for this extremely beneficial documentation.  I have been trying to find a good document with more details about relationships between rules, the configuration of the rules, and DRS and you hit all of these.

    I appreciate your time and effort into this post.

    6+

  2. kishan 9 months ago

    Can you help me with esxcli command to fetch Affinity rule for VM

    0

  3. chandrakant 2 months ago

    Realy very well explained , I appreciate your time and effort into this post.

    0

  4. S Mohamad 4 weeks ago

    Thank you for explain infinity rulls

    it is Very complete

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account