KubeVirt: Part 3 - Deploying VMs

In short
  • This article is outdated and has been replaced by the KubeVirt V2 series.
  • Network profiles (NetworkAttachmentDefinition) for LAN and DMZ.
  • Creating VMs, mounting ISO images, and using PV/PVCs.
  • An overview of the KubeVirt ecosystem at that time.

Introduction

Now it's time to deploy the VMs. The goal is to achieve the following result:

Target to deploy

Click the image to enlarge.

Of course, for those who haven't read the previous articles and are not familiar with KubeVirt, I strongly recommend starting by reading them before continuing this tutorial.

Deploying network profiles

We will start by declaring the network profiles, or NetworkAttachmentDefinitions (net-attach-def).

These new Kubernetes objects allow you to define which interfaces to use and with what configuration.

In our case, we will need three net-attach-defs:

  • nad-lan-default: used by the VM on the LAN network. The profile is declared in the file 01-nad-lan-default.yml
  •       ---
          apiVersion: k8s.cni.cncf.io/v1
          kind: NetworkAttachmentDefinition
          metadata:
            name: nad-lan-default
            annotations:
              k8s.v1.cni.cncf.io/resourceName: macvtap.network.kubevirt.io/enp8s0
            namespace: inf-kubevirt
          spec:
            config: '{
              "cniVersion": "0.3.1",
              "name": "enp8s0",
              "type": "macvtap",
              "mtu": 1500
            }'
          
  • nad-dmz-web: used by the VM on the WEB VLAN. The profile is declared in the file 02-nad-dmz-web.yml
  •       ---
          apiVersion: k8s.cni.cncf.io/v1
          kind: NetworkAttachmentDefinition
          metadata:
            name: nad-dmz-web
            annotations:
              k8s.v1.cni.cncf.io/resourceName: macvtap.network.kubevirt.io/enp8s0.6
            namespace: inf-kubevirt
          spec:
            config: '{
              "cniVersion": "0.3.1",
              "name": "enp8s0.6",
              "type": "macvtap", 
              "mtu": 1500
            }'
          
  • nad-dmz-first: used by the VM in the DMZ. The profile is declared in the file 03-nad-dmz-first.yml
  •   
          ---
          apiVersion: k8s.cni.cncf.io/v1
          kind: NetworkAttachmentDefinition
          metadata:
            name: nad-dmz-first
            namespace: inf-kubevirt
            annotations:
                k8s.v1.cni.cncf.io/resourceName: macvtap.network.kubevirt.io/enp13s0f3   
          spec:
            config: '{
              "cniVersion": "0.3.1",
              "name": "enp13s0f3",
              "type": "macvtap",
              "mode": "bridge"
            }'
          

The configuration of each profile is quite similar. Only the physical card to use is different and corresponds to what we described in the previous article for the macvtap CNI.

For organizational reasons, they will be hosted in a namespace inf-kubevirt. This is intended to be resources common to the KubeVirt configuration that are independent of the namespaces used to run the VMs.

We start by creating the namespace.

kubectl create ns inf-kubevirt
Namespace creation

Click the image to enlarge.

You can then apply the three profiles directly, which I have grouped in a folder called inf-kubevirt.

kubectl apply -f inf-kubevirt
Profile creation

Click the image to enlarge.

It is then possible to list the available network profiles in this "infrastructure" namespace that was created for this purpose.

kubectl get network-attachment-definitions -n inf-kubevirt
Listing of profiles

Click the image to enlarge.

Creating virtual machines

VM in the LAN

Next, we create the first VM vm-demo-lan. To adhere to my naming convention defined in my Kubernetes cookbook, I create a specific namespace.

kubectl create ns dev-demovm-lan
Namespace creation

Click the image to enlarge.

Since it is possible to establish access and resource limits per namespace, it becomes very easy to reserve a portion of your Kubernetes cluster for VMs intended for specific purposes. For example, all DEV or DMZ VMs. This is the advantage of KubeVirt; it incorporates most of the possibilities offered by the basic K8S objects.

The VM is described in a file 01-vm-demo-lan.yml, so here is its content:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: vm-demo-lan
  namespace: dev-demovm-lan
  labels:
    network: lan
    application: demo
    os: linux
    environnement: dev
spec:
  runStrategy: Always
  template:
    spec:
      domain:
        cpu:
          cores: 1
        resources:
          requests:
            memory: 1Gi
        devices:
          # On définit ici les disques utilisés par la VM
          disks:
            # Disque de l'image du système
            - name: containerdisk
              disk:
                bus: virtio
            # Disque cloud-init pour injecter la configuration initiale
            - name: cloudinitdisk
              disk:
                bus: virtio
          # Interfaces réseau existantes
          interfaces:
            # Interface connectée via Multus au VLAN (ici vlan LAN)
            - name: lan
              model: virtio
              binding:
               name: macvtap 
      networks:
        # Réseau 
        #L'interface LAN
        - name: lan
          multus:
            networkName: inf-kubevirt/nad-lan-default
      volumes:
        # Volume contenant l'image du système
        - name: containerdisk
          containerDisk:
            image: quay.io/containerdisks/centos:8.4
        # Volume cloud-init contenant la configuration initiale
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              #cloud-config
              hostname: vm-demo-lan
              users:
                - name: bvivi57
                  sudo: ALL=(ALL) NOPASSWD:ALL
                  shell: /bin/bash
              chpasswd:
                list: |
                  bvivi57:defaultpassword
                expire: False
              ssh_pwauth: True
    

As you can see, we have the syntax of a Kubernetes component. We are manipulating the VirtualMachine object from the API branch kubevirt.io/v1. This was added to the base API during the KubeVirt deployment.

In the spec section, we can specify the characteristics of our VM, starting with CPU and memory configuration.

Then, in the devices subsection, we add the desired devices, such as disks and network interfaces.

For this first example, we are using a containerdisk. This is a virtual disk based on a container and can be retrieved from a registry. It allows for rapid OS deployment. It is not intended to persist data but rather to quickly provide a VM based on an operating system, in this case centos:8.4, via the registry quay.io/containerdisks/ (we see the reference to the OS a little further down).

For networking, we declare a network interface. This interface will be attached via Multus to the profile inf-kubevirt/nad-lan-default. This is the first network profile that we detailed in the previous step. Since it is hosted in a different namespace than the VM, we need to specify the full path.

To initiate the VM configuration, we use a cloudinitdisk. This is a disk that is mounted first and contains the boot configuration. Cloud-init is a widely used configuration tool for customizing virtual machines during their initial startup.

The configuration to be used is declared in the volume attached to the containerdisk. We see the traditional Cloud-init syntax here. I haven't gone very far in this first example; I simply created a default user with a basic password and set the hostname.

To create and start the VM, you only need to apply the file.

kubectl apply -f 01-vm-demo-lan.yml

You can quickly list the VMs in the namespace.

kubectl get vm -n dev-demovm-lan
VM control

Click the image to enlarge.

Our virtual machine is in a running state and ready to be used.

Connecting to the VM

To connect to it, you need to access the VM's console. This can be accessed via the virtctl CLI (see previous article).

Simply run the following command:

virtctl console vm-demo-lan -n dev-demovm-lan
Access to the VM

Click the image to enlarge.

You will then be in the context of the VM. You can check some details, such as the system's IP address.

IP control

Click the image to enlarge.

We are correctly connected to the subnet corresponding to my LAN. The VM has been assigned an IP address via DHCP, so it is properly attached to my dedicated physical interface, as explained in the introduction to KubeVirt and implemented in the prerequisites.

It is also possible to open an SSH session with the user created during cloud init.

SSH access to the VM

Click the image to enlarge.

Performing actions on the VM

This is the first VM that has been made accessible. Since we will not be using it further, we can decide to shut it down.

To do this, you must first exit the VM by pressing Ctrl + 5, and then run the following command:

virtctl stop vm-demo-lan -n dev-demovm-lan

VM shutdown

Click the image to enlarge.

The VM is then stopped.

VM status

Click the image to enlarge.

If needed, you can restart it with the following command virtctl start vm-demo-lan -n dev-demovm-lan

VM in the WEB VLAN

We can now move on to the second VM, which is connected to the WEB VLAN.

Since this is a subnet in my DMZ, I will first create a new namespace.

kubectl create ns dev-demovm-dmz
Namespace creation

Click the image to enlarge.

Once again, this is simply an example of resource organization and usage of namespaces. You are free to choose the logic that suits you best.

As with the VM in LAN, this VM is described in a YAML file, 01-vm-demo-web.yaml, which contains the following:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: vm-demo-web
  namespace: dev-demovm-dmz
  labels:
    network: web
    application: demo
    os: linux
    environnement: dev
spec:
  runStrategy: Always
  template:
    spec:
      domain:
        cpu:
          cores: 1
        resources:
          requests:
            memory: 1Gi
        devices:
          # On définit ici les disques utilisés par la VM
          disks:
            # Disque de l'image du système
            - name: containerdisk
              disk:
                bus: virtio
            # Disque cloud-init pour injecter la configuration initiale
            - name: cloudinitdisk
              disk:
                bus: virtio
          # Interfaces réseau existantes
          interfaces:
            # Interface connectée via Multus au VLAN (ici vlan LAN)
            - name: lan
              model: virtio
              binding:
               name: macvtap 
      networks:
        # Réseau supplémentaire via Multus (NAD existant)
        - name: lan
          multus:
            networkName: inf-kubevirt/nad-dmz-web
      volumes:
        # Volume contenant l'image du système
        - name: containerdisk
          containerDisk:
            image: quay.io/containerdisks/centos:8.4
        # Volume cloud-init contenant la configuration initiale
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              #cloud-config
              hostname: vm-demo-lan
              users:
                - name: bvivi57
                  sudo: ALL=(ALL) NOPASSWD:ALL
                  shell: /bin/bash
              chpasswd: 
                list: |
                  bvivi57:defaultpassword
                expire: False
              ssh_pwauth: True

              # Configuration réseau cloud-init (statique sur eth0)
              network:
                version: 1
                config:
                  - type: physical
                    name: eth0
                    subnets:
                      - type: static
                        address: 192.168.5.188/24
                        gateway: 192.168.5.253
                        dns_nameservers:
                          - 192.168.10.30
                          - 8.8.8.8
    

It is very similar to the previous example, except that here we are using the network profile inf-kubevirt/nad-dmz-web.

We will also go a little further with cloud init by providing an IP address to the VM's interface this time, because I do not have DHCP on this subnet.

Simply deploy the YAML file to create the VM:

kubectl apply -f 01-vm-demo-web.yml
Deploying the VM

Click the image to enlarge.

If we list the VMs in the namespace, we will find our virtual machine.

kubectl get vm -n dev-demovm-dmz
VM control

Click the image to enlarge.

Connecting to the VM

Here too, you can access the VM's console via the following command: virtctl console vm-demo-web -n dev-demovm-dmz

For example, you can start upgrade operations.

Example command in the VM

Click the image to enlarge.

But as with the LAN VM, here we are using a temporary disk of type container disk.

VM in the FIRST DMZ

It is time to move on to the last VM, which is on the second subnet in DMZ (DMZ First).

For this example, we will go a little further and use a classic OS installation, namely Rocky Linux 9.5.

We will define a real disk that we can format as desired and mount the ISO image of the OS into the VM to install it at our convenience.

Storage management: using an ISO image

There's no need to create a new namespace, as I'm staying within a DMZ zone, and I don't have any specific distinction in my nomenclature between my DMZs.

However, we first need to prepare the ISO image. In the previous article, I describe how to install and configure Containerized-Data-Importer (CDI).

This is the time to use this addon. The goal is to upload the ISO image to a DataVolume (dv) object.

This will automatically provision a PVC (and therefore a PV) using the dedicated NFS storage class.

The upload will go through the ingress associated with the service provisioned by the CDI installation. Feel free to review the dedicated section on this topic in the previous article.

We'll use the virtctl CLI.

virtctl image-upload dv dv-iso-rocky-linux-9-5 --namespace dev-demovm-dmz --size=3Gi --storage-class=sc-local-storage-nfs-iso --access-mode=ReadWriteOnce --image-path=/mnt/nfs/nfstools/src-iso/pvc-iso-rocky-linux-9-5/Rocky-9.5-x86_64-minimal.iso --uploadproxy-url=https://cdi.coolcorp.priv –insecure
ISO upload

Click the image to enlarge.

The command takes the following main arguments:

  • the path to the ISO image: /mnt/nfs/nfstools/src-iso/pvc-iso-rocky-linux-9-5/Rocky-9.5-x86_64-minimal.iso
  • the name of the dv: dv-iso-rocky-linux-9-5 and the namespace where it should be declared: dev-demovm-dmz
  • The size to allocate (choose slightly more than the ISO image size): 3Gi
  • The URL of the CDI proxy: https://cdi.coolcorp.priv

Once again, feel free to take a look at the previous article dedicated to installing and configuring CDI.

Once the upload is complete, we can list the dv:

kubectl get dv -n dev-demovm-dmz
Listing of the virtual disk

Click the image to enlarge.

As well as the PVC that is associated with it:

kubectl get pvc -n dev-demovm-dmz
Listing of the persistent volume claim

Click the image to enlarge.

The ISO image is now contained in the PVC, which we can then attach to our VM later.

Storage management: creating the system disk

Next, we'll create the disk for the VM itself. We no longer want to use a containerdisk, but instead store the data for our VMs on the Kubernetes node.

As a reminder, I deployed KubeVirt on a single-node K8S configuration that I had previously used to demonstrate the use of a GPU with Kubernetes. In the associated article, I explain that I'm using three storage classes (sc) corresponding to three SSDs present within the server.

I don't need to use these scs, but I will rely on one of the high-performance SSDs that I have dedicated to storing my containers.

This is mounted at /mnt/datastores/prddtsnme001.

SSD disk mount point

Click the image to enlarge.

I'm going to create a folder specifically for the VM within this directory structure:

mkdir -p /mnt/datastores/prddtsnme001/localpv/dev-demovm-dmz/vm-demo-dmz

We will refer to this location via the PV described in the 01-pv-vm-demo-dmz.yml YAML file.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vm-demo-dmz
  labels:
    environment: dev
    network: dmz
    application: kubevirt
spec:
  capacity:
    storage: 30Gi 
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: sc-local-storage-prddtsnme001
  local:
    path: /mnt/datastores/prddtsnme001/localpv/dev-demovm-dmz/vm-demo-dmz
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - prdk8sctp001
              

Then, we associate this PV with a PVC through the 02-pvc-vm-demo-dmz.yml file.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-vm-demo-dmz
  namespace: dev-demovm-dmz
  labels:
    environment: dev
    network: dmz
    application: kubevirt
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: sc-local-storage-prddtsnme001
  resources:
    requests:
      storage: 30Gi
    

(If you are not familiar with the concepts of persistent volumes, persistent volume claims, and storage classes, feel free to take a look here and here)

Using persistent volumes and persistent volume claims allows us to prepare the environment for the virtual machine.

kubectl apply -f 01-pv-vm-demo-dmz.yml
PV creation

Click the image to enlarge.

kubectl apply -f 02-pvc-vm-demo-dmz.yml
PVC creation

Click the image to enlarge.

We verify that all storage is available:

kubectl get pvc -n dev-demovm-dmz

We have the PVC attached to the ISO and the PVC attached to the volume that we will use as the VM's disk.

Status of PVCs

Click the image to enlarge.

Deploying the VM

Now, we just need to define the virtual machine in the file 03-vm-demo-dmz.yml:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: vm-demo-dmz
  namespace: dev-demovm-dmz
  labels:
    network: dmz
    application: demo
    os: linux
    environnement: dev
spec:
  runStrategy: Always
  template:
    spec:
      domain:
        firmware:
          bootloader:
            efi:
              secureBoot: false
        cpu:
          cores: 1
        resources:
          requests:
            memory: 1Gi
        devices:
          # On définit ici les disques utilisés par la VM
          disks:
            # CDROM
            - name: pv-iso-rocky-linux-9-5
              cdrom:
                bus: sata
                readonly: true
              bootOrder: 2  
            # Disque du systeme
            - name: pv-vm-demo-dmz
              disk:
                bus: virtio
              bootOrder: 1       
          # Interfaces réseau existantes
          interfaces:
            # Interface connectée via Multus au VLAN (ici vlan LAN)
            - name: eth0
              model: virtio
              binding:
               name: macvtap 
      networks:
        # Réseau supplémentaire via Multus (NAD existant)
        - name: eth0
          multus:
            networkName: inf-kubevirt/nad-dmz-first
      volumes:
        # Volume local 
        - name: pv-iso-rocky-linux-9-5
          persistentVolumeClaim:
            claimName: dv-iso-rocky-linux-9-5
        - name: pv-vm-demo-dmz
          persistentVolumeClaim:
            claimName: pvc-vm-demo-dmz            
    

The syntax is very similar to the previous examples.

However, we will find the concept of a CD-ROM again, through the disk pv-iso-rocky-linux-9-5. We explicitly define that it is a specific device connected to a SATA bus.

The second disk pv-vm-demo-dmz corresponds to our persistent volume, which we created previously.

Note that we do not include a boot order with the bootOrder option.

The network profile used is the last one declared: inf-kubevirt/nad-dmz-first.

This time there is no more cloud-init configuration; instead, at the start of the spec section, we set the type of BIOS to emulate, in this case not a BIOS but an EFI, with secureBoot set to false.

To run the VM, simply apply the YAML file:

kubectl apply -f 03-vm-demo-dmz.yml
VM creation

Click the image to enlarge.

It now appears correctly in the list of started VMs within the namespace dev-demovm-dmz.

Listing of VMs

Click the image to enlarge.

Connect to the VM (VNC).

The challenge is now being able to access the OS installation assistant, because if the VM configuration is correct, and the system disk attached to the persistent volume pv-vm-demo-dmz is empty, the virtual machine should boot from the ISO.

The Rocky Linux installer runs through a GUI, so we cannot use console access as with previous VMs.

You need a graphical environment.

To do this, you can go directly to your workstation and use virtctl again, but this time with the vnc option.

It is through this protocol that Kubevirt allows access to the "graphical" view of VMs.

Therefore, in addition to virtctl (and the appropriate kubeconfig file), you need a VNC client.

On Windows, there are several available. Personally, I use TigerVNC; it is lightweight, simple, and regularly updated.

TigerVNC website

Click the image to enlarge.

To access the VM's GUI, you need to run the following command on a workstation with a graphical interface (in my case, my Windows 11).

virtctl vnc vm-demo-dmz -n dev-demovm-dmz --proxy-only
Launching the VNC proxy

Click the image to enlarge.

With this command, virtctl acts as a proxy listening on the local IP address of your machine (127.0.0.1) on a port chosen dynamically and visible in the command output.

Simply use the local IP address and this port in your VNC client to obtain the graphical view of the VM.

Configuring the VNC client

Click the image to enlarge.

From there, you can start the OS installation procedure as if you had connected a screen to the VM.

Installation GUI

Click the image to enlarge.

You can see the blank disk mapped to the virtual machine through the pv/pvc.

Disk control

Click the image to enlarge.

The network interface is mapped to the correct subnet. You can also define the hostname.

Network card control

Click the image to enlarge.

Simply follow the installation instructions and then restart.

Starting the installation

Click the image to enlarge.

Installation process

Click the image to enlarge.

The VM now has an OS deployed and will boot directly into it.

From there, you can switch back to a console connection if you have not deployed a GUI, or use the VNC protocol as before.

Installation control

Click the image to enlarge.

Conclusion

If we list all the VMs on the entire Kubernetes node:

kubectl get vm --all-namespaces
Presence of three VMs

Click the image to enlarge.

We see the three desired VMs (the first one is stopped).

So, we indeed have three virtual machines, each on a different network zone and using the physical interfaces of the node.

In addition to this, we still have our pods and containers, especially those used to demonstrate the use of GPUs with K8S and the deployment of Ollama.

Compared to the objectives described in the first article dedicated to KubeVirt, we have successfully met expectations: namely, to combine VMs and containers within a Kubernetes platform, all through the logic of K8S: using APIs and manipulating objects.

I tried to go a little further than the traditional demonstrations that you can find on the web about KubeVirt. I wanted to use different network interfaces to achieve something closer to what you might find in more classic virtual machine configurations.

I still have the idea of trying to reduce my reliance on VMware due to its acquisition and to centralize as many of my resources as possible around Kubernetes.

But it's not easy. KubeVirt is functional, and the KVM hypervisor on which it relies to run the VMs has nothing left to prove.

The ecosystem is still under development, and many of the add-ons necessary for the proper functioning of the entire system do not all have the same level of stability, especially around networking.

I am still far from having implemented everything necessary to truly take full advantage of KubeVirt's functions. There is a lot of work to be done, especially on the storage side, and also in switching to a multi-node setup with the ability to move VMs from one server to another, ideally without any loss of access, as you might do with vMotion on VMware. This still leaves me with a lot to discover.

As it stands, implementing all the components brick by brick remains tedious... but motivating. The alternative would be to use OpenShift, which seems to offer much greater ease of deployment with a packaged and stabilized integration.

But that means relying on a commercial (and expensive) offering from a third-party company. There is a free and community version of OpenShift called OKD. But I haven't had the chance to try it, and for now, I don't get the impression that it is being widely adopted.

No solution is perfect, but I have no concerns about the improvement of the KubeVirt ecosystem. The project remains interesting to follow and implement. I just need to find some time (and that's not easy) to continue all this and eventually end up with only Kub in the lab 😊 (and a little XCP-ng…)

(If needed, all the files used for this tutorial are freely accessible in my GitHub repository