Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Openstack Affinity Groups, make sure instances are on the same or different compute hypervisor hosts

Published: 29-11-2014 | Author: Remy van Elst | Text only version of this article


Table of Contents


This guide shows you how to use Openstack Affinity groups. Affinity or Anti-Affinity groups allow you to make sure instances (VM/VPS) are on the samehypervisor host or on a different one. There are cases when you want twoinstances on different compute nodes, for example, when they are clusteredservers like a load balancer or a database master-master setup. All VM's in eachAffinity group are hosted in the same hypervisor, while no two VM's of a sameAnti-Affinity group are hosted in the same hypervisor.

Affinity is supported since Openstack IceHouse. You can only add an instance toan affinity group when you create the instance with nova boot. You can not addexisting instances. You can however create an image of an instance and boot anew server from that image, in the new affinity group.

You can see all my Openstack related articles here. For example, how tobuild a High Available cluster with Ansible and Openstack.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

Create an Affinity Group

Make sure you have the Openstack Command Line Tools installed. Also make sureyou have a computerc file. See the first part of this page to find outhow to do that.

The command syntax to create an affinity group with the novaclient:

nova server-group-create $group-name $policy

If you want all servers to run on the same hypervisor host, create a group withan affinity policy.

If you want all servers to run on different hypervisor host, create a group withan anti-affinity policy. This means that if you have 5 servers in this group,they will be on 5 different hypervisor (compute) nodes, never will a VM be on ahost node where another VM from this affinity policy is.

You can see all available affinity groups with server-group-list:

$ nova server-group-list+--------------------------------------+------+--------------------+---------+----------+| Id                                   | Name | Policies           | Members | Metadata |+--------------------------------------+------+--------------------+---------+----------+| 415eea22-4e37-44e0-a4c9-59c395688fc8 | Anti | [u'anti-affinity'] | []      | {}       |+--------------------------------------+------+--------------------+---------+----------+

You can delete a server group with server-group-delete. Deleting a servergroup does not delete all the vm's in that group. The policy is removed from thevm's, so they can be on the same node, or on different ones. Exclusivity is notguaranteed anymore.

Add instance to affinity group

You can only add a server to an affinity group when you create the instance. Notafterwards. To add an instance to an affinity group, use the following extraoption when executing nova boot:

--hint group=$affinity-group-uuid

Example commands:

nova boot --image "CloudVPS Ubuntu 14.04" --hint group=415eea22-4e37-44e0-a4c9-59c395688fc8 --key-name $ssh_key --flavor "Standard 1" "Anti-Affinity 1-1"nova boot --image "CloudVPS Ubuntu 14.04" --hint group=415eea22-4e37-44e0-a4c9-59c395688fc8 --key-name $ssh_key --flavor "Standard 1" "Anti-Affinity 1-2"nova boot --image "CloudVPS Ubuntu 14.04" --hint group=415eea22-4e37-44e0-a4c9-59c395688fc8 --key-name $ssh_key --flavor "Standard 1" "Anti-Affinity 1-3"nova boot --image "CloudVPS Ubuntu 14.04" --hint group=415eea22-4e37-44e0-a4c9-59c395688fc8 --key-name $ssh_key --flavor "Standard 1" "Anti-Affinity 1-4"

These 4 servers will be on different hypervisor hosts. You can check this withthe nova show command:

nova show $instance_uuid

Look for the hostId value:

| hostId     | 4b499ffc320810e06891d15fca10a4ac76a50eae788d2f28a990fbbf     |

This is the hostId of the hosting compute hypervisor node. With an Anti-Affinitypolicy, these are different. With an Affinity Policy, these are the same.

No valid host was found

If you reveive the No valid host was found error during the nova bootcommand, that means that there are no hosts that have the capacity to host thatVM and guarantee there will not be another VM from that anti-affinity group onit (in the case of an anti affinity group). It can also mean that the hypervisorhas not enoigh free capacity (in the case of an affinity group).

This might mean that your Openstack provider has put restrictions on the filtersor is low on free capacity. Affinity policies are limited by the availableresources of the current hypervisor that hosts the group, while Anti-Affinitypolicies are limited by the number of available hypervisors in the datacenter(after filtering).

If you want your load balancers, app servers and database servers on differentcompute nodes from each other (with the same role) then you can create 3 anti-affinity groups, one for the load balancers, one for the app servers and one forthe database servers. Your app server might be on a host node with a loadbalancer, but not with another app server.

How to enable Affinity/Anti-Affinity (Admin Only)

Enabling Affinity and Anti-Afiinity is simply done by addingServerGroupAffinityFilter and ServerGroupAntiAffinityFilter intoscheduler_default_filters. By default these filters are already enabled:

# nova.confscheduler_default_filters = ServerGroupAffinityFilter,ServerGroupAntiAffinityFilter

To read more about the Openstack filtering options, see the officialdocumentation: .

Tags: affinity, anti-affinity, articles, cloud, cluster, compute, nova, openstack