Mysql 容器无法将数据装载到 nfs 文件夹

Mysql container can not mount data to a nfs folder

通过swarm模式,容器可以部署在任何加入的节点中。我在 host1 上创建了一个共享的 nfs 文件夹作为 mysql 数据文件夹。

mkdir -p /nfs/data-volume

在另一个host2中,它挂载到这个共享文件夹。并添加了必要的权限。我通过在其中读取和写入一些文本文件来尝试这个 nfs 共享文件夹。它运作良好。 (没有权限错误) 在这些 nfs 配置之后,我这样定义了我的容器卷;

mysqldb-read:
    image: demo/db-slave
    ports:
     - "3308:3306"
    volumes:
     - /nfs/data-volume:/var/lib/mysql

结果是: 如果 mysql 容器 运行 在 host1 上,效果很好。 如果主机 2 上的 mysql 容器 运行,它不会启动。但是容器并没有退出,线程停留在那里,看起来像是在等待什么。 通过运行ning查看日志命令:

docker logs -f mymysql

它显示这样的日志:

   2017-06-07T02:40:13.627195Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-06-07T02:40:13.632313Z 0 [Note] mysqld (mysqld 5.7.18-log) starting as process 52 ...
2017-06-07T02:40:13.648010Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-06-07T02:40:13.648054Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-06-07T02:40:13.648059Z 0 [Note] InnoDB: Uses event mutexes
2017-06-07T02:40:13.648062Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-06-07T02:40:13.648066Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-06-07T02:40:13.648069Z 0 [Note] InnoDB: Using Linux native AIO
2017-06-07T02:40:13.648326Z 0 [Note] InnoDB: Number of pools: 1
2017-06-07T02:40:13.648770Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-06-07T02:40:13.651011Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-06-07T02:40:13.760444Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-06-07T02:40:13.829981Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().

此日志没有更多内容,它在这一行停止。 我尝试登录到容器,并输入命令

mysqld -uroot -proot

显示日志完全一样。

感觉是nfs造成的。但是我google了一下,发现几乎所有的资料都建议使用nfs来共享数据。有没有人成功地完成了这项工作?或者对我有什么建议?

谢谢

Q1: 有没有人成功完成这个工作?

我的经验是……没有。几个月前我尝试了 NFS、MySQL 和 Docker Swarm (v1.12),但我也失败了。

他们确实很清楚,来自 MySQL documentation:

Using NFS with MySQL

Caution is advised when considering using NFS with MySQL. Potential issues, which vary by operating system and NFS version, include:

  • MySQL data and log files placed on NFS volumes becoming locked and unavailable for use...
  • Data inconsistencies...
  • Maximum file size limitations

我也经历过file locks,查询慢,写慢...

Q2:或者对我有什么建议吗?

docker-swarm 棘手的部分之一确实是数据,尤其是数据库。您不知道 主机上的 mysql 容器将是 运行。我使用了两种替代方案,所以克服了这个问题:

1. Swarm 模式服务创建 --constraint 选项

此选项将指示 docker 始终在同一主机上部署您的 MySQL 容器,例如:

mysqldb-read:
  image: demo/db-slave
  ports:
    - "3308:3306"
  volumes:
    - /nfs/data-volume:/var/lib/mysql
  deploy:
    placement:
      constraints: [node.hostname == host1]

如果 docker 群服务 mysqldb-read 重新启动,这将始终在 host1 节点上。

2。 Docker 卷

另一种选择是dynamically attach a shared docker volume to the MySQL service before startup. The documentation states:

If you want your data to persist, use a named volume and a volume driver that is multi-host aware, so that the data is accessible from any node...

在 AWS 环境中有一些 docker volume plugins that allow you to do that. I personally tried rancher's convoy,但我还有卷删除、同步等其他问题...

你也可以看看

PS:关于 NFS

我并不是说你应该放弃 NFS 用于其他 docker 服务,我仍然将它用于 read-only 配置文件(Apache Tomcat 和 Nginx 配置等。 ..),但对于 MySQL 这是不行的。

希望我的经验对您有所帮助!