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 https://kubevirt.io/labs/manifests/vm.yaml 

Check on the virtual machine you just created:

kubectl get vm

NAME     AGE   STATUS    READY
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
virtualmachine.kubevirt.io/testvm   26s   Running   True


NAME                                        AGE   PHASE     IP           NODENAME   READY
virtualmachineinstance.kubevirt.io/testvm   7s    Running   10.42.0.16   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