Specifying nodes for virtual machines

You can place virtual machines (VMs) on specific nodes by using node placement rules.

About node placement for virtual machines

To ensure that virtual machines (VMs) run on appropriate nodes, you can configure node placement rules. You might want to do this if:

  • You have several VMs. To ensure fault tolerance, you want them to run on different nodes.

  • You have two chatty VMs. To avoid redundant inter-node routing, you want the VMs to run on the same node.

  • Your VMs require specific hardware features that are not present on all available nodes.

  • You have a pod that adds capabilities to a node, and you want to place a VM on that node so that it can use those capabilities.

Virtual machine placement relies on any existing node placement rules for workloads. If workloads are excluded from specific nodes on the component level, virtual machines cannot be placed on those nodes.

You can use the following rule types in the spec field of a VirtualMachine manifest:

nodeSelector

Allows virtual machines to be scheduled on nodes that are labeled with the key-value pair or pairs that you specify in this field. The node must have labels that exactly match all listed pairs.

affinity

Enables you to use more expressive syntax to set rules that match nodes with virtual machines. For example, you can specify that a rule is a preference, rather than a hard requirement, so that virtual machines are still scheduled if the rule is not satisfied. Pod affinity, pod anti-affinity, and node affinity are supported for virtual machine placement. Pod affinity works for virtual machines because the VirtualMachine workload type is based on the Pod object.

Affinity rules only apply during scheduling. OKD does not reschedule running workloads if the constraints are no longer met.

tolerations

Allows virtual machines to be scheduled on nodes that have matching taints. If a taint is applied to a node, that node only accepts virtual machines that tolerate the taint.

Node placement examples

The following example YAML file snippets use nodePlacement, affinity, and tolerations fields to customize node placement for virtual machines.

Example: VM node placement with nodeSelector

In this example, the virtual machine requires a node that has metadata containing both example-key-1 = example-value-1 and example-key-2 = example-value-2 labels.

If there are no nodes that fit this description, the virtual machine is not scheduled.

Example VM manifest

  1. metadata:
  2. name: example-vm-node-selector
  3. apiVersion: kubevirt.io/v1
  4. kind: VirtualMachine
  5. spec:
  6. template:
  7. spec:
  8. nodeSelector:
  9. example-key-1: example-value-1
  10. example-key-2: example-value-2
  11. ...

Example: VM node placement with pod affinity and pod anti-affinity

In this example, the VM must be scheduled on a node that has a running pod with the label example-key-1 = example-value-1. If there is no such pod running on any node, the VM is not scheduled.

If possible, the VM is not scheduled on a node that has any pod with the label example-key-2 = example-value-2. However, if all candidate nodes have a pod with this label, the scheduler ignores this constraint.

Example VM manifest

  1. metadata:
  2. name: example-vm-pod-affinity
  3. apiVersion: kubevirt.io/v1
  4. kind: VirtualMachine
  5. spec:
  6. affinity:
  7. podAffinity:
  8. requiredDuringSchedulingIgnoredDuringExecution: (1)
  9. - labelSelector:
  10. matchExpressions:
  11. - key: example-key-1
  12. operator: In
  13. values:
  14. - example-value-1
  15. topologyKey: kubernetes.io/hostname
  16. podAntiAffinity:
  17. preferredDuringSchedulingIgnoredDuringExecution: (2)
  18. - weight: 100
  19. podAffinityTerm:
  20. labelSelector:
  21. matchExpressions:
  22. - key: example-key-2
  23. operator: In
  24. values:
  25. - example-value-2
  26. topologyKey: kubernetes.io/hostname
  27. ...
1If you use the requiredDuringSchedulingIgnoredDuringExecution rule type, the VM is not scheduled if the constraint is not met.
2If you use the preferredDuringSchedulingIgnoredDuringExecution rule type, the VM is still scheduled if the constraint is not met, as long as all required constraints are met.

Example: VM node placement with node affinity

In this example, the VM must be scheduled on a node that has the label example.io/example-key = example-value-1 or the label example.io/example-key = example-value-2. The constraint is met if only one of the labels is present on the node. If neither label is present, the VM is not scheduled.

If possible, the scheduler avoids nodes that have the label example-node-label-key = example-node-label-value. However, if all candidate nodes have this label, the scheduler ignores this constraint.

Example VM manifest

  1. metadata:
  2. name: example-vm-node-affinity
  3. apiVersion: kubevirt.io/v1
  4. kind: VirtualMachine
  5. spec:
  6. affinity:
  7. nodeAffinity:
  8. requiredDuringSchedulingIgnoredDuringExecution: (1)
  9. nodeSelectorTerms:
  10. - matchExpressions:
  11. - key: example.io/example-key
  12. operator: In
  13. values:
  14. - example-value-1
  15. - example-value-2
  16. preferredDuringSchedulingIgnoredDuringExecution: (2)
  17. - weight: 1
  18. preference:
  19. matchExpressions:
  20. - key: example-node-label-key
  21. operator: In
  22. values:
  23. - example-node-label-value
  24. ...
1If you use the requiredDuringSchedulingIgnoredDuringExecution rule type, the VM is not scheduled if the constraint is not met.
2If you use the preferredDuringSchedulingIgnoredDuringExecution rule type, the VM is still scheduled if the constraint is not met, as long as all required constraints are met.

Example: VM node placement with tolerations

In this example, nodes that are reserved for virtual machines are already labeled with the key=virtualization:NoSchedule taint. Because this virtual machine has matching tolerations, it can schedule onto the tainted nodes.

A virtual machine that tolerates a taint is not required to schedule onto a node with that taint.

Example VM manifest

  1. metadata:
  2. name: example-vm-tolerations
  3. apiVersion: kubevirt.io/v1
  4. kind: VirtualMachine
  5. spec:
  6. tolerations:
  7. - key: "key"
  8. operator: "Equal"
  9. value: "virtualization"
  10. effect: "NoSchedule"
  11. ...

Additional resources