如何使用 docker compose v3 在容器中直接挂载 NFS 共享/卷 [英] How to directly mount NFS share/volume in container using docker compose v3

查看:77
本文介绍了如何使用 docker compose v3 在容器中直接挂载 NFS 共享/卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 v3 的撰写文件,其中有 3 个服务共享/使用相同的卷.在使用 swarm 模式时,我们需要创建额外的容器 &卷来管理我们整个集群的服务.

I have a compose file with v3 where there are 3 services sharing/using the same volume. While using swarm mode we need to create extra containers & volumes to manage our services across the cluster.

我计划使用 NFS 服务器,以便单个 NFS 共享将直接挂载到集群内的所有主机上.

I am planning to use NFS server so that single NFS share will get mounted directly on all the hosts within the cluster.

我发现了以下两种方法,但需要在 docker 主机上执行额外的步骤 -

I have found below two ways of doing it but it needs extra steps to be performed on the docker host -

  • 在主机上使用fstab"或mount"命令挂载 NFS 共享 &然后将其用作 docker 服务的主机卷.

  • Mount the NFS share using "fstab" or "mount" command on the host & then use it as a host volume for docker services.

使用 Netshare 插件 - https://github.com/ContainX/docker-volume-网络共享

Use Netshare plugin - https://github.com/ContainX/docker-volume-netshare

是否有一种标准方法可以让我通过在 docker 主机上仅执行很少/没有步骤(我知道无论如何都需要nfs-common"包)来使用 docker compose v3 直接使用/挂载 NFS 共享?

Is there a standard way where i can directly use/mount NFS share using docker compose v3 by performing only few/no steps(I understand that "nfs-common" package is required anyhow) on the docker host?

推荐答案

在发现大量未记录后,这里是使用 stack 和 docker compose 挂载 NFS 卷的正确方法.

After discovering that this is massively undocumented,here's the correct way to mount a NFS volume using stack and docker compose.

最重要的是您需要使用 version: "3.2" 或更高版本.如果不这样做,您将遇到奇怪且不明显的错误.

The most important thing is that you need to be using version: "3.2" or higher. You will have strange and un-obvious errors if you don't.

第二个问题是卷在定义更改时不会自动更新.这可能会导致您误以为您的更改不正确,因为它们还没有被应用.确保在任何可能存在的地方 docker rm VOLUMENAME,就好像该卷存在一样,它不会被验证.

The second issue is that volumes are not automatically updated when their definition changes. This can lead you down a rabbit hole of thinking that your changes aren't correct, when they just haven't been applied. Make sure you docker rm VOLUMENAME everywhere it could possibly be, as if the volume exists, it won't be validated.

第三个问题更多是 NFS 问题 - 如果 NFS 文件夹不存在,将在服务器上不会创建.这就是 NFS 的工作方式.在你做任何事情之前,你需要确保它存在.

The third issue is more of a NFS issue - The NFS folder will not be created on the server if it doesn't exist. This is just the way NFS works. You need to make sure it exists before you do anything.

(除非您确定自己知道自己在做什么,否则不要删除soft"和nolock"——如果您的 NFS 服务器消失,这会阻止 docker 冻结)

(Don't remove 'soft' and 'nolock' unless you're sure you know what you're doing - this stops docker from freezing if your NFS server goes away)

这是一个完整的例子:

[root@docker docker-mirror]# cat nfs-compose.yml
version: "3.2"

services:
  rsyslog:
    image: jumanjiman/rsyslog
    ports:
      - "514:514"
      - "514:514/udp"
    volumes:
      - type: volume
        source: example
        target: /nfs
        volume:
          nocopy: true
volumes:
  example:
    driver_opts:
      type: "nfs"
      o: "addr=10.40.0.199,nolock,soft,rw"
      device: ":/docker/example"



[root@docker docker-mirror]# docker stack deploy --with-registry-auth -c nfs-compose.yml rsyslog
Creating network rsyslog_default
Creating service rsyslog_rsyslog
[root@docker docker-mirror]# docker stack ps rsyslog
ID                  NAME                IMAGE                       NODE                DESIRED STATE       CURRENT STATE                     ERROR               PORTS
tb1dod43fe4c        rsyslog_rsyslog.1   jumanjiman/rsyslog:latest   swarm-4             Running             Starting less than a second ago
[root@docker docker-mirror]#

现在,在 swarm-4 上:

Now, on swarm-4:

root@swarm-4:~# docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS               NAMES
d883e0f14d3f        jumanjiman/rsyslog:latest   "rsyslogd -n -f /e..."   6 seconds ago       Up 5 seconds        514/tcp, 514/udp    rsyslog_rsyslog.1.tb1dod43fe4cy3j5vzsy7pgv5
root@swarm-4:~# docker exec -it d883e0f14d3f df -h /nfs
Filesystem                Size      Used Available Use% Mounted on
:/docker/example          7.2T      5.5T      1.7T  77% /nfs
root@swarm-4:~#

此卷将在堆栈运行的任何 swarm 节点上创建(但不会销毁).

This volume will be created (but not destroyed) on any swarm node that the stack is running on.

root@swarm-4:~# docker volume inspect rsyslog_example
[
    {
        "CreatedAt": "2017-09-29T13:53:59+10:00",
        "Driver": "local",
        "Labels": {
            "com.docker.stack.namespace": "rsyslog"
        },
        "Mountpoint": "/var/lib/docker/volumes/rsyslog_example/_data",
        "Name": "rsyslog_example",
        "Options": {
            "device": ":/docker/example",
            "o": "addr=10.40.0.199,nolock,soft,rw",
            "type": "nfs"
        },
        "Scope": "local"
    }
]
root@swarm-4:~#

这篇关于如何使用 docker compose v3 在容器中直接挂载 NFS 共享/卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆