1.1. Create a VM

Create your first virtual machine

Creating a virtual machine

To create a virtual machine in our Kubernetes cluster we have to create and apply a VirtualMachine manifest.

This is a very basic example of a bootable virtual machine manifest:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: lab01-firstvm
spec:
  runStrategy: Halted
  template:
    metadata:
      labels:
        kubevirt.io/size: small
        kubevirt.io/domain: lab01-firstvm
    spec:
      domain:
        devices: {}
        resources:
          requests:
            memory: 64M

Task 1.1.2: Review the VirtualMachine manifest

Do you see any problems with the specification above? Try to answer the following questions:

  • What happens when you run this VM?
  • What is required to successfully boot a machine?
Task hint

Our created manifest does not contain any bootable devices. Our VM is able to start, but it will just hang as there are no bootable devices available.

No bootable device

Having a bootable disk within the specification is all you need to start the VM. However, as there is no network interface specified which is connected to the underlying network, our system would not be capable of interacting with the outside world.

Task 1.1.3: Write your own VirtualMachine manifest

Create a new file firstvm.yaml in the folder labs/lab01/ and copy the VirtualMachine manifest from above as a starting point. You then need to add a bootable disk for your VM and a network and interface specification. To achieve this you need to specify the following parts in the VirtualMachine manifest:

  • spec.template.spec.domain.devices
  • spec.template.spec.networks
  • spec.template.spec.volumes

The easiest way is to use an ephemeral containerDisk mountable as a volume. Regarding the network, we simply connect our VM to the underlying Kubernetes default network:

spec:
  template:
    spec:
      domain:
        devices:
          disks:
            - name: containerdisk
              disk: 
                bus: virtio
          interfaces:
            - name: default
              masquerade: {}
      networks:
        - name: default
          pod: {}
      volumes:
        - name: containerdisk
          containerDisk:
            image: quay.io/kubevirt/cirros-container-disk-demo

Make sure you implement the required parts for a container disk and the network interface specification in your VM manifest.

Task hint: Resulting yaml

Your VirtualMachine definition should look like this:

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: lab01-firstvm
spec:
  runStrategy: Halted
  template:
    metadata:
      labels:
        kubevirt.io/size: small
        kubevirt.io/domain: lab01-firstvm
    spec:
      domain:
        devices:
          disks:
            - name: containerdisk
              disk:
                bus: virtio
          interfaces:
            - name: default
              masquerade: {}
        resources:
          requests:
            memory: 64M
      networks:
        - name: default
          pod: {}
      volumes:
        - name: containerdisk
          containerDisk:
            image: quay.io/kubevirt/cirros-container-disk-demo

Task 1.1.4: Create the VirtualMachine

It is now time to create the VM on the Kubernetes cluster using the definition you just created:

kubectl apply -f labs/lab01/firstvm.yaml --namespace lab-<username>

The output should be:

virtualmachine.kubevirt.io/lab01-firstvm created