NFS结合docker存储技术

本文介绍如何利用NFS实现跨多台宿主机的Docker容器间共享存储。通过配置NFS服务器并挂载共享目录,使得不同宿主机上的Docker容器能够访问相同的文件系统,从而实现高效的数据共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NFS 结合docker 技术:

使用NFS作为存储共享服务器:

这是大型的共享存储技术:

 

 

创建一台主机:

搭建NFS服务器:

[root@room9pc01 ~]# cd /var/lib/libvirt/images/

[root@room9pc01 images]# qemu-img  create  -b king.img   -f qcow2 coke3.img 32G

[root@room9pc01 images]# cd /etc/libvirt/qemu/

[root@room9pc01 qemu]# sed 's/node/coke3/'  node.xml   > /etc/libvirt/qemu/coke3.xml

[root@room9pc01 qemu]# virsh define  /etc/libvirt/qemu/coke3.xml

定义域 coke3(从 /etc/libvirt/qemu/coke3.xml)

[root@room9pc01 qemu]# virsh start coke3

域 coke3 已开始

[root@room9pc01 qemu]# virsh console coke3

连接到域 coke3

换码符为 ^]

vim  /etc/sysconfig/network-scripts/ifcfg-eth0

# Generated by dracut initrd

DEVICE="eth0"

ONBOOT="yes"

NM_CONTROLLED="no"

TYPE="Ethernet"

BOOTPROTO="none"

IPADDR="192.168.1.10"

PREFIX="24"

GATEWAY="192.168.1.254"

:wq

 

[root@localhost ~]# systemctl  restart  network

[root@localhost ~]# ip a s

 

[root@docker3 ~]# yum -y install  nfs-utils

 

[root@docker3 ~]# mkdir  /var/webroot

[root@docker3 ~]# chmod  777  /var/webroot/

 

[root@docker3 ~]# vim  /etc/exports

 

[root@docker3 ~]# man exports

 

/var/webroot  *(rw,root_squash)

:wq

 

[root@docker3 ~]# systemctl   start nfs

 

[root@docker3 ~]# exportfs  -avr

exporting *:/var/webroot

[root@docker3 ~]# showmount  -e 192.168.1.10

Export list for 192.168.1.10:

/var/webroot *

 

 

doccke1服务器:

[root@coker1 ~]# yum -y install  nfs-utils

[root@coker1 ~]# mount -t nfs 192.168.1.10:/var/webroot  /mnt

[root@coker1 ~]# df -h

192.168.1.10:/var/webroot   32G  1.1G   31G    4% /mnt

[root@coker1 ~]# showmount  -e 192.168.1.10

Export list for 192.168.1.10:

/var/webroot *

 

查看注册信息:

[root@coker1 ~]# rpcinfo  192.168.1.10

 

启动容器:

[root@coker1 ~]# docker run -itd -v /mnt:/var/www/html  192.168.1.50:5000/myos:httpd

efc02541fbaa6913255e0f48bcb8de06fa42cf587389b720aa314b12c770b915

 

 

使用容器:

[root@coker1 ~]# docker ps

[root@coker1 ~]# docker inspect  ef

[root@coker1 ~]# curl -i  http://172.17.0.4  | head

 

 

 

 

docker2服务器:

[root@coker2 ~]# yum -y install  nfs-utils.x86_64 

[root@coker2 ~]# mount -t nfs  192.168.1.10:/var/webroot  /mnt

[root@coker2 ~]# df -h 

192.168.1.10:/var/webroot   32G  1.1G   31G    4% /mnt

[root@coker2 ~]#  showmount  -e 192.168.1.10

Export list for 192.168.1.10:

/var/webroot *

[root@coker2 ~]# rpcinfo  192.168.1.10

 

启动容器:

[root@coker2 ~]# docker run -itd  -v /mnt:/var/www/html  192.168.1.50:5000/myos:httpd

 

使用容器:

[root@coker2 ~]# docker ps

 

[root@coker2 ~]# docker exec  -it 3e8  /bin/bash

 

[root@3e8eb3f8c76c html]# cd  /var/www/html/

 

[root@3e8eb3f8c76c html]# echo haha  > index.html

bash: index.html: Permission denied

是因为共享目录,没有给与权限

 

[root@3e8eb3f8c76c html]# echo haha  > index.html

 

再次回到docker1 查看共享效果:

[root@coker1 ~]# curl -i  http://172.17.0.4  | head

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100     5  100     5    0     0   2608      0 --:--:-- --:--:-- --:--:--  5000

HTTP/1.1 200 OK

Date: Wed, 22 Aug 2018 07:39:03 GMT

Server: Apache/2.4.6 (CentOS)

Last-Modified: Wed, 22 Aug 2018 07:36:19 GMT

ETag: "5-5740132cf00a7"

Accept-Ranges: bytes

Content-Length: 5

Content-Type: text/html; charset=UTF-8

 

haha

 

这就是多个宿主机的共享:通过共享目录把数据共享给所有的宿主机上:

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维螺丝钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值