Guided Exercise: Create and access a virtual machine in KubeVirt

Create a virtual machine

Install the VM manifest from the previous section:

kubectl apply -f 

Check on the virtual machine you just created:

kubectl get vm

testvm   17s   Stopped   False

As mentioned in the previous section, the VM definition here is for a stopped VM. Use virtctl to change the expected state of the VM to running:

Start a virtual machine

virtctl start testvm

VM testvm was scheduled to start

Recall that the VirtualMachine manages a VirtualMachineInstance much in the same way that a Deployment manages a set of Pods. Likewise, a VirtualMachineInstance creates a Pod. We can see all those entities with one kubectl get command:

kubectl get vm,vmi,pod

NAME                                AGE   STATUS    READY   26s   Running   True

NAME                                        AGE   PHASE     IP           NODENAME   READY   7s    Running   ubuntu     True

NAME                             READY   STATUS    RESTARTS   AGE
pod/virt-launcher-testvm-smlqz   2/2     Running   0          7s

Connect to a virtual machine

With the VM running,  you can connect to the serial console using virtctl:

virtctl console testvm

Note that breaking out of the serial console environment requires a ctrl and right square bracket “]” key combination, which may be difficult in some web based practice environments.

Stop and delete a virtual machine

Because Kubernetes works in a reconciliation pattern, shutting down a VirtualMachine from within the OS will not work as intended. KubeVirt will restart the VM if it is found not running. Instead, use virtctl to stop the VM from the outside.

virtctl stop testvm

Note that the running: true/false behavior is not the only way to control the running status of virtual machines in KubeVirt. See the KubeVirt documentation on run strategies for more information.

To delete the VirtualMachine, use kubectl to delete the object just as any Kubernetes resource:

kubectl delete virtualmachine testvm