Updated on 2025-04-27 GMT+08:00

Creating a VM

Creating a VM Using a Manifest File

  1. Create a VM as follows:

    kubectl create -f - <<EOF
    apiVersion: kubevirt.io/v1
    kind: VirtualMachine
    metadata:
      name: testvm
    spec:
      runStrategy: Halted
      template:
        metadata:
          labels:
            kubevirt.io/size: small
            kubevirt.io/domain: testvm
        spec:
          domain:
            devices:
              disks:
                - name: containerdisk
                  disk:
                    bus: virtio
                - name: cloudinitdisk
                  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
            - name: cloudinitdisk
              cloudInitNoCloud:
                userDataBase64: SGkuXG4=
    EOF

    If the command is executed successfully, the following information is displayed:

    virtualmachine.kubevirt.io/testvm created

  2. (The VM is stopped because runStrategy is set to Halted.) Check the VM status.

    kubectl get vms
    Example output:
    NAME     AGE   STATUS    READY
    testvm   73s   Stopped   False

Creating a VM with Specific Resources Using virtctl

The create vm subcommand of virtctl allows you to easily create your VirtualMachine resource manifest through the command line. By default, this command checks the instance type, preference setting, and inference function, and provides multiple flags to control the detailed settings of the VM. You can run the following command to view all available configuration items:

virtctl create vm -h

For example, to create a VM named vm-cirros with 4-GiB memory, run the following command:

virtctl create vm --name vm-cirros --memory "4Gi" | kubectl create -f -

For more information about creating a VM, see https://kubevirt.io/user-guide/user_workloads/lifecycle/.