Docker学习—Docker多机器数据共享-nfs

Docker多机器数据共享-nfs

实验流程

一、安装软件包

yum install -y nfs-utils

二、启动服务

systemctl start nfs

设置开机自启:
systemctl enable nfs

三、查看rpcbind的进程和端口

22:31:30[root@zjx520 ~]# ps aux | grep nfs
root       1587  0.0  0.0      0     0 ?        S<   21:28   0:00 [nfsd4_callbacks]
root       1593  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1594  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1595  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1596  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1597  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1598  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1599  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1600  0.0  0.0      0     0 ?        S    21:28   0:00 [nfsd]
root       1773  0.0  0.0 112812   976 pts/1    R+   22:32   0:00 grep --color=auto nfs
22:33:14[root@zjx520 ~]# ss -anplut | grep rpcbind
udp    UNCONN     0      0         *:891                   *:*                   users:(("rpcbind",pid=1565,fd=7))
udp    UNCONN     0      0         *:111                   *:*                   users:(("rpcbind",pid=1565,fd=6))
udp    UNCONN     0      0      [::]:891                [::]:*                   users:(("rpcbind",pid=1565,fd=10))
udp    UNCONN     0      0      [::]:111                [::]:*                   users:(("rpcbind",pid=1565,fd=9))
tcp    LISTEN     0      128       *:111                   *:*                   users:(("rpcbind",pid=1565,fd=8))
tcp    LISTEN     0      128    [::]:111                [::]:*                   users:(("rpcbind",pid=1565,fd=11))

nfsd进程将监听端口的活外包给了rpcbind进程

四、创建nfs共享目录

22:37:37[root@zjx520 /]# mkdir /nfs-web
22:41:17[root@zjx520 /]# cd nfs-web/
22:41:23[root@zjx520 /nfs-web]# echo "welcome zhangjiaxin" >index.html
22:41:44[root@zjx520 /nfs-web]# ll index.html 
-rw-r--r--. 1 root root 20 Mar  1 22:41 index.html
22:41:39[root@zjx520 /nfs-web]# cat index.html 
welcome zhangjiaxin

五、编辑/etc/exports文件 **修改配置文件需要重新加载配置(exportfs -av)

/web 192.168.220.0/24(rw,sync,all_squash)

/web 共享的目录的路径
192.168.220.0/24 允许能访问的机器的网段
(rw,sync,all_squash) 拥有的权限  rw 可以读写  sync 在host上修改的数据,里面同步到nfs服务器
all_squash 任何机器上的任何用户连接过来都看成一个普通的用户nobody对待

22:49:47[root@zjx520 /nfs-web]# exportfs -av
exporting 192.168.220.0/24:/web
让共享目录生效

六、设置共享目录权限

22:52:10[root@zjx520 /nfs-web]# ll -d /nfs-web/
drwxr-xr-x. 2 root root 24 Mar  1 22:41 /nfs-web/
22:52:21[root@zjx520 /nfs-web]# chown nfsnobody:nfsnobody /nfs-web/
22:52:44[root@zjx520 /nfs-web]# ll -d /nfs-web/
drwxr-xr-x. 2 nfsnobody nfsnobody 24 Mar  1 22:41 /nfs-web/

关闭防火墙
22:53:32[root@zjx520 /nfs-web]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
22:55:37[root@zjx520 /nfs-web]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
22:55:47[root@zjx520 /nfs-web]# getenforce
Enforcing

七、客户机准备

1、安装nfs
[root@docker-1 ~]# yum install -y nfs-utils
[root@docker-2 ~]# yum install -y nfs-utils

2、新建挂载目录,然后挂载
[root@docker-1 ~]# mkdir /nfs-web
[root@docker-2 ~]# mkdir /nfs-web

3、挂载
[root@docker-1 ~]# mount 192.168.220.104:/nfs-web /nfs-web
[root@docker-2 ~]# mount 192.168.220.104:/nfs-web /nfs-web

挂载成功:
[root@docker-1 ~]# df -Th
文件系统                 类型      容量  已用  可用 已用% 挂载点
devtmpfs                 devtmpfs  475M     0  475M    0% /dev
tmpfs                    tmpfs     487M     0  487M    0% /dev/shm
tmpfs                    tmpfs     487M   39M  448M    8% /run
tmpfs                    tmpfs     487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root  xfs        17G  9.2G  7.9G   55% /
/dev/sda1                xfs      1014M  137M  878M   14% /boot
tmpfs                    tmpfs      98M     0   98M    0% /run/user/0
overlay                  overlay    17G  9.2G  7.9G   55% /var/lib/docker/overlay2/0153b37c74f57c2a9564e6fc17be67b6b2b6eec43d4113c91d1e754f1fe5f376/merged
overlay                  overlay    17G  9.2G  7.9G   55% /var/lib/docker/overlay2/f4e6c2c496ffd3520a25c5ccade4398f0b70e1dc62526efd51277b93e595a3c6/merged
overlay                  overlay    17G  9.2G  7.9G   55% /var/lib/docker/overlay2/20f97a642c70e584f92ef0d0b0dcaf1fae4a49fa81920db68a58665086812630/merged
192.168.220.104:/nfs-web nfs4       19G  5.7G   13G   31% /nfs-web

5、查看是否挂载成功
[root@docker-1 ~]# cd /nfs-web/
[root@docker-1 nfs-web]# ll
总用量 4
-rw-r--r--. 1 root root 20 31 22:41 index.html
[root@docker-1 nfs-web]# cat index.html 
welcome zhangjiaxin

八、挂载卷到容器中

[root@docker-1 nfs-web]# docker run -d -p 8090:80 --name zjx-nginx-1 -v /nfs-web/:/usr/share/nginx/html nginx
1a3bbdd00e0912616e113e0d593564a512b2a0269ea0e554b917a147b0013774

九、创建卷来映射

# 创建一个映射到192.168.220.104下/nfs/web的nfs-zjx-1的卷
[root@docker-2 nfs-web]# docker volume create --driver local --opt type=nfs --opt o=addr=192.168.220.104,nolock,soft,rw,sync --opt device=:/nfs-web nfs-zjx-1
nfs-zjx-1
[root@docker-2 nfs-web]# docker volume ls
DRIVER    VOLUME NAME
local     9fc5137ed617c16dcc3781c08d6436412bd38a01139812da243d608215047b8b
local     72d4768da9725ff94d91fcc97c147d1cb0c95ccaedae24aa0e328fe5de461afd
local     599d932ea29206ce0d309fa5df3515d85f8bbdd13e5359382629b39afcfb866f
local     nfs-zjx-1

# 创建一个zjx-nginx-2容器使用nfs-zjx-1卷
[root@docker-2 nfs-web]# docker run -d -p 8091:80 --name zjx-nginx-2 -v nfs-zjx-1:/usr/share/nginx/html nginx
d87deb146abb77a6000c37a61fee87140f25818b7552ca975b03425aed1735c5
root@docker-2 nfs-web]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
d87deb146abb   nginx     "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   0.0.0.0:8091->80/tcp, :::8091->80/tcp   zjx-nginx-2
5805fc764800   nginx     "/docker-entrypoint.…"   10 hours ago    Up 5 minutes   0.0.0.0:8090->80/tcp, :::8090->80/tcp   zjx-nginx-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值