我正在尝试通过以下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地址?
如果你具有公共IP,则从你的实例到Internet的请求将通过Internet网关。如果你的实例没有公共IP,则需要设置Cloud NAT以提供到Internet的路由。这是最简单的解决方案。如果你只需要访问Google API和服务,而无需访问公共Internet,请参阅下一个选项。
Google还提供Private Google Access
仅覆盖Google API和服务的提议。
约翰,您好,感谢您的回复。我似乎遇到的问题是该实例无法访问GCR,后者是内部Google服务。提出这个问题的原因恰恰是,为什么既然实例的创建由于GCR是内部的而不需要访问Internet,那么为什么实例的创建似乎依赖于外部访问呢?
GCR服务未连接到您的内部网络(VPC)。它是Google网络的一部分。因此,您必须通过我的答案中的一种方法创建连接。