gcloud 只能使用私有镜像创建实例

gcloud can only create instances using private image

我有一个名为 'test-1' 的私有镜像,我可以用它来创建一个实例

$ gcloud compute instances create demo --image test-1

但是我不能使用 public 图片:

$ gcloud compute instances create demo --image ubuntu-1804-bionic-v20190204 
ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - Invalid value for field 'resource.disks[0].initializeParams.sourceImage': 'https://www.googleapis.com/compute/v1/projects/szabgab-149/global/images/ubuntu-1804-bionic-v20190204'. The referenced image resource cannot be found.

也不是 public 图像系列:

$ gcloud compute instances create demo --image-family ubuntu-1804-lts 
ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - The resource 'projects/szabgab-149/global/images/family/ubuntu-1804-lts' was not found

我做错了什么?

我注意到在生成的错误消息中,您正在生成图像的位置,就好像它位于您的项目中一样,我知道它被命名为 szabgab-149:

The resource 'projects/szabgab-149/global/images/family/ubuntu-1804-lts'

问题是 public 图像位于它自己的项目中,请参阅命令 gcloud compute images list 的输出。

例如,ubuntu-1804-bionic-v20190204 在项目 ubuntu-os-cloud 和图像系列 ubuntu-1804-lts 下。

要解决这个问题,你可以简单地添加--image-project标志,从前面的gcloud命令的信息,你执行这个命令,使用这个特定的图像:

gcloud compute instances create demo \
--image ubuntu-1804-bionic-v20190204 \
--image-project ubuntu-os-cloud

还是这个,使用项目ubuntu-os-cloud和家族ubuntu-1804-lts下的默认图片:

gcloud compute instances create demo \
--image-family ubuntu-1804-lts \
--image-project ubuntu-os-cloud
https://github.com/khushbuparakh/gcp/blob/master/instance.py

你可以用这个。它创建了一个桶。将您的私人文件上传到存储桶。使用该文件创建图像并启动实例