Mongodb GCE“;使用容器创建--容器安装磁盘“;标志将磁盘装载为只读

Mongodb GCE“;使用容器创建--容器安装磁盘“;标志将磁盘装载为只读,mongodb,docker,google-compute-engine,gce-persistent-disk,Mongodb,Docker,Google Compute Engine,Gce Persistent Disk,我试图在GCE上为MongoDB使用Percona Docker映像,但是我遇到了一个问题,Mongo说挂载路径是只读的。我尽可能地环顾四周,但我对可能出现的问题感到困惑 gcloud compute instances create-with-container mongo-svr \ --create-disk name=disk-1,size=1GB \ --container-mount-disk mount-path="/data/mongodb",mode=rw \ --contai

我试图在GCE上为MongoDB使用Percona Docker映像,但是我遇到了一个问题,Mongo说挂载路径是只读的。我尽可能地环顾四周,但我对可能出现的问题感到困惑

gcloud compute instances create-with-container mongo-svr \
--create-disk name=disk-1,size=1GB \
--container-mount-disk mount-path="/data/mongodb",mode=rw \
--container-image=docker.io/percona/percona-server-mongodb:4.2
我使用了上面的命令,它创建了我的实例。然后,我使用SSH连接到服务器,连接到正在运行的mongo实例以关闭,然后运行:
docker exec-it[NAME]mongod--configsvr--replSet rs0--dbpath=/data/mongodb--bind_ip localhost

这会抛出一个错误,指出:

CONTROL  [initandlisten] options: { net: { bindIp: "localhost" }, replication: { replSet: "rs0" }, sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/data/mongodb" } }
STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/mongodb, terminating
在这一点上,我一直在用不同的参数重新创建实例,但到目前为止没有任何效果。有人知道我错过了什么吗

使用命令输出更新

gcloud compute instances create-with-container mongo-config-f --zone us-central1-f --create-disk name=disk-1,size=1GB --container-mount-disk mount-path="/data/mongodb" --container-image=docker.io/percona/percona-server-mongodb:4.2 --machine-type=f1-micro
WARNING: Default device-name for disk name [disk-1] will be [disk-1] because it is being mounted to a container with [`--container-mount-disk`]
Created [https://www.googleapis.com/compute/v1/projects/[PROJECT_NAME]/zones/us-central1-f/instances/mongo-config-f].
NAME            ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
mongo-config-f  us-central1-f  f1-micro                   xx.xx.xx.xx  xx.xx.xx.xx     RUNNING

我尝试在我的测试项目中复制您的问题,发现:

  • 按照预期以读写模式创建并装载了持久磁盘

    bash-4.2$ mount 
    ...
    /dev/sdb on /data/mongodb type ext4 (rw,relatime)
    
  • docker在我们的虚拟机中正确运行容器

  • 运行
    docker exec-it[NAME]mongod--configsvr--replSet rs0--dbpath=/data/mongodb--bind_ip localhost
    时出错的原因是mongodb容器内的权限:

    bash-4.2$ ls -l /data/        
    ...
    drwxr-xr-x 3 root    root 4096 Feb 19 15:33 mongodb
    
    $ docker exec -it klt-mongo-svr-upd-wowt /bin/bash
    bash-4.2$ mount 
    ...
    /dev/sdb on /data/mongodb type ext4 (rw,relatime)
    ...
    
作为一种解决方法,可以使用root权限执行命令

$ docker exec -it --user root klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --db path=/data/mongodb
请在下面查找更多详细信息和我的步骤:

  • 创建虚拟机:

    $ gcloud compute instances create-with-container mongo-svr \
    --create-disk name=disk-1,size=1GB \                                                                                 
    --container-image docker.io/percona/percona-server-mongodb:4.2 \
    --container-mount-disk mount-path="/data/mongodb"                               
    WARNING: Default device-name for disk name [disk-1] will be [disk-1] because it is being mounted to a container with [`--container-mount-disk`]
    Created [https://www.googleapis.com/compute/v1/projects/test-prj/zones/europe-west3-a/instances/mongo-svr].
    NAME           ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
    mongo-svr-upd  europe-west3-a  n1-standard-1               10.156.0.9   35.XXX.155.XXX  RUNNING
    
  • SSH到实例

  • 检查容器是否正在运行:

    $ docker ps
    CONTAINER ID        IMAGE                                                                COMMAND                  CREATED              STATUS              PORTS               NAMES
    dfad9c10235d        percona/percona-server-mongodb:4.2                                   "/entrypoint.sh mong…"   About a minute ago   Up About a minute                       klt-mongo-svr-upd-wowt
    bbe02c8e8621        gcr.io/stackdriver-agents/stackdriver-logging-agent:0.2-1.5.33-1-1   "/entrypoint.sh /usr…"   About a minute ago   Up About a minute                       stackdriver-logging-agent
    
    在这一点上,一切看起来都很好

  • 尝试以用户身份运行命令:

     $ docker exec -it klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --dbpath=/data/mongodb --bind_ip localhost
    
    并观察相同的错误:

    2020-02-19T15:37:53.176+0000 I  STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/mongodb, terminating
    
    此处键只读目录:/data/mongodb

  • 检查容器内的装载和权限:

    bash-4.2$ ls -l /data/        
    ...
    drwxr-xr-x 3 root    root 4096 Feb 19 15:33 mongodb
    
    $ docker exec -it klt-mongo-svr-upd-wowt /bin/bash
    bash-4.2$ mount 
    ...
    /dev/sdb on /data/mongodb type ext4 (rw,relatime)
    ...
    
    正如我们所料,磁盘是以读写模式创建并装入容器的

    bash-4.2$ ls -l /data/        
    total 8
    drwxr-xr-x 4 mongodb root 4096 Feb 19 15:36 db
    drwxr-xr-x 3 root    root 4096 Feb 19 15:33 mongodb
    bash-4.2$ 
    
    但是要使用
    /data/mongodb
    您需要
    root
    权限

  • 尝试以root用户身份运行命令:

    $ docker exec -it --user root klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --dbpath=/data/mongodb
    2020-02-19T15:45:24.970+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
    2020-02-19T15:45:24.973+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=119 port=27019 dbpath=/data/mongodb 64-bit host=mongo-svr-upd
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] db version v4.2.2-3
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] git version: 2cdb6e50913583f627acc5de35dc4e04dbfe196f
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] modules: none
    2020-02-19T15:45:24.974+0000 I  CONTROL  [initandlisten] build environment:
    2020-02-19T15:45:24.975+0000 I  CONTROL  [initandlisten]     distarch: x86_64
    2020-02-19T15:45:24.975+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
    2020-02-19T15:45:24.975+0000 I  CONTROL  [initandlisten] options: { replication: { replSet: "rs0" }, sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/data/mongodb" } }
    2020-02-19T15:45:24.976+0000 I  STORAGE  [initandlisten] Detected data files in /data/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
    ...
    
    并且它正在使用根权限


  • 我知道不需要
    mode=rw
    ,但希望明确,因为什么都不起作用。带/不带命令运行命令会产生相同的结果。至于挂载,没有挂载/data/mongodb。但是,我的理解是,您为
    --create disk
    (disk-1)使用的名称将导致
    /mnt/disks/gce containers mounts/gce persistent disks/disk-1
    ,它将作为docker容器的安装点,在内部映射到
    /data/mongodb
    。因此,调用
    mount
    不会显示该路径,而是显示
    /mnt/disks/
    路径。