无法使用没有外部 IP 地址的容器映像创建 Google Compute Engine VM

Cannot create a Google Compute Engine VM with a container image without en external IP address

我正在尝试使用 marketplace posgresql11 映像构建一个 VM(尽管这个问题对于我尝试过的所有映像来说都是普遍存在的),使用以下 GCLOUD 命令:

gcloud compute instances create-with-container postgres-test \
  --container-image gcr.io/cloud-marketplace/google/postgresql11:latest \
  --container-env-file=envdata.txt \
  --container-mount-host-path mount-path=/var/lib/postgresql,host-path=/mnt/disks/postgres_data,mode=rw \
  --machine-type=e2-small \
  --scopes=cloud-platform \
  --boot-disk-size=10GB \
  --boot-disk-device-name=postgres-test \
  --create-disk="mode=rw,size=10GB,type=pd-standard,name=postgres-test-data,device-name=postgres-test_data" \
  --network-interface=subnet="default,no-address" \
  --tags=database-postgres \
  --metadata-from-file user-data=metadata.txt

envdata.txt 文件包含图像的环境变量数据,metadata.txt 文件包含用于格式化和装载 postgres 数据的外部磁盘的 bootcmd 指令。

envdata.txt:

POSTGRES_USER=postgresuser
POSTGRES_PASSWORD=postgrespassword

metadata.txt:

#cloud-config

bootcmd:
- fsck.ext4 -tvy /dev/sdb
- mkdir -p /mnt/disks/postgres_data
- mount -t ext4 -O ... /dev/sdb /mnt/disks/postgres_data

VM 已创建,但 sudo journalctl 命令显示尝试开始连接到 GCR,但这似乎不成功。用于 postgres 的 docker 映像未下载且未在 VM 上启动。

如果我现在通过执行以下命令从云命令的网络接口行中删除 no-address 命令(允许 google 为 VM 分配外部 IP 地址):

gcloud compute instances create-with-container postgres-test \
  --container-image gcr.io/cloud-marketplace/google/postgresql11:latest \
  --container-env-file=envdata.txt \
  --container-mount-host-path mount-path=/var/lib/postgresql,host-path=/mnt/disks/postgres_data,mode=rw \
  --machine-type=e2-small \
  --scopes=cloud-platform \
  --boot-disk-size=10GB \
  --boot-disk-device-name=postgres-test \
  --create-disk="mode=rw,size=10GB,type=pd-standard,name=postgres-test-data,device-name=postgres-test_data" \
  --network-interface=subnet="default" \
  --tags=database-postgres \
  --metadata-from-file user-data=metadata.txt

然后创建一个VM,下载并执行POSTGRES镜像。 sudo journalctl 显示与 GCR 的连接开始并开始。

任何人都可以向我解释为什么在我的案例中图像的执行取决于拥有外部 IP 以及我如何使用 GCR 创建 VM 而无需为实例分配外部 IP 地址?

如果您有 public IP,那么从您的实例到 Internet 的请求将通过 Internet 网关。如果您的实例没有 public IP,那么您需要设置 Cloud NAT 以提供到 Internet 的路由。这是最简单的解决方案。如果您只需要访问 Google API 和服务而不是 public Internet,请参阅下一个选项。

Google Cloud NAT

Google 还提供 Private Google Access 以仅访问 Google API 和服务。

Private Google Access