Reputation: 2363
I have a basic SH script that I use to create multiple VMs on GCP, and it works fine, but sequentially. When a number of VMs is say above 4 or 5, it becomes a material delay of time. I noticed that in platforms like Dataflow or Dataproc, an arbitrary number of VMs gets created virtually simultaneously. Is there a way to mimic that functionality in GCE? (after all, these seem to be basic GCE machines anyway).
Right now I use the following (simplified) script:
vms=4
for i in `seq 1 $vms`
do
gcloud compute --project PROJECT disks create VM"$i" --size 50 --zone ZONE --type "pd-ssd"
gcloud beta compute --project=PROJECT instances create VM"$i" --zone=ZONE --machine-type=MACHINE_TYPE --subnet=default --maintenance-policy=MIGRATE --scopes=https://www.googleapis.com/auth/cloud-platform --disk=name=VM"$i",device-name=VM"$i",mode=rw,boot=yes,auto-delete=yes
done
Thanks for any suggestions!
Upvotes: 2
Views: 2852
Reputation: 626
The bulk instance creation APIs allow you to create many VMs with a single API request:
gcloud compute instances bulk create \
--name-pattern="VM#" \
--count=4 \
--region=REGION \
--machine-type=MACHINE_TYPE \
--image-project=IMAGE_PROJECT \ # project where your boot disk image is stored
--image=IMAGE \ # boot disk image name
--boot-disk-type=pd-ssd \
--boot-disk-size=50GB \
--boot-disk-auto-delete \
--boot-disk-device-name=DEVICE_NAME \ # boot disk device name, the same for all VMs
--subnet=default \
--maintenance-policy=MIGRATE \
[...]
Note that:
Upvotes: 0
Reputation: 64
You can create multiple similar VMs faster by creating a group of managed VMs.
First, create an instance template, specifying the VM configuration that you need:
gcloud compute instance-templates create TEMPLATE_NAME \
--machine-type MACHINE_TYPE \
--image-project IMAGE_PROJECT \ # project where your boot disk image is stored
--image IMAGE \ # boot disk image name
--boot-disk-type pd-ssd \
--boot-disk-size 50GB \
--boot-disk-auto-delete \
--boot-disk-device-name DEVICE_NAME \ # boot disk device name, the same for all VMs
--subnet default \
--maintenance-policy MIGRATE \
[...]
Note:
Then, create a group of 4 (or 100, or 1000+) VMs, based on this template:
gcloud compute instance-groups managed create GROUP_NAME \
--zone ZONE \
--template TEMPLATE_NAME \ # name of the instance template that you have just created
--size 4 \ number of VMs that you need to create
The group creates multiple similar VMs, based on your template, much faster than you would do it by iterating creation of standalone VMs.
Upvotes: 4
Reputation: 1426
Direct way
A quick win would be to add the --async
parameters to the gcloud
command.
Moreover, you can add parallelization in bash with wait
and &
:
for i in `seq 1 4`
do
gcloud compute instances [...] --async &
done
wait
Alternative
You can use terraform to do it in a different way
Upvotes: 1