Configuring mediated devices
OKD Virtualization automatically creates mediated devices, such as virtual GPUs (vGPUs), if you provide a list of devices in the HyperConverged
custom resource (CR).
Prerequisites
If your hardware vendor provides drivers, you installed them on the nodes where you want to create mediated devices.
- If you use NVIDIA cards, you installed the NVIDIA GRID driver.
About using virtual GPUs with OKD Virtualization
Some graphics processing unit (GPU) cards support the creation of virtual GPUs (vGPUs). OKD Virtualization can automatically create vGPUs and other mediated devices if an administrator provides configuration details in the HyperConverged
custom resource (CR). This automation is especially useful for large clusters.
Refer to your hardware vendor’s documentation for functionality and support details. |
Mediated device
A physical device that is divided into one or more virtual devices. A vGPU is a type of mediated device (mdev); the performance of the physical GPU is divided among the virtual devices. You can assign mediated devices to one or more virtual machines (VMs), but the number of guests must be compatible with your GPU. Some GPUs do not support multiple guests.
Configuration overview
When configuring mediated devices, an administrator must:
Create the mediated devices.
Expose the mediated devices to the cluster.
The HyperConverged
CR includes APIs that accomplish both tasks:
Creating mediated devices
...
spec:
mediatedDevicesConfiguration:
mediatedDevicesTypes: (1)
- <device_type>
nodeMediatedDeviceTypes: (2)
- mediatedDevicesTypes: (3)
- <device_type>
nodeSelector: (4)
<node_selector_key>: <node_selector_value>
...
1 | Required: Configures global settings for the cluster. |
2 | Optional: Overrides the global configuration for a specific node or group of nodes. Must be used with the global mediatedDevicesTypes configuration. |
3 | Required if you use nodeMediatedDeviceTypes . Overrides the global mediatedDevicesTypes configuration for select nodes. |
4 | Required if you use nodeMediatedDeviceTypes . Must include a key:value pair. |
Exposing mediated devices to the cluster
...
permittedHostDevices:
mediatedDevices:
- mdevNameSelector: GRID T4-2Q (1)
resourceName: nvidia.com/GRID_T4-2Q
...
1 | Exposes the mediated devices that map to this value on the host.
|
How vGPUs are assigned to nodes
For each physical device, OKD Virtualization configures:
A single mdev type.
The maximum number of instances of the selected mdev type.
The cluster architecture affects how devices are created and assigned to nodes.
Large cluster with multiple cards per node
On nodes with multiple cards that can support similar vGPU types, the relevant device types are created in a round-robin manner. For example:
...
mediatedDevicesConfiguration:
mediatedDevicesTypes:
- nvidia-222
- nvidia-228
- nvidia-105
- nvidia-108
...
In this scenario, each node has two cards, both of which support the following vGPU types:
nvidia-105
...
nvidia-108
nvidia-217
nvidia-299
...
On each node, OKD Virtualization creates:
16 vGPUs of type nvidia-105 on the first card.
2 vGPUs of type nvidia-108 on the second card.
One node has a single card that supports more than one requested vGPU type
OKD Virtualization uses the supported type that comes first on the mediatedDevicesTypes
list.
For example, a node’s card supports nvidia-223
and nvidia-224
. The following mediatedDevicesTypes
list is configured:
...
mediatedDevicesConfiguration:
mediatedDevicesTypes:
- nvidia-22
- nvidia-223
- nvidia-224
...
In this example, OKD Virtualization uses the nvidia-223
type.
About changing and removing mediated devices
OKD Virtualization updates the cluster’s mediated device configuration if:
You edit the
HyperConverged
CR and change the contents of themediatedDevicesTypes
stanza.You change the node labels that match the
nodeMediatedDeviceTypes
node selector.You remove the device information from the
spec.mediatedDevicesConfiguration
andspec.permittedHostDevices
stanzas of theHyperConverged
CR.If you remove the device information from the
spec.permittedHostDevices
stanza without also removing it from thespec.mediatedDevicesConfiguration
stanza, you cannot create a new mediated device type on the same node. To properly remove mediated devices, remove the device information from both stanzas.
Depending on the specific changes, these actions cause OKD Virtualization to reconfigure mediated devices or remove them from the cluster nodes.
Preparing hosts for mediated devices
You must enable the IOMMU (Input-Output Memory Management Unit) driver before you can configure mediated devices.
Adding kernel arguments to enable the IOMMU driver
To enable the IOMMU (Input-Output Memory Management Unit) driver in the kernel, create the MachineConfig
object and add the kernel arguments.
Prerequisites
Administrative privilege to a working OKD cluster.
Intel or AMD CPU hardware.
Intel Virtualization Technology for Directed I/O extensions or AMD IOMMU in the BIOS (Basic Input/Output System) is enabled.
Procedure
Create a
MachineConfig
object that identifies the kernel argument. The following example shows a kernel argument for an Intel CPU.apiVersion: machineconfiguration.openshift.io/v1
kind: MachineConfig
metadata:
labels:
machineconfiguration.openshift.io/role: worker (1)
name: 100-worker-iommu (2)
spec:
config:
ignition:
version: 3.2.0
kernelArguments:
- intel_iommu=on (3)
...
1 Applies the new kernel argument only to worker nodes. 2 The name
indicates the ranking of this kernel argument (100) among the machine configs and its purpose. If you have an AMD CPU, specify the kernel argument asamd_iommu=on
.3 Identifies the kernel argument as intel_iommu
for an Intel CPU.Create the new
MachineConfig
object:$ oc create -f 100-worker-kernel-arg-iommu.yaml
Verification
Verify that the new
MachineConfig
object was added.$ oc get MachineConfig
Adding and removing mediated devices
Creating and exposing mediated devices
You can expose and create mediated devices such as virtual GPUs (vGPUs) by editing the HyperConverged
custom resource (CR).
Prerequisites
- You enabled the IOMMU (Input-Output Memory Management Unit) driver.
Procedure
Edit the
HyperConverged
CR in your default editor by running the following command:$ oc edit hyperconverged kubevirt-hyperconverged -n openshift-cnv
Add the mediated device information to the
HyperConverged
CRspec
, ensuring that you include themediatedDevicesConfiguration
andpermittedHostDevices
stanzas. For example:Example configuration file
apiVersion: hco.kubevirt.io/v1
kind: HyperConverged
metadata:
name: kubevirt-hyperconverged
namespace: openshift-cnv
spec:
mediatedDevicesConfiguration: (1)
mediatedDevicesTypes: (2)
- nvidia-231
nodeMediatedDeviceTypes: (3)
- mediatedDevicesTypes: (4)
- nvidia-233
nodeSelector:
kubernetes.io/hostname: node-11.redhat.com
permittedHostDevices: (5)
mediatedDevices:
- mdevNameSelector: GRID T4-2Q
resourceName: nvidia.com/GRID_T4-2Q
- mdevNameSelector: GRID T4-8Q
resourceName: nvidia.com/GRID_T4-8Q
...
1 Creates mediated devices. 2 Required: Global mediatedDevicesTypes
configuration.3 Optional: Overrides the global configuration for specific nodes. 4 Required if you use nodeMediatedDeviceTypes
.5 Exposes mediated devices to the cluster. Save your changes and exit the editor.
Verification
You can verify that a device was added to a specific node by running the following command:
$ oc describe node <node_name>
Removing mediated devices from the cluster using the CLI
To remove a mediated device from the cluster, delete the information for that device from the HyperConverged
custom resource (CR).
Procedure
Edit the
HyperConverged
CR in your default editor by running the following command:$ oc edit hyperconverged kubevirt-hyperconverged -n openshift-cnv
Remove the device information from the
spec.mediatedDevicesConfiguration
andspec.permittedHostDevices
stanzas of theHyperConverged
CR. Removing both entries ensures that you can later create a new mediated device type on the same node. For example:Example configuration file
apiVersion: hco.kubevirt.io/v1
kind: HyperConverged
metadata:
name: kubevirt-hyperconverged
namespace: openshift-cnv
spec:
mediatedDevicesConfiguration:
mediatedDevicesTypes: (1)
- nvidia-231
permittedHostDevices:
mediatedDevices: (2)
- mdevNameSelector: GRID T4-2Q
resourceName: nvidia.com/GRID_T4-2Q
1 To remove the nvidia-231
device type, delete it from themediatedDevicesTypes
array.2 To remove the GRID T4-2Q
device, delete themdevNameSelector
field and its correspondingresourceName
field.Save your changes and exit the editor.
Assigning a mediated device to a virtual machine
Assign mediated devices such as virtual GPUs (vGPUs) to virtual machines.
Prerequisites
- The mediated device is configured in the
HyperConverged
custom resource.
Procedure
Assign the mediated device to a virtual machine (VM) by editing the
spec.domain.devices.gpus
stanza of theVirtualMachine
manifest:Example virtual machine manifest
apiVersion: kubevirt.io/v1
kind: VirtualMachine
spec:
domain:
devices:
gpus:
- deviceName: nvidia.com/TU104GL_Tesla_T4 (1)
name: gpu1 (2)
- deviceName: nvidia.com/GRID_T4-1Q
name: gpu2
1 The resource name associated with the mediated device. 2 A name to identify the device on the VM.
Verification
To verify that the device is available from the virtual machine, run the following command, substituting
<device_name>
with thedeviceName
value from theVirtualMachine
manifest:$ lspci -nnk | grep <device_name>