Create a VM with specific CPU and memory resources

When you create VMs that use VM Runtime on Google Distributed Cloud, you can configure compute resources in two ways:

  • Manually specify CPU and memory: Define compute resources directly in the VirtualMachine manifest. This approach is useful for one-time VMs or custom workloads that require unique resource allocations.
  • Create and use VM types: Define reusable VirtualMachineType custom resources that standardize CPU and memory allocations. This approach is useful for managing fleets of VMs, ensuring compute consistency across your cluster, and simplifying VM provisioning for developers.

Before you begin

To manually specify CPU and memory resources and to create and use VM types, you need access to the following resources:

  • A Distributed Cloud connected cluster, version 1.9.0 or higher.
  • The Kubernetes command-line tool, kubectl, installed and configured to access your cluster. For more information, see install kubectl.
  • Optional: The virtctl client tool installed as a plugin for kubectl. If needed, see Install the virtctl management tool.

Create a VM and specify the CPU and memory

When you create a VM, you can manually specify the CPU and memory requirements. Use this feature to create VMs that have the appropriate compute resources to match your application needs.

To create a VM and specify the CPU and memory, use a VirtualMachine manifest.

  1. In the editor of your choice, create a VirtualMachine manifest, such as my-custom-vm.yaml:

    apiVersion: vm.cluster.gke.io/v1
    kind: VirtualMachine
    metadata:
      name: VM_NAME
      namespace: NAMESPACE
    spec:
      compute:
        cpu:
          vcpus: VCPU_NUMBER
        memory:
          capacity: MEMORY_SIZE
      interfaces:
        - name: eth0
          networkName: L2_NETWORK_NAME
          default: true
      disks:
        - virtualMachineDiskName: VM_BOOT_NAME-boot-dv
          boot: true

    In this YAML file, define the following settings:

    • VM_NAME: the name for your VM.
    • NAMESPACE: the target namespace for your VM.
    • L2_NETWORK_NAME: The name of the L2 network to connect the VM to.
    • VCPU_NUMBER: The number of vCPUs to assign to the VM. You can assign between 1 and 96 vCPUs to a VM.
    • MEMORY_SIZE: The amount of memory to assign to the VM. You can assign between 1 Mi and 1 Ti of memory to a VM. For more information, see Memory resource units in the Kubernetes documentation.
    • VM_BOOT_NAME: the name of your VM boot disk. The boot disk must already exist. For more information, see Create a virtual machine disk from a virtual machine image.

    The VM connects the eth0 interface to the specified L2 network.

  2. To create the VM, apply the manifest to your Distributed Cloud connected cluster using the kubectl apply command:

    kubectl apply -f my-custom-vm.yaml
    
  3. To verify that the VM is created, use the kubectl get command:

    kubectl get vm VM_NAME -n NAMESPACE
    

    The expected output shows the VM status. You can also use the kubectl describe command to view the detailed configuration, including the assigned CPU and memory.

Create and use VM types

When you enable VM Runtime on GDC, the VirtualMachineType custom resource definition becomes available in your cluster. You can use this resource type to create reusable templates that define specific CPU and memory allocations. By creating custom VM types for your different workloads, you can provision multiple VMs with consistent compute resource configurations.

If VM Runtime on GDC is enabled in Distributed Cloud connected, a predefined VM type might be available. You can't update this predefined VM type. The following definition shows the default example-machinetype VM type:

apiVersion: vm.cluster.gke.io/v1
kind: VirtualMachineType
metadata:
  name: "example-machinetype"
  labels:
    vm.cluster.gke.io/predefined-machinetype: "true"
spec:
  cpu:
    vcpus: 2
  memory:
    capacity: 4G

Create a VM type

You can create your own VM types to fit the compute needs of your workloads.

  1. In the editor of your choice, create a VirtualMachineType manifest, such as my-vm-type.yaml:

    apiVersion: vm.cluster.gke.io/v1
    kind: VirtualMachineType
    metadata:
      name: VM_TYPE_NAME
    spec:
      cpu:
        vcpus: VCPU_NUMBER
      memory:
        capacity: MEMORY_SIZE

    In this VM type, define the following settings:

    • VM_TYPE_NAME: the name for your VM type.
    • VCPU_NUMBER: the number of vCPUs to assign to the VM. You can assign between 1 and 96 vCPUs to a VM.
    • MEMORY_SIZE: the amount of memory to assign to the VM. You can assign between 1 Mi and 1 Ti of memory to a VM. For more information, see Memory resource units in the Kubernetes documentation.
  2. To create the VM type, apply the manifest to your Distributed Cloud connected cluster using the kubectl apply command:

    kubectl apply -f my-vm-type.yaml
    
  3. To verify that the VM type is created, run the kubectl get command:

    kubectl get virtualmachinetype VM_TYPE_NAME
    

Create a VM using a VM type

To apply the CPU and memory settings defined in a VM type to your VM, specify the name of the VM type in the compute section of the VirtualMachine manifest.

  1. In the editor of your choice, create a VirtualMachine manifest, such as my-custom-vm.yaml. In this YAML file, specify the name of the custom VM type that you created in the previous section, such as my-vm-type, as the value for the virtualMachineTypeName.

    apiVersion: vm.cluster.gke.io/v1
    kind: VirtualMachine
    metadata:
      name: VM_NAME
      namespace: NAMESPACE
    spec:
      compute:
        virtualMachineTypeName: VM_TYPE_NAME
      interfaces:
        - name: eth0
          networkName: L2_NETWORK_NAME
          default: true
      disks:
        - virtualMachineDiskName: VM_BOOT_NAME-boot-dv
          boot: true

    Define the following settings:

    • VM_NAME: the name for your VM.
    • NAMESPACE: the target namespace for your VM.
    • L2_NETWORK_NAME: The name of the L2 network to connect the VM to.
    • VM_TYPE_NAME: the name of the custom VM type that you created in the previous section, such as my-vm-type.
    • VM_BOOT_NAME: the name of your VM boot disk. The boot disk must already exist. For more information, see Create a virtual machine disk from a virtual machine image.

    The VM connects the eth0 interface to the specified L2 network.

  2. To create the VM, apply the manifest to your Distributed Cloud connected cluster using the kubectl apply command:

    kubectl apply -f my-custom-vm.yaml
    
  3. To verify that the VM is using the VM type, run the kubectl get command with the -o jsonpath option:

    kubectl get vm VM_NAME -n NAMESPACE -o jsonpath='{.spec.compute.virtualMachineTypeName}'
    

    The expected output displays the name of your VM type.

What's next