How to use affinity groups to create resilient VM solutions

Introduction

Creating an affinity group

Adding a VM to an affinity group

Managing your affinity groups

Managing affinity groups with the VDC API

 

Introduction

In VDC you can create associations between virtual machines, known as affinity groups. These influence how your VMs get distributed to the underlying hardware within one VDC zone. The type currently available is an anti-affinity group, which means that VMs that are members of the same group will be set to run on different physical hardware in the zone.

This gives you automatic fault tolerance in the case of a failure of underlying VDC compute hardware. For example, if you have two or more VMs running a cluster of web servers and you put all of the VMs into the same anti-affinity group, then you will get automatic resilience for the cluster by deploying the VM’s over different physical blades.

You can carry out certain operations with affinity groups using the VDC Control Centre, and there are additional controls available if you use the VDC API.

Creating an affinity group

Open the Affinity Groups panel by clicking Affinity Groups on the left-hand menu.

Click the Add new affinity group button to create a new affinity group:

A Name is required, and Description text is optional. Only one Type is currently available ('host anti-affinity').

Adding a VM to an affinity group

You can add a VM to one or more of the existing affinity groups during the creation (deployment) of the VM. See How to create a virtual machine.

You will need to create at least one affinity group before starting the Add Virtual Machine procedure.

If you want to change the groups that a VM belongs to, you can do this using the API (see below).

Managing your affinity groups

To see the members of any group, first click the name of the group, then click Connected VMs.

You can delete an affinity group using the Delete ('X') button in the group's Details view. This will not affect the affinity status of any member VMs that are running, but when you (re)start a VM the anti-affinity group allocation rule will no longer apply.

Managing affinity groups with the VDC API

There are API commands equivalent to the operations above: createAffinityGroup, deleteAffinityGroup and listAffinityGroups. The deployVirtualMachine command has an optional parameter to specify affinity group membership for the new VM.

The command updateVMAffinityGroup allows you to change the memberships for an existing VM (however you can't do it directly the other way around, that is to modify the memberships of one affinity group). The VM must be stopped to apply membership changes with this API command.

The inputs to this command need to specify the UUID for a virtual machine and all of the affinity groups which the VM should belong to. The affinity groups can be specified either by UUID or by name.

For example, if a VDC account has some existing affinity groups:

(local) > list affinitygroups filter=id,name,virtualmachineIds
+--------------------------------------+--------------------+-------------------------------------------+
|                  id                  |        name        |             virtualmachineIds             |
+--------------------------------------+--------------------+-------------------------------------------+
| 484016da-206e-46aa-8317-04e5cd9121e1 |   database-group   | [u'71d38f14-1774-4b10-a5b3-773dec251482'] |
| 499f2d8d-a022-40b7-a735-331f161669d3 | webserver-group-02 |                                           |
| 45d159ee-4eaf-4e01-a06f-ab7def385e41 | webserver-group-01 | [u'71d38f14-1774-4b10-a5b3-773dec251482'] |
+--------------------------------------+--------------------+-------------------------------------------+  

This command changes affinity group membership for a VM using affinity group UUIDs:

(local) > update vmaffinitygroup id=UUID affinitygroupids=484016da-206e-46aa-8317-04e5cd9121e1,45d159ee-4eaf-4e01-a06f-ab7def385e41
cmd = org.apache.cloudstack.api.command.user.affinitygroup.UpdateVMAffinityGroupCmd
created = 2017-08-14T12:45:10+0000
jobid = 4f5bb82a-1bc9-4f6f-9921-20ef94db4b39
...
affinitygroup:
+----------------+--------------------------------------+--------------------+
|    account     |                  id                  |        name        |
+----------------+--------------------------------------+--------------------+
| Interoute Demo | 484016da-206e-46aa-8317-04e5cd9121e1 |   database-group   |
| Interoute Demo | 45d159ee-4eaf-4e01-a06f-ab7def385e41 | webserver-group-01 |
+----------------+--------------------------------------+--------------------+
...

Or equivalently, using the affinity group names:

(local) > update vmaffinitygroup id=UUID affinitygroupnames=database-group,webserver-group-01

To remove a VM from all affinity groups, use one of the parameters with a blank value:

(local) > update vmaffinitygroup id=UUID affinitygroupids=
...
affinitygroup:
...

The changed affinity group memberships will apply when the VM is restarted.