Labs - Use KubeVirt

Use KubeVirt

  • You can experiment with this lab online at Killercoda

Create a Virtual Machine

Download the VM manifest and explore it. Note it uses a container disk and as such doesn’t persist data. Such container disks currently exist for alpine, cirros and fedora.

wget https://kubevirt.io/labs/manifests/vm.yaml
less vm.yaml

Apply the manifest to Kubernetes.

kubectl apply -f https://kubevirt.io/labs/manifests/vm.yaml

You should see following results

virtualmachine.kubevirt.io “testvm” created virtualmachineinstancepreset.kubevirt.io “small” created

Manage Virtual Machines (optional):

To get a list of existing Virtual Machines. Note the running status.

kubectl get vms
kubectl get vms -o yaml testvm

To start a Virtual Machine you can use:

virtctl start testvm

If you installed virtctl via krew, you can use kubectl virt:

# Start the virtual machine:
kubectl virt start testvm

# Stop the virtual machine:
kubectl virt stop testvm

Alternatively you could use kubectl patch:

# Start the virtual machine:
kubectl patch virtualmachine testvm --type merge -p \
    '{"spec":{"running":true}}'

# Stop the virtual machine:
kubectl patch virtualmachine testvm --type merge -p \
    '{"spec":{"running":false}}'

Now that the Virtual Machine has been started, check the status (kubectl get vms). Note the Running status.

You now want to see the instance of the vm you just started :

kubectl get vmis
kubectl get vmis -o yaml testvm

Note the difference between VM (virtual machine) resource and VMI (virtual machine instance) resource. The VMI does not exist before starting the VM and the VMI will be deleted when you stop the VM. (Also note that restart of the VM is needed if you like to change some properties. Just modifying VM is not sufficient, the VMI has to be replaced.)

Accessing VMs (serial console)

Connect to the serial console of the Cirros VM. Hit return / enter a few times and login with the displayed username and password.

virtctl console testvm

Disconnect from the virtual machine console by typing: ctrl+].

If you like to see the complete boot sequence logs from the console. You need to connect to the serial console just after starting the VM (you can test this by stopping and starting the VM again, see below).

Controlling the State of the VM

To shut it down:

virtctl stop testvm

To delete a Virtual Machine:

kubectl delete vm testvm

This concludes this section of the lab.

You can watch how the laboratory is done in the following video:

Next Lab