Warm tip: This article is reproduced from serverfault.com, please click

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

(docker - Cannot create a Google Compute Engine VM with a container image without en external IP address)

发布于 2020-11-30 07:52:00

我正在尝试通过以下GCLOUD命令使用市集posgresql11映像构建VM(尽管对于我尝试过的所有映像来说,问题似乎都是普遍的):

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文件包含映像的环境变量数据,而metasdata.txt文件包含bootcmd指令以格式化和装入Postgres数据的外部磁盘。

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上启动。

如果我现在通过执行以下命令从cloud命令的网络接口行中删除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地址?

Questioner
Jason Connor
Viewed
0
John Hanley 2020-11-30 16:06:05

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

Google Cloud NAT

Google还提供Private Google Access仅覆盖Google API和服务的提议

私有Google访问