利用映像系列来简化映像版本控制。将映像添加到映像系列以将其设置为最新的映像版本。如果您确定必须将映像系列回滚到以前的映像版本,请弃用系列中最新的映像。
如需了解使用映像系列时的最佳做法,请参阅映像系列最佳做法。
(可选)您可以使用Google Cloud 控制台、带有 --storage-location 标志的 gcloud compute images create 命令或者 images().insert 方法指定映像的存储位置。
准备工作
- 阅读映像文档。
-
如果您尚未设置身份验证,请进行设置。身份验证用于验证您的身份,以便访问 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
-
安装 Google Cloud CLI,然后使用联合身份登录 gcloud CLI。 登录后,运行以下命令来初始化 Google Cloud CLI:
gcloud init - Set a default region and zone.
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。
安装 Google Cloud CLI,然后使用联合身份登录 gcloud CLI。 登录后,运行以下命令来初始化 Google Cloud CLI:
gcloud init如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
-
在映像系列中设置映像版本
控制台
在 Google Cloud 控制台中,前往创建映像页面。
指定映像的名称。 例如
image-v1。指定您要根据其创建映像的来源。可以是 Cloud Storage 中的永久性磁盘、快照、其他映像或 disk.raw 文件。
如果您从挂接到正在运行的虚拟机的磁盘来创建映像,请勾选保持实例运行,以确认您要在虚拟机保持运行的状态下创建映像。您可以在创建映像之前准备虚拟机。
在基于来源磁盘位置(默认)下拉列表中,指定映像的存储位置。例如,指定
us可将映像存储在us多区域中,或指定us-central1可将其存储在us-central1区域中。如果您未选择位置,Compute Engine 会将映像存储在最接近映像来源位置的多区域中。为新映像指定映像系列。例如,添加
my-image-family可将映像整理到一个映像系列中。可选:指定其他映像属性:
- 说明:自定义映像的说明。
- 标签:用于将资源组织在一起的标签。
指定加密密钥。您可以选择 Google-owned and Google-managed encryption key、Cloud Key Management Service (Cloud KMS) 密钥或客户提供的加密密钥 (CSEK)。如果未指定加密密钥,则系统会使用 Google-owned and Google-managed encryption key对映像进行加密。
点击创建来创建映像。
gcloud
gcloud compute images create image-v1 \
--source-disk disk-1 \
--source-disk-zone us-central1-f \
--family my-image-family
映像系列指向 image-v1。在该系列中添加第二个映像:
gcloud compute images create image-v2 \
--source-disk disk-2 \
--source-disk-zone us-central1-f \
--family my-image-family
REST
向 images().insert 方法发出 POST 请求。在请求正文中指定映像系列。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images
{
"name": "image-v2",
"sourceDisk": "/zones/us-central1-f/disks/disk-2",
"family":"my-image-family",
}
该映像系列指向 image-v2,因为这是您添加到映像系列的最新映像。
gcloud
您可以运行 gcloud compute images describe-from-family 命令来查看某个系列指向哪个映像。
例如:
gcloud compute images describe-from-family my-image-family
family: my-image-family
id: '8904691942610171306'
kind: compute#image
name: image-v2
selfLink: https://compute.googleapis.com/compute/v1/projects/my-project/global/images/image-v2
sourceDisk: https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-f/disks/disk-v2
sourceDiskId: '1677449456001963379'
sourceType: RAW
status: READY
REST
向 images().getFromFamily 方法发出 GET 请求。在请求正文中指定映像系列。
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/images/family
{
"resourceId":"my-image-family",
}
如果您确定必须回滚该映像系列,以使其不再指向 image-v2,请弃用 image-v2,这样该系列将再次指向 image-v1。
gcloud compute images deprecate image-v2 \
--state DEPRECATED \
--replacement image-v1
检查以确保该映像系列指向 image-v1。
gcloud compute images describe-from-family my-image-family
family: my-image-family
id: '2741732787056801255'
kind: compute#image
name: image-v1
selfLink: https://compute.googleapis.com/compute/v1/projects/my-project/global/images/image-v1
sourceDisk: https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-f/disks/disk-v1
sourceDiskId: '1677449456001963379'
sourceType: RAW
status: READY