使用机器映像可以存储 Compute Engine 上运行的虚拟机实例的多个磁盘的所有配置、元数据、权限和数据。用于创建机器映像的虚拟机实例称为“来源虚拟机实例”。
如需了解何时以及如何使用机器映像,请参阅机器映像。
本文档介绍了通过来源虚拟机实例创建机器映像的步骤。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
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.
- Set a default region and zone.
- 每 60 分钟最多可以创建特定实例的机器映像 6 次。
- 您可以使用 Cloud Key Management Service (Cloud KMS) 或 CSEK(客户提供的加密密钥)来保护机器映像。加密密钥用于保护磁盘的内容。不过,元数据不受保护。
- 如果您使用 CSEK 来保护机器映像,则在生成机器映像期间,系统会存储所有磁盘数据的完整副本。在其他所有情况下,磁盘数据会存储在差分副本中。
您无法通过符合以下条件的来源虚拟机创建机器映像:
- 挂接的磁盘超过 200 TB。
- 没有挂接任何磁盘。
- 挂接了任何 Hyperdisk 卷。
- 使用 C3D、H3、A3 或 Z3 机器类型
disks.architecture
disks.description
disks.labels
disks.locked
disks.multiWriter
disks.onUpdateAction
disks.provisionedIops
disks.shieldedInstanceInitialState
disks.sourceImage
disks.sourceImageEncryptionKey
disks.sourceInstantSnapshot
disks.sourceSnapshot
disks.sourceSnapshotEncryptionKey
privateIpv6GoogleAccess
resourceManagerTags
resourcePolicies
secure_tag
shieldedInstanceConfig
- 您要创建的机器映像的名称。
- 来源虚拟机实例的名称。
- 来源虚拟机实例所在的可用区。
- 可选的说明。
- 可选的
storageLocation
。 如果您没有指定位置,则默认存储位置为来源虚拟机实例的多区域 Cloud Storage 位置。 - 可选的加密密钥。您可以选择 Google-owned and Google-managed encryption key、Cloud Key Management Service (Cloud KMS) 密钥或客户提供的加密密钥 (CSEK)。如果未指定加密密钥,则系统会使用 Google-owned and Google-managed encryption key对机器映像进行加密。
如果您想使用机器映像进行实例克隆和快照,请先移除实例特有的操作系统和应用信息,然后再通过实例生成机器映像。例如,对于 Windows 虚拟机实例,请使用
GCESysprep
为复制准备系统。MACHINE_IMAGE_NAME
:您要创建的机器映像的名称。SOURCE_VM_NAME
:您要通过其创建映像的来源虚拟机实例的名称。PROJECT_ID
:项目 ID。MACHINE_IMAGE_NAME
:您要创建的机器映像的名称。SOURCE_INSTANCE_URL
:您要用于创建机器映像的来源虚拟机实例的完整网址或部分网址。例如,如果您在名为myProject
的项目中有一个名为my-instance
的源实例。以下网址有效:https://www.googleapis.com/compute/v1/projects/myProject/global/instances/my-instance
projects/myProject/global/instances/my-instance
global/instances/my-instance
- 详细了解机器映像
- 通过机器映像创建虚拟机实例
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 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.
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
局限和限制
机器映像不支持的实例和磁盘属性
通过虚拟机实例创建机器映像或通过机器映像创建虚拟机实例时,系统不会保留或恢复以下实例和磁盘属性:
通过虚拟机创建机器映像
您可以使用Google Cloud 控制台、Google Cloud CLI 或 REST 创建机器映像。
如需创建机器映像,您需要以下信息:
控制台
gcloud
使用
gcloud compute machine-images create
命令可通过实例创建机器映像。gcloud compute machine-images create MACHINE_IMAGE_NAME \ --source-instance=SOURCE_VM_NAME
请替换以下内容:
示例
例如,您可以使用以下
gcloud
命令通过一个名为my-instance
的源实例创建一个名为my-machine-image
的机器映像:gcloud compute machine-images create my-machine-image \ --source-instance=my-instance
此过程需要几分钟的时间。创建机器映像后,您会获得类似于以下内容的输出:
Created [https://www.googleapis.com/compute/v1/projects/project-12345/global/machineImages/my-machine-image]. NAME STATUS my-machine-image READY
REST
在 API 中,构建对
machineimages.insert
方法的POST
请求。在请求正文中,添加以下POST
请求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/machineImages { "name": "MACHINE_IMAGE_NAME", "sourceInstance": "SOURCE_INSTANCE_URL" }
请替换以下内容:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-25。
-