在使用 gcloud 控制台创建的 Container Optimized OS [COS] 上的 GCP 计算实例中安装磁盘

mounting disk in GCP compute instance on Container Optimized OS [COS] created using gcloud console

所以我正在使用这个 gcloud 控制台命令从容器镜像创建一个实例

gcloud compute instances create-with-container test-instance \
--zone us-xx \
--container-image asia.gcr.io/my-project/my-docker-image \
--container-privileged \
--network my-network \
--subnet my-net-sub \
--create-disk name=test-data,device-name=test-data,auto-delete=yes,size=200GB,type=pd-ssd \
--container-mount-disk name=test-data,mount-path=/mnt/disks/data \
--service-account me@myproject.iam.gserviceaccount.com

它工作正常并创建了实例,但它没有挂载数据磁盘。为什么? 更准确地说,要添加我需要的数据磁盘

如何指定用ext4创建分区,然后挂载分区部分?

您不能将主机的磁盘挂载到容器中(两者使用相同的磁盘)。 You can however mount a directory or another disk。无论哪种方式,您都可以在其上存储数据,并且 OS'es(主机和容器)都可以从中 read/write。

假设您想要将所有数据存储在 /datadir/ 的主机 OS 磁盘中,并且您希望将其安装在 /mnt/disks/data 下的容器内。您将在下面找到一个完整的(经过测试的)示例来使用:

gcloud compute instances create-with-container mytestvm1 \
--zone=europe-west3-c \
--container-image=gcr.io/google-containers/mycontainer \
--container-privileged \
--network default \
--subnet default \
--create-disk name=test-data,device-name=test-data,auto-delete=yes,size=20GB,type=pd-ssd \
--container-mount-host-path=mount-path=/mnt/disks/data,host-path=/home/myhomedir/,mode=rw \
--service-account=my_service_account@developer.gserviceaccount.com

如果您需要安装另一个磁盘,只需更改行:

--container-mount-host-path=mount-path=/mnt/disks/data,host-path=/home/myhomedir/,mode=rw \

--container-mount-disk=mount-path=/mnt/disks/data,name=data1,mode=rw \