Running VMs on Kubernetes with Kubevirt

  • In this post I will walk you through on how to deploy a VM in Kubernetes using kubevirt

What is kubevirt?

KubeVirt is a virtual machine management add-on for Kubernetes. The aim is to provide a common ground for virtualization solutions on top of Kubernetes
At its core, KubeVirt extends Kubernetes by adding additional virtualization resource types (especially the VM type) through Kubernetes's Custom Resource Definitions API. By using this mechanism, the Kubernetes API can be used to manage these VM resources alongside all other resources Kubernetes provides.

Pre-requisites

  • You require a Kubernetes platform deployed on a cloud environment, a bare-metal instance, or a local computer
  • Ensure that your hosts are capable of running virtualization workloads

What we will do

  • Download and install KubeVirt operator
  • Download and install CDI custom operator
  • Download the virtctl cli to manage the VM's
  • Deploy a VM using a iso image specified 
  • Download and install NoVnc to access the VM via web browser

Download and Install KubeVirt operator

Pick an upstream version of KubeVirt to install
Deploy the KubeVirt operator
    export RELEASE=v0.46.0
    kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-operator.yaml

Create the KubeVirt CR (instance deployment request) which triggers the actual installation

     kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-cr.yaml

Download and Install CDI custom operator

Install the CDI custom operator
    export VERSION=v0.40.0

Download the virtctl cli to manage the VM's

Get virtctl to manage VM's

    wget virtctl https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/virtctl-${RELEASE}-linux-amd64

   chmod +x virtctl

Deploy a VM using a iso image specified

  • Create 2 PV for the VMs which will serve as :
    • CDROM where iso will boot from 
    • VM HDD where iso will be installed 

apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv-nfs-vmdata
    namespace: cdi
    labels:
      type: nfs
      app: vm-system
spec:
    storageClassName: vm-machine
    capacity:
      storage: 2Gi
    accessModes:
      - ReadWriteMany
    nfs:
      server: 192.168.1.107
      path: "/nfs_shares/data/vm/fedora/netiso"

apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv-nfs-vmdata1
    namespace: cdi
    labels:
      type: nfs
      app: vm-system
spec:
    storageClassName: vm-machine1
    capacity:
      storage: 5Gi
    accessModes:
      - ReadWriteMany
    nfs:
      server: 192.168.1.107
     path: "/nfs_shares/data/vm/fedora"

  • Create PVC, pay attention to the annotation in the CDROM PVC claim. This is where you specify the iso image for kubevirt to use.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pvc-fedora-netinst
    namespace: cdi
    labels:
      app: containerized-data-importer
    annotations:
      cdi.kubevirt.io/storage.import.endpoint: "http://mirror.math.princeton.edu/pub/centos/8-stream/isos/x86_64/CentOS-Stream-8-x86_64-20211007-boot.iso"
spec:
    storageClassName: vm-machine
    accessModes:
    - ReadWriteMany
    resources:
      requests:
        storage: 2Gi

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pvc-fedora-hd
    namespace: cdi
    labels:
      type: nfs
      app: vm-system
spec:
    storageClassName: vm-machine1 
    accessModes:
    - ReadWriteMany
    resources:
      requests:
        storage: 5Gi
 
  • Create VM deployment.
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: fedora-vm
  namespace: cdi
spec:
  running: false
  template:
    metadata:
      labels:
        kubevirt.io/domain: fedora-vm
        app: vm-system
    spec:
      domain:
        cpu:
          cores: 2
        devices:
          disks:
          - bootOrder: 1
            cdrom:
              bus: sata
            name: cdromiso
          - disk:
              bus: virtio
            name: harddrive
        machine:
          type: q35
        resources:
          requests:
            memory: 1G
      volumes:
      - name: cdromiso
        persistentVolumeClaim:
          claimName: pvc-fedora-netinst
      - name: harddrive
        persistentVolumeClaim:
          claimName: pvc-fedora-hd

To start the VM

The Deployed VM will be in a stopped state. To start the VM execute the below command
   [root@K8s-master Downloads]# virtctl start fedora-vm -n cdi
   VM fedora-vm was scheduled to start

To access the VM via Web Console

Deploy NoVnc

Since NoVnc runs as nodeport, get the port # exposed by kubernetes
kubectl get svc -n kubevirt virtvnc

Open a web browser and replace <ip>:<port> from previous command and replace in (next line)

If all goes well you should see the below in your web browser


Clicking on VNC will bring up the install/setup screen for the VM