在 Docker 中使用 JuiceFS

JuiceFS 可以通过 volume plugin 与 Docker 引擎集成。

安装插件

通过下面的命令安装插件,按照提示为 FUSE 提供必要的权限。

  1. $ docker plugin install juicedata/juicefs
  2. Plugin "juicedata/juicefs" is requesting the following privileges:
  3. - network: [host]
  4. - device: [/dev/fuse]
  5. - capabilities: [CAP_SYS_ADMIN]
  6. Do you grant the above permissions? [y/N] y
  7. latest: Pulling from juicedata/juicefs
  8. bb7fe456a3d7: Download complete
  9. Digest: sha256:e49817cdde62aa0596e5d61cbb805ee04c5921322ec696ddfc5e92d0ece4562f
  10. Status: Downloaded newer image for juicedata/juicefs:latest
  11. Installed plugin juicedata/juicefs

创建 Volume

创建新的 Volume 时需要提供 JuiceFS 文件系统名称、密钥以及对象存储的访问凭据,例如

  1. $ docker volume create -d juicedata/juicefs -o name=$JFS_NAME -o token=$JFS_TOKEN -o accesskey=$ACCESS_KEY -o secretkey=$SECRET_KEY jfsvolume

JFS_TOKEN 可以在 JuiceFS 控制中心的设置页面里找到,例如 https://juicefs.com/console/vol/<jfs_name>/setting

挂载 Volume

在 Docker 中挂载 Volume 的方法如下所示:

  1. $ docker run -it -v jfsvolume:/jfs busybox ls /jfs

删除 Volume

  1. $ docker volume remove jfsvolume

注意

  1. volume 必须处于未被挂载状态才能正常删除。

  2. 删除 volume 不影响对应的 JuiceFS 上的数据,只是从 Docker Engine 中移除

卸载插件

  1. $ docker plugin disable juicedata/juicefs
  2. $ docker plugin remove juicedata/juicefs

通过 docker-compose 挂载

下面是使用 docker-compose 挂载 JuiceFS 文件系统的例子

  1. version: '3'
  2. services:
  3. busybox:
  4. image: busybox
  5. command: "ls /jfs"
  6. volumes:
  7. - jfsvolume:/jfs
  8. volumes:
  9. jfsvolume:
  10. driver: juicedata/juicefs
  11. driver_opts:
  12. name: ${JFS_NAME}
  13. token: ${JFS_TOKEN}
  14. accesskey: ${ACCESS_KEY}
  15. secretkey: ${SECRET_KEY}

敏感信息可以通过环境变量传入。

启动服务:

  1. docker-compose up

关闭服务并从 Docker 中卸载 JuiceFS 文件系统:

  1. docker-compose down --volumes

在 Docker swarm 中使用

JuiceFS 文件系统支持多节点的 Docker swarm 集群。

首先确保 juicedata/juicefs 插件在 所有 工作节点上安装并启用。

JuiceFS 的挂载参数通过 --mount 指定,如下面的例子所示。

  1. docker service create --name nginx --mount \
  2. type=volume,volume-driver=juicedata/juicefs,source=jfsvolume,destination=/jfs,\
  3. volume-opt=name=$JFS_VOL,volume-opt=token=$JFS_TOKEN,volume-opt=accesskey=$ACCESS_KEY,volume-opt=secretkey=$SECRET_KEY nginx:alpine