- 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
kubectl create -f
https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-operator.yaml
Deploy the CDI resource itself
kubectl create -f https://github.com/kubevirt/containerized-data-importer/releases/download/$VERSION/cdi-cr.yaml
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: v1kind: PersistentVolumemetadata:name: pv-nfs-vmdatanamespace: cdilabels:type: nfsapp: vm-systemspec:storageClassName: vm-machinecapacity:storage: 2GiaccessModes:- ReadWriteManynfs:server: 192.168.1.107path: "/nfs_shares/data/vm/fedora/netiso"apiVersion: v1kind: PersistentVolumemetadata:name: pv-nfs-vmdata1namespace: cdilabels:type: nfsapp: vm-systemspec:storageClassName: vm-machine1capacity:storage: 5GiaccessModes:- ReadWriteManynfs: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: v1kind: PersistentVolumeClaimmetadata:name: pvc-fedora-netinstnamespace: cdilabels:app: containerized-data-importerannotations: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-machineaccessModes:- ReadWriteManyresources:requests:storage: 2GiapiVersion: v1kind: PersistentVolumeClaimmetadata:name: pvc-fedora-hdnamespace: cdilabels:type: nfsapp: vm-systemspec:storageClassName: vm-machine1accessModes:- ReadWriteManyresources:requests:storage: 5Gi
- Create VM deployment.
apiVersion: kubevirt.io/v1
kind: VirtualMachinemetadata:name: fedora-vmnamespace: cdispec:running: falsetemplate:metadata:labels:kubevirt.io/domain: fedora-vmapp: vm-systemspec:domain:cpu:cores: 2devices:disks:- bootOrder: 1cdrom:bus: sataname: cdromiso- disk:bus: virtioname: harddrivemachine:type: q35resources:requests:memory: 1Gvolumes:- name: cdromisopersistentVolumeClaim:claimName: pvc-fedora-netinst- name: harddrivepersistentVolumeClaim: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
kubectl apply -f
https://github.com/wavezhang/virtVNC/raw/master/k8s/virtvnc.yaml
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