VMware Cloud Community
Raghavendra2019
Contributor
Contributor
Jump to solution

VM-VM Anti affinity

Hi Team,

I'm facing an issue with VM-VM Anti-affinity, requirement is to keep the VMs in separate host, but I don't want this to be mandatory(only best effort). I have created the rule with mandatory=False as shown below. To test this I created two VMs on a same node and applied the rule, then I stopped both the VMs are tried to start them, then one of the node was not started with error

"This operation would violate a virtual machine affinity/anti-affinity rule". As I had chosen it as an optional rule, I was expecting both VMs to come up on same host. Am I missing something. Thanks in advance for any help in this regard.

As per the document

mandatory*boolean

Flag to indicate whether compliance with this rule is mandatory or optional. The default value is false (optional).
  • A mandatory rule will prevent a virtual machine from being powered on or migrated to a host that does not satisfy the rule.
  • An optional rule specifies a preference. DRS takes an optional rule into consideration when it places a virtual machine in the cluster. DRS will act on an optional rule as long as it does not impact the ability of the host to satisfy current CPU or memory requirements for virtual machines on the system. (As long as the operation does not cause any host to be more than 100% utilized.)

    try:

        content = service_instance.RetrieveContent()

        print("starting host anti-affinity process, for vms:" + ' '.join(vm_list)+" rule name:"+affinity_rule_name)

        vm_objs = []

        for one_vm in vm_list:

            vm_obj = get_obj(content, [vim.VirtualMachine], one_vm)

            if vm_obj is not None:

                vm_objs.append(vm_obj)

        if len(vm_objs) >0:

            cluster = get_cluster_for_vm(content,vm_objs[0])

            print("Found Cluster:"+cluster.name)

            if cluster is not None:

               rule = vim.cluster.AntiAffinityRuleSpec(vm=vm_objs, enabled=True, mandatory=False,

                                                        name=affinity_rule_name)

                ruleSpec = vim.cluster.RuleSpec(info=rule, operation='add')

                configSpec = vim.cluster.ConfigSpecEx(rulesSpec=[ruleSpec])

                task = cluster.ReconfigureEx(configSpec, modify=True)

                wait_for_tasks(service_instance, [task])

            else:

                print("Cluster could not be identified, skipping  anti-affinity setting")

        else:

            print("VM Object list is empty, skipping anti-affinity setting")

    except Exception as ex:

        print("Error:"+ex.message)

0 Kudos
1 Solution

Accepted Solutions
daphnissov
Immortal
Immortal
Jump to solution

To my knowledge, a VM anti-affinity rule is always hard. There is no best effort basis for this type of rule. It's either enabled (active) or disabled (inactive).

View solution in original post

2 Replies
daphnissov
Immortal
Immortal
Jump to solution

To my knowledge, a VM anti-affinity rule is always hard. There is no best effort basis for this type of rule. It's either enabled (active) or disabled (inactive).

scott28tt
VMware Employee
VMware Employee
Jump to solution

I think you’re right for VM-VM rules: Create a VM-VM Affinity Rule


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos