Create VMs in bulk

This document explains how to create virtual machine (VM) instances that are deployed on Hypercompute Cluster. For more information about creating VMs in bulk, see About bulk creation of VMs in the Compute Engine documentation.

After you request blocks of resources and Compute Engine provisions them, you can start consuming these blocks by creating VMs in bulk. This is useful for managing groups of VMs and incorporating your own workload scheduler.

To learn about other ways to create VMs or clusters, see the Overview page.

Before you begin

  • Select the tab for how you plan to use the samples on this page:

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Required roles

To get the permissions that you need to create VMs in bulk, ask your administrator to grant you the Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) IAM role on the project. For more information about granting roles, see Manage access to projects, folders, and organizations.

This predefined role contains the permissions required to create VMs in bulk. To see the exact permissions that are required, expand the Required permissions section:

The following permissions are required to create VMs in bulk:

  • compute.instances.create on the project
  • To use a custom image to create the VM: compute.images.useReadOnly on the image
  • To use a snapshot to create the VM: compute.snapshots.useReadOnly on the snapshot
  • To use an instance template to create the VM: compute.instanceTemplates.useReadOnly on the instance template
  • To assign a legacy network to the VM: compute.networks.use on the project
  • To specify a static IP address for the VM: compute.addresses.use on the project
  • To assign an external IP address to the VM when using a legacy network: compute.networks.useExternalIp on the project
  • To specify a subnet for your VM: compute.subnetworks.use on the project or on the chosen subnet
  • To assign an external IP address to the VM when using a VPC network: compute.subnetworks.useExternalIp on the project or on the chosen subnet
  • To set VM instance metadata for the VM: compute.instances.setMetadata on the project
  • To set tags for the VM: compute.instances.setTags on the VM
  • To set labels for the VM: compute.instances.setLabels on the VM
  • To set a service account for the VM to use: compute.instances.setServiceAccount on the VM
  • To create a new disk for the VM: compute.disks.create on the project
  • To attach an existing disk in read-only or read-write mode: compute.disks.use on the disk
  • To attach an existing disk in read-only mode: compute.disks.useReadOnly on the disk

You might also be able to get these permissions with custom roles or other predefined roles.

Optional: Create a compact placement policy

When you apply compact placement policies to your VMs, Compute Engine makes best-effort attempts to create VMs as close to each other as possible. If you require a minimum compactness to minimize network latency, then specify the maxDistance field when creating a placement policy. A lower maxDistance value ensures closer VM placement, but it also increases the chance that some VMs won't be created.

The following table shows the machine series and number of VMs that each maxDistance value supports:

Maximum distance value Description Supported machine series Maximum number of VMs
Unspecified (Not recommended) Compute Engine makes best-effort attempts to place the VMs as close to each other as possible, but with no maximum distance between VMs. A4 and A3 Ultra 1,500
3 Compute Engine creates VMs in adjacent blocks. A4 1,500
2 Compute Engine creates VMs in the same block. A4 and A3 Ultra For A4 VMs: 150, for A3 Ultra VMs: 256
To create a compact placement policy, select one of the following options:

To create a compact placement policy, use the gcloud beta compute resource-policies create group-placement command:

gcloud beta compute resource-policies create group-placement POLICY_NAME \
    --collocation=collocated \
    --max-distance=MAX_DISTANCE \
    --region=REGION

Replace the following:

  • POLICY_NAME: the name of the compact placement policy.
  • MAX_DISTANCE: the maximum distance configuration for your VMs. The value must be 3 to place VMs in the adjacent blocks, or 2 to place VMs in the same block.
  • REGION: the region where you want to create the placement policy. Specify a region in which the machine type that you want to use is available. For information about regions, see GPU regions and zones.

To create a compact placement policy, make a POST request to the beta resourcePolicies.insert method. In the request body, include the collocation field set to COLLOCATED, and the maxDistance field.

POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies
  {
    "name": "POLICY_NAME",
    "groupPlacementPolicy": {
      "collocation": "COLLOCATED",
      "maxDistance": MAX_DISTANCE
    }
  }

Replace the following:

  • PROJECT_ID: your project ID
  • POLICY_NAME: the name of the compact placement policy.
  • MAX_DISTANCE: the maximum distance configuration for your VMs. The value must be 3 to place VMs in the adjacent blocks, or 2 to place VMs in the same block.
  • REGION: the region where you want to create the placement policy. Specify a region in which the machine type that you want to use is available. For information about regions, see GPU regions and zones.

Create VPC networks

Based on the machine type that you want to use and the number of network interfaces in the machine type, you need to create Virtual Private Cloud (VPC) networks as follows:

Machine type Physical NIC count* Network interfaces Number of VPC networks to create
a4-highgpu-8g 10
  • 2 gVNIC network interfaces used for host to host communication
  • 1 RDMA network interface (attached to a network with 8 subnets) for GPU to GPU communication
3
a3-ultragpu-8g 10
  • 2 gVNIC network interfaces used for host to host communication.
  • 1 RDMA network interface (attached to a network with 8 subnets) for GPU to GPU communication.
3
*For more information about NIC arrangement, see Review network bandwidth and NIC arrangement.
For more information about the network interfaces, see Using Google Virtual NIC and RDMA network profiles.

Set up the networks either manually by following the instruction guides or automatically by using the provided script.

To create the networks, you can use the following instructions:

To create the networks, you can use the following script.

  #!/bin/bash

  # Create standard VPCs (network and subnets) for the gVNICs
  for N in $(seq 0 1); do
    gcloud beta compute networks create GVNIC_NAME_PREFIX-net-$N \
      --subnet-mode=custom

    gcloud beta compute networks subnets create GVNIC_NAME_PREFIX-sub-$N \
      --network=GVNIC_NAME_PREFIX-net-$N \
      --region=REGION \
      --range=10.$N.0.0/16

    gcloud beta compute firewall-rules create GVNIC_NAME_PREFIX-internal-$N \
      --network=GVNIC_NAME_PREFIX-net-$N \
      --action=ALLOW \
      --rules=tcp:0-65535,udp:0-65535,icmp \
      --source-ranges=10.0.0.0/8
  done

  # Create SSH firewall rules
  gcloud beta compute firewall-rules create GVNIC_NAME_PREFIX-ssh \
    --network=GVNIC_NAME_PREFIX-net-0 \
    --action=ALLOW \
    --rules=tcp:22 \
    --source-ranges=IP_RANGE

  # Assumes that an external IP is only created for vNIC 0
  gcloud beta compute firewall-rules create GVNIC_NAME_PREFIX-allow-ping-net-0 \
    --network=GVNIC_NAME_PREFIX-net-0 \
    --action=ALLOW \
    --rules=icmp \
    --source-ranges=IP_RANGE

  # List and make sure network profiles exist
  gcloud beta compute network-profiles list

  # Create network for CX-7
  gcloud beta compute networks create RDMA_NAME_PREFIX-mrdma \
    --network-profile=ZONE-vpc-roce \
    --subnet-mode custom

  # Create subnets.
  for N in $(seq 0 7); do
    gcloud beta compute networks subnets create RDMA_NAME_PREFIX-mrdma-sub-$N \
      --network=RDMA_NAME_PREFIX-mrdma \
      --region=REGION \
      --range=10.$((N+2)).0.0/16  # offset to avoid overlap with gVNICs
  done
  

Replace the following:

  • GVNIC_NAME_PREFIX: the name prefix to use for the standard VPC networks and subnets that use gVNIC NICs.
  • RDMA_NAME_PREFIX: the name prefix to use for the VPC networks and subnets that use RDMA NICs.
  • ZONE: specify a zone in which the machine type that you want to use is available. For information about regions, see GPU regions and zones.
  • REGION: the region where you want to create the networks. This must correspond to the zone specified. For example, if your zone is europe-west1-b, then your region is europe-west1.
  • IP_RANGE: the IP range to use for the SSH firewall rules.

Create VMs

To create your VMs in bulk, use one of the following methods:

To create the VMs, use the gcloud beta compute instances bulk create command:

If you chose to use a compact placement policy, also add the following flag: --resource-policies=POLICY_NAME. Replace POLICY_NAME with the name of the compact placement policy.

gcloud beta compute instances bulk create \
    --name-pattern=NAME_PATTERN \
    --count=COUNT \
    --machine-type=MACHINE_TYPE \
    --image-family=IMAGE_FAMILY \
    --image-project=IMAGE_PROJECT \
    --reservation-affinity=specific \
    --reservation=RESERVATION \
    --provisioning-model=RESERVATION_BOUND \
    --instance-termination-action=DELETE \
    --region=REGION \
    --boot-disk-type=hyperdisk-balanced \
    --boot-disk-size=DISK_SIZE \
    --scopes=cloud-platform \
    --network-interface=nic-type=GVNIC,network=GVNIC_NAME_PREFIX-net-0,subnet=GVNIC_NAME_PREFIX-sub-0 \
    --network-interface=nic-type=GVNIC,network=GVNIC_NAME_PREFIX-net-1,subnet=GVNIC_NAME_PREFIX-sub-1,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-0,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-1,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-2,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-3,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-4,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-5,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-6,no-address \
    --network-interface=nic-type=MRDMA,network=RDMA_NAME_PREFIX-mrdma,subnet=RDMA_NAME_PREFIX-mrdma-sub-7,no-address

Replace the following:

  • NAME_PATTERN: the name pattern of the VMs. For example, using vm-# for the name pattern generates VMs with names such as vm-1 and vm-2, up to the number of VMs specified by --count.
  • COUNT: the number of VMs to create.
  • MACHINE_TYPE: the machine type to use for the VM. This must either be an A4 or A3 Ultra machine type. See GPU machine types.
  • IMAGE_FAMILY: the image family of the OS image that you want to use. For a list of supported operating systems, see Supported operating systems.
  • IMAGE_PROJECT: the project ID of the OS image.
  • RESERVATION: for this value, you can either specify the reservation name or a specific block within a reservation. To get the reservation name or the available blocks, see View capacity. Choose one of the following:
    Reservation value When to use
    RESERVATION_NAME

    For example: exr-5010-01

    • If you are using a placement policy. The placement policy will be applied to the reservation and the VMs are placed on a single block.
    • If you aren't using a placement policy and are ok with VMs placed anywhere in your reservation.
    RESERVATION_NAME/reservationBlocks/RESERVATION_BLOCK_NAME

    For example: exr-5010-01/reservationBlocks/exr-5010-01-block-1

    • If you aren't using a placement policy and want your VMs to be placed in a specific block.
  • REGION: specify a region in which the machine type that you want to use is available. For information about regions, see GPU regions and zones.
  • DISK_SIZE: the size of the boot disk in GB.

To create the VMs, make a POST request to the instances.insert method as follows:

If you chose to use a compact placement policy, also add the placement policy parameter to the request body.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instances/bulkInsert
  {
    {
      "namePattern":"NAME_PATTERN",
      "count":"COUNT",
      "instanceProperties":{
         "machineType":"MACHINE_TYPE",
         "disks":[
            {
               "boot":true,
               "initializeParams":{
                  "diskSizeGb":"DISK_SIZE",
                  "diskType":"hyperdisk-balanced",
                  "sourceImage":"projects/IMAGE_PROJECT/global/images/family/IMAGE_FAMILY"
               },
               "mode":"READ_WRITE",
               "type":"PERSISTENT"
            }
         ],
         "name":"default",
         "networkInterfaces":[
            {
               "network":"NETWORK",
               "nicType":"GVNIC"
            }
         ],
         "reservationAffinity":{
            "consumeReservationType":"SPECIFIC_RESERVATION",
            "key":"compute.googleapis.com/reservation-name",
            "values":[
               "RESERVATION"
            ],
            "scheduling":{
               "provisioningModel":"RESERVATION_BOUND",
               "instanceTerminationAction":"DELETE",
               "automaticRestart":true
            }
         }
      }
   }
  }

Replace the following:

  • PROJECT_ID: the project ID of the project where you want to create the VM.
  • REGION: specify a region in which the machine type that you want to use is available. For information about regions, see GPU regions and zones.
  • NAME_PATTERN: the name pattern of the VMs. For example, using vm-# for the name pattern generates VMs with names such as vm-1 and vm-2, up to the number of VMs specified by --count.
  • COUNT: the number of VMs to create.
  • MACHINE_TYPE: the machine type to use for the VM. This must either be an A4 or A3 Ultra machine type. See GPU machine types.
  • VM_NAME: the name of the VM.
  • DISK_SIZE: the size of the boot disk in GB.
  • IMAGE_PROJECT: the project ID of the OS image.
  • IMAGE_FAMILY: the image family of the OS image that you want to use. For a list of supported operating systems, see Supported operating systems.
  • RESERVATION: for this value, you can either specify the the reservation name or a specific block within a reservation. To get the reservation name or the available blocks, see View capacity. Choose one of the following:
    Reservation value When to use
    RESERVATION_NAME

    For example: exr-5010-01

    • If you are using a placement policy. The placement policy will be applied to the reservation and the VMs are placed on a single block.
    • If you aren't using a placement policy and are ok with VMs placed anywhere in your reservation.
    RESERVATION_NAME/reservationBlocks/RESERVATION_BLOCK_NAME

    For example: exr-5010-01/reservationBlocks/exr-5010-01-block-1

    • If you aren't using a placement policy and want your VMs to be placed in a specific block.
Compact placement policy

If you chose to use a compact placement policy, also add the following flag to the request body:

  "resourcePolicies": [
    "projects/PROJECT_ID/regions/REGION/resourcePolicies/POLICY_NAME"
  ],

Replace the following:

  • PROJECT_ID: the project ID of the compact placement policy.
  • REGION: the region of the compact placement policy.
  • POLICY_NAME: the name of the compact placement policy.

For more information about the configuration options when creating VMs in bulk, see Create VMs in bulk in the Compute Engine documentation.

What's next