Google compute engine 同时创建多个GCE虚拟机的脚本

Google compute engine 同时创建多个GCE虚拟机的脚本,google-compute-engine,Google Compute Engine,我有一个基本的SH脚本,我用它在GCP上创建多个vm,它运行良好,但顺序良好。当虚拟机的数量超过4或5时,就变成了时间的实质性延迟。我注意到,在Dataflow或Dataproc等平台中,几乎同时创建任意数量的虚拟机。有没有办法在GCE中模仿这种功能?(毕竟,这些似乎是基本的GCE机器) 现在我使用以下(简化)脚本: 谢谢你的建议 直接方式 将--async参数添加到gcloud命令中是一个快速的胜利。 此外,您可以在bash中使用wait和&添加并行化: for i in `seq 1 4`

我有一个基本的SH脚本,我用它在GCP上创建多个vm,它运行良好,但顺序良好。当虚拟机的数量超过4或5时,就变成了时间的实质性延迟。我注意到,在Dataflow或Dataproc等平台中,几乎同时创建任意数量的虚拟机。有没有办法在GCE中模仿这种功能?(毕竟,这些似乎是基本的GCE机器)

现在我使用以下(简化)脚本:


谢谢你的建议

直接方式

--async
参数添加到
gcloud
命令中是一个快速的胜利。 此外,您可以在bash中使用
wait
&
添加并行化:

for i in `seq 1 4`
do
  gcloud compute instances [...] --async &
done
wait
备选方案


您可以使用以不同的方式进行操作

直接方式

--async
参数添加到
gcloud
命令中是一个快速的胜利。 此外,您可以在bash中使用
wait
&
添加并行化:

for i in `seq 1 4`
do
  gcloud compute instances [...] --async &
done
wait
备选方案


您可以使用以不同的方式创建多个类似的虚拟机

您可以更快地创建多个类似的虚拟机

首先,指定您需要的VM配置:

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 \
  [...]
注:

  • 您可以将启动磁盘指定为实例模板的一部分
  • 无需为实例模板指定区域。您将在创建实例组时指定所需的区域
  • 组中所有VM的引导磁盘的设备名称相同。这不是冲突,因为特定磁盘的设备名是每个特定VM的名称,并且是该VM的本地名称
  • 其他参数与用于创建VM的参数相同
然后,基于此模板创建一组4个(或100个或1000多个)虚拟机:

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

该组基于您的模板创建多个类似的VM,比您通过迭代创建独立VM要快得多

您可以更快地创建多个类似的虚拟机

首先,指定您需要的VM配置:

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 \
  [...]
注:

  • 您可以将启动磁盘指定为实例模板的一部分
  • 无需为实例模板指定区域。您将在创建实例组时指定所需的区域
  • 组中所有VM的引导磁盘的设备名称相同。这不是冲突,因为特定磁盘的设备名是每个特定VM的名称,并且是该VM的本地名称
  • 其他参数与用于创建VM的参数相同
然后,基于此模板创建一组4个(或100个或1000多个)虚拟机:

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
该组基于您的模板创建多个类似的VM,比您通过迭代创建独立VM要快得多

允许您使用单个API请求创建多个VM:

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 \
  [...]
请注意:

  • 所有虚拟机都是并行创建的
  • 它会根据可用性的位置自动选择创建虚拟机的区域
  • 如果它检测到阻止创建完整请求的问题(例如,如果没有足够的容量),它将提前失败
  • 它可以自动为您生成名称
允许您使用单个API请求创建多个虚拟机:

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 \
  [...]
请注意:

  • 所有虚拟机都是并行创建的
  • 它会根据可用性的位置自动选择创建虚拟机的区域
  • 如果它检测到阻止创建完整请求的问题(例如,如果没有足够的容量),它将提前失败
  • 它可以自动为您生成名称

谢谢你的异步建议——听起来不错。在这种情况下,我将如何在循环的每个迭代中协调硬盘驱动器和虚拟机的创建?或者我想我可以先异步创建驱动器,然后再基于这些驱动器异步创建虚拟机?(我对bash-TBH一无所知)是的,我会首先在一个专用循环中创建持久化磁盘,该循环没有
--async
参数,但使用
&
等待
进行并行创建。第二个循环只有在磁盘准备好使用时才能开始。小心gcloud错误,许多呼叫可能会失败(配额、权限、错误请求等)。感谢您的建议。接受另一个答案,因为它看起来更像GCP-native。事实上,这取决于你想做什么。例如,如果您想要稳定的实例名称(用于简单的发现目的或其他目的),则不能使用托管实例组。更糟糕的是,实例模板AFAIK不支持额外的持久磁盘。但是,如果您不需要这些特性,那么使用托管实例组会更干净。谢谢您的异步建议——听起来很棒。在这种情况下,我将如何在循环的每个迭代中协调硬盘驱动器和虚拟机的创建?或者我想我可以先异步创建驱动器,然后再基于这些驱动器异步创建虚拟机?(我对bash-TBH一无所知)是的,我会首先在一个专用循环中创建持久化磁盘,该循环没有
--async
参数,但使用
&
等待
进行并行创建。第二个循环只有在磁盘准备好使用时才能开始。小心gcloud错误,许多呼叫可能会失败(配额、权限、错误请求等)。感谢您的建议。接受另一个答案,因为它看起来更像GCP-native。事实上,这取决于你想做什么。例如,如果您想要稳定的实例名称(用于简单的发现目的或其他目的),则不能使用托管实例组。更糟糕的是,实例模板AFAIK不支持额外的持久磁盘。但是,如果您不需要这些功能,那么使用托管实例组会更干净。谢谢,这非常有效。与脚本相比,这里唯一需要注意的是,似乎无法控制机器的确切主机名(它们都以随机后缀结尾)