KubeVirt - Part 5: Containerized Data Importer (CDI)

In short
  • Containerized Data Importer (CDI) creates disk images from an existing source.
  • Principle: DataVolume, PVC, PV.
  • Installation and injection of an ISO image.
  • Additional "root" DataVolume for the system disk.

Introduction

In the previous article, we deployed the LongHorn distributed storage solution.

The objective was to have high-performance and resilient storage for hosting the data of virtual machines running on KubeVirt.

However, this is not sufficient to cover all our needs. In fact, when maintaining operational status on VMs, regardless of the ecosystem, we often need to import disks or ISOs.

In the world of KubeVirt, we use Containerized Data Importer (CDI) for this purpose.

CDI is an essential and widely implemented open-source project.

As a reminder, in Kubernetes, data is stored in Persistent Volumes (PV). Usually, a PV is empty when it starts. CDI provides the ability to create a disk image from an existing source, such as a .qcow2, .iso, or .raw file.

CDI supports many inputs:

  • HTTP/S URL: Download an image from a web server.
  • S3 Compatible: Import from an AWS S3 or MinIO bucket.
  • Container Registry: Use a Docker image that contains a VM disk.
  • Direct Upload: Send a file from your local machine via an API.
  • Clone: Copy an existing PVC to a new one.

Operating principle

When deploying CDI on a Kubernetes cluster, we introduce a new custom resource (CRD - Custom Resource Definition) called DataVolume (DV).

This is an additional layer of abstraction where we define the source of the disk image that we want to use.

CDI uses this DataVolume to create a request for a persistent volume (PersistentVolumeClaim) (PVC). This, in turn, will trigger the creation of a persistent volume (PV) on which the image will be downloaded, decompressed (if necessary), and written.

Once complete, the DV is marked as "Ready" and available for use in a KubeVirt VM, which can then benefit from the attached PV.

Advantage

Importing an image becomes a declarative operation, using a simple YAML file as with any K8S object. CDI automates the operations and intelligently relies on existing disk configurations.

Objective

In our case, we will use CDI to inject the ISO images of our operating systems and to prepare the root disks of our VMs.

The first use case will be retrieving the OPNsense ISO.

Here is a diagram of what will be built.

CDI Diagram

Click the image to enlarge.

Installation

Operator deployment

CDI can be installed directly using the YAML files available in the project's GitHub repository. To do this, we first retrieve the latest release:

export VERSION=$(curl -s https://api.github.com/repos/kubevirt/containerized-data-importer/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')

Then, we apply the CRDs (Custom Resource Definitions) to create the associated operator.

kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator.yaml
kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-cr.yaml
CDI Deployment

Click the image to enlarge.

By default, all resources are deployed in a namespace cdi. I recommend leaving the default name and not trying to modify this configuration. This will simplify future update operations.

We can verify that everything is correct using the following command:

kubectl get pods -n cdi
Checking the CDI pods

Click the image to enlarge.

HTTPRoute deployment

As explained above, CDI "injects" data from the source you ask it to retrieve. For this to work, even within the cluster, it is essential to expose the CDI upload service so that it can receive the data.

To do this, we will use everything we deployed in part 3 of this cookbook, dedicated to KubeVirt, namely the GatewayAPI and Traefik. I will not go back over this topic, and I invite you to read the dedicated article if you want to know more.

We will first allow the CDI namespace to be visible by our Gateway by adding a label:

kubectl label namespace cdi network=lan

Then, we create the HTTPRoute object through the file 01-httproute-cdi-upload.yml, which contains the following content.

        
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-traefik-to-cdi
  namespace: cdi
spec:
  from:
  - group: gateway.networking.k8s.io
    kind: Gateway
    namespace: inf-traefik-lan
  to:
  - group: ""
    kind: Service
    name: cdi-uploadproxy
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: httproute-cdi-upload
  namespace: cdi
spec:
  parentRefs:
  - name: traefik-gateway-lan
    namespace: inf-traefik-lan
    sectionName: websecure
  hostnames:
  - cdi.coolcorp.priv
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: cdi-uploadproxy      

As mentioned before, we also add the ReferenceGrant object to the file to allow the Gateway to use the wildcard certificate that we previously generated (see the dedicated article). This will allow us to expose the CDI upload point on the URL cdi.coolcorp.priv.

The request will be redirected to the cdi-uploadproxy service deployed during the CDI installation.

Simply apply the file.

Creating the httproute

Click the image to enlarge.

ISO injection

Creation of the DataVolume

At this stage, everything is ready. We just need to retrieve our ISO from the OPNsense website. We can dedicate a folder on the server to store it:

sudo mkdir -p /mnt/local/isos
sudo chown -R bvivi57:bvivi57 /mnt/local/isos
Creating the folder for ISOs

Click the image to enlarge.

Since it is in bz2 format, you will need to ensure that you have the bzip2 package installed to access the ISO format directly.

sudo dnf install -y bzip2
bunzip2 OPNsense-24.7-dvd-amd64.iso.bz2
Decrypting the ISO

Click the image to enlarge.

Our ISO image is ready and available. We can now create our DataVolume (DV). Before we do, let's define a namespace called dev-opnsenselantoweb-inf to host our future OPNSense VM. Since our ISO image, and therefore our DV, will be used by this VM, it makes sense to create the DV within the associated namespace.

kubectl create namespace dev-opnsenselantoweb-inf

The DV is represented by the file 01-dv-opnsenselantoweb-iso.yml, and here's its content:


---
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: dv-opnsenselantoweb-iso
  namespace: dev-opnsenselantoweb-inf
  labels:
    environment: dev
    application: opnsenselantoweb
spec:
  source:
    upload: {}
    #http:
    #   url: "https://pkg.opnsense.org/releases/24.7/OPNsense-24.7-dvd-amd64.iso.bz2"
  pvc:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 4Gi
    storageClassName: sc-longhorn-sata

As you can see, I initially planned to integrate automatic ISO image retrieval directly from the OPNsense website using a source type of "upload". Unfortunately, this won't work due to the bz2 format, as KubeVirt expects an uncompressed ISO file. However, I have left the example in comments.

As we saw earlier, our DV will result in a PersistentVolumeClaim (PVC) and therefore a PersistentVolume (PV), because we need to store the contents of our ISO image in a persistent volume. We are therefore using our storage class sc-longhorn-sata, which was created in the previous article and is associated with the SATA SSD disks present in the server, used by Longhorn.

Pay attention to the size of the volume; it must be large enough to accommodate the ISO image. We apply the following file:

kubectl apply -f 01-dv-opnsenselantoweb-iso.yml

ISO upload

Since I couldn't natively retrieve the ISO image during DV creation, the DV is currently empty and unusable. Therefore, we need to manually inject our locally retrieved ISO image using our HTTPRoute, which was deployed earlier. To do this, we use the virtctl CLI, which we deployed along with KubeVirt.

virtctl image-upload dv dv-opnsenselantoweb-iso \
  --namespace dev-opnsenselantoweb-inf \
  --image-path=/mnt/local/isos/OPNsense-24.7-dvd-amd64.iso \
  --size=4Gi \
  --storage-class=sc-longhorn-sata \
  --access-mode=ReadWriteOnce \
  --insecure \
  --uploadproxy-url=https://cdi.coolcorp.priv
  --force-bind

As you can see, we provide a set of parameters to the image-upload argument, starting with the name and namespace of our DV. We specify where the source is located, our local ISO image, and how to upload it to the DV, either via our URL that is accessible through our HTTPRoute.

Starting the upload

Click the image to enlarge.

We can monitor the progress of the upload and ensure the operation's success using the following command:

kubectl get datavolume dv-opnsenselantoweb-iso -n dev-opnsenselantoweb-inf
Checking the ISO upload

Click the image to enlarge.

We need to finish with a progress indicator at 100%. You can explore the entire logic by tracing the lineage of the created objects.

If we list the PVCs in the namespace using the command:

kubectl get pvc -n dev-opnsenselantoweb-inf
Checking the PVCs

Click the image to enlarge.

We have our PVC dv-opnsenselantoweb-iso (with the same name as the DV) attached to a PV pvc-08511331-e6c2-4149-a18c-993cea4dc563 in the StorageClass sc-longhorn-sata. You can find this PV using:

kubectl get pv pvc-08511331-e6c2-4149-a18c-993cea4dc563
Checking the PVs

Click the image to enlarge.

Additional "root" DataVolume

Before finishing this part of the cookbook, there is one last thing to do that we haven't discussed yet, but which is visible in my diagram: the DV for the root disk of the VM.

Indeed, DVs are not reserved for ISOs only, but can be used for all disk images, even empty ones, that can be attached to a VM. We will therefore use a second YAML file 02-dv-opnsenselantoweb-root.yml, and here is its content.


---
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: dv-opnsenselantoweb-root
  namespace: dev-opnsenselantoweb-inf
  labels:
    environment: dev
    application: opnsenselantoweb
spec:
  source:
    blank: {}
  pvc:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 40Gi 

The syntax is the same as for the DV dedicated to the ISO, except that we leave the source empty and refer to the SC sc-longhorn-nvme to use the high-performance NVME disk that we configured with LongHorn. We apply the file:

kubectl apply -f 02-dv-opnsenselantoweb-root.yml

What are we going to do with it now? We will see this in the next article, where we will finally be able to create our first VM.