GCP从现有VM创建实例模板



我正试图从一个关闭电源的现有VM创建一个实例模板。虚拟机是一个非常基本的Centos Linux盒子。在谷歌上搜索了一下这个问题后,我想到了以下内容,但我不知道如何格式化磁盘设备名称。我找不到太多的例子。提前感谢你对这个话题的任何了解。

$gcloud compute instance-templates create plainid-pocv12-cent8-autostart --source-instance ajm-centos8 --configure-disk=device-name=/dev/sda,instantiate-from=source-image,auto-delete=true
ERROR: (gcloud.compute.instance-templates.create) Could not fetch resource:
- Invalid value for field 'resource.sourceInstanceParams.diskConfigs[0].deviceName': '/dev/sda'. Device name specified in disk instantiation config not found in source instance: '/dev/sda'.

根据下面的建议,我运行了描述。看起来设备名称也是"ajm-centos8">

$gcloud compute instances describe ajm-centos8
canIpForward: false
confidentialInstanceConfig:
enableConfidentialCompute: false
cpuPlatform: Intel Haswell
creationTimestamp: '2020-10-05T06:04:50.611-07:00'
deletionProtection: false
description: ''
disks:
- autoDelete: true
boot: true
deviceName: ajm-centos8
diskSizeGb: '30'
guestOsFeatures:
- type: UEFI_COMPATIBLE
- type: VIRTIO_SCSI_MULTIQUEUE
- type: SEV_CAPABLE
index: 0
interface: SCSI
kind: compute#attachedDisk
licenses:
- https://www.googleapis.com/compute/v1/projects/centos-cloud/global/licenses/centos-8
mode: READ_WRITE
source: https://www.googleapis.com/compute/v1/projects/plainid-presales/zones/us-east1-b/disks/ajm-centos8
type: PERSISTENT

现在我正在运行以下命令。它创建了一个模板耶!然而,新模板上没有安装我在原始VM上安装的任何软件。

$gcloud compute instance-templates create plainid-pocv12-cent8-autostart --source-instance ajm-centos8 --configure-disk=device-name=ajm-centos8,instantiate-from=source-image,auto-delete=true
Created [https://www.googleapis.com/compute/v1/projects/plainid-presales/global/instanceTemplates/plainid-pocv12-cent8-autostart].
NAME                            MACHINE_TYPE                   PREEMPTIBLE  CREATION_TIMESTAMP
plainid-pocv12-cent8-autostart  custom (e2, 2 vCPU, 6.00 GiB)               2020-10-09T13:55:02.563-07:00

为了能够将安装在VM上的任何程序保留到实例模板中,必须使用原始实例中使用的映像创建实例模板。

按照以下步骤从您的原始实例创建图像:

  1. 转到控制台菜单>计算引擎>图像
  2. 单击Create Image
  3. 为您的图像提供Name
  4. 选择Image作为Source
  5. 单击"创建">

从这里,您可以使用创建的映像创建实例模板,并且映像上预装的所有应用程序或程序都将被保留。

实例模板主要用于定义和保存机器配置,如机器类型、启动磁盘映像或容器映像、标签和其他实例属性。然后,该模板可以立即用于创建单独的VM或MIG。您可以查看实例模板以了解更多详细信息。

基于现有实例创建实例模板的文档在这里看起来很有用。在本文档中,您可以查看用于覆盖如何在模板中定义磁盘的选项。

正如其他答案所指出的:

  • 实例模板:保存VM配置,如机器类型、服务帐户、作用域等。但不保存磁盘
  • image:保存可由根据模板创建的虚拟机使用的磁盘

因此,您需要创建一个图像,然后创建一个使用该图像的模板。以下是如何从命令行执行操作:

# Create the Golden VM with all the options you want
$ gcloud compute instances create goldenvm --image-project=almalinux-cloud --image-family=almalinux-8 ...
# Log to goldenvm and work your magic
$ ssh goldenvm...
# Power off goldenvm
# Create an image from goldenvm disk
$ gcloud compute images create goldenvm-image --source-disk=goldenvm --family=almalinux-8 --storage-location=europe-west2
# Create a template from goldenvm that will use goldenvm-image
$ gcloud compute instance-templates create goldenvm-template --source-instance=goldenvm --configure-disk=device-name=persistent-disk-0,instantiate-from=custom-image,custom-image="projects/your-project/global/images/goldenvm-image"
# To create VMs using the template
$ gcloud compute instances create  testvm --source-instance-template=goldenvm-template

最新更新