GFS 分布式文件系统
一、概述
1.1 GlusterFS简介
- 开源的分布式文件系统
- 由存储服务器,客户端以及NFS/Samba存储网关组成
- 无元数据服务器
1.2 GlusterFS特点
- 扩展性和高性能
- 高可用性
- 全局统一命名空间
- 弹性卷管理
- 基于标准协议
1.3 GlusterFS术语
- Brick(存储块):指可信主机池中由主机提供的用于物理存储的专用分区,是GlusterFS中的基本存储单元,同时也是可信存储池中服务器上对外提供的存储目录
- Volume(逻辑卷):有一个逻辑卷是一组Brick的集合。卷是数据存储的逻辑设备,类似于LVM中的逻辑卷。大部分GlusterFS管理操作是在卷上进行的
-FUSE(Filesystem inUserspace):是一个内核模块,允许用户创建自己的文件系统,无需修改内核代码 - VFS:内核空间对用户空间提供的访问磁盘的接口
- Glusterd(后台管理进程):在存储群集中的每个节点上都要运行
- Volume(逻辑卷):有一个逻辑卷是一组Brick的集合。卷是数据存储的逻辑设备,类似于LVM中的逻辑卷。大部分GlusterFS管理操作是在卷上进行的
1.4 模块化堆栈式架构
- 模块化、堆栈式的架构
- 通过对模块的组合,实现复杂的功能
二、GlusterFS工作原理
2.1 GlusterFs工作流程
- 1)在客户端,用户通过GlusterFS的mount point来读写数据,对于用户来说,集群系统的存在对用户是完全透明的,用户感觉不到是操作本地系统还是远端的集群系统
- 2)用户的这个操作被递交给本地linux系统的VFS来处理
- 3)VFS将数据递交给FUSE内核文件系统:在启动GlusterFS客户端以前,需要向系统注册一个文件系统,文件系统是将数据通过/dev/fuse这个设备文件递交给客户端,所以,我们可以将文件系统理解为一个代理
- 4)数据被文件系统递交给客户端之后,client对数据进行一些指定的处理
- 5)在客户端处理末端,通过网络将数据递交给服务器后,并且将数据写入到服务器所控制的存储设备上
2.2 弹性HASH算法
- 通过HASH算法得到一个32位的整数
- 划分为N个连续的子空间,每个空间对应一个Brick
- 弹性HASH算法的优点
- 保证数据平均分布在每一个Brick
- 解决了对元数据服务器的依赖,进而解决了单点故障
三、GlusterFS的卷类型
3.1 分布式卷
- 没有对文件进行分块处理
- 通过扩展文件属性保存HASH值
- 支持的底层文件系统有EXT3,EXT4,ZFS,XFS等
- 没有分块处理,文件只能存储在一个server中,效率不提升
- 分布式卷的特点
- 文件分布在不同的服务器,不具备冗余性
- 更容易和廉价的扩展卷的大小
- 单点故障会造成数据丢失
- 依赖底层的数据保护
- 创建分布式卷
## 创建一个名为dis-volume的分布式卷,文件将根据HASH分布在server1:/dir1、server2:/dir2和server3:/dir3中
gluster volume create dis-volume server1:/dir1 server2:/dir2 server3:/dir3
3.2 条带卷
- 根据偏移量将文件分成N块(N个条带卷节点)、轮询的存储在每个Brick Server节点
- 存储大文件时,性能尤为突出
- 不具备冗余性,类似Raid0
- 从多个server中同时读取文件,效率提升
- 条带卷特点
- 数据被分割成更小快分布到块服务器群集中的不同条带区
- 分布减少了负载且更小的文件加速了存取的速度
- 没有数据冗余
- 创建条带卷
## 创建了一个名为stripe-volume的条带卷,文件将被分块轮询的存储在server1:/dir1、server2:/dir2中
gluster volume create stripe-volume stripe 2 transport tcp server1:/dir1 server:/dir2
3.3 复制卷
- 同一文件保存一份或多份副本
- 因为要保存副本,所以磁盘利用率较低
- 若多个节点上的存储空间不一致,将按照木桶效应取最低节点的容量作为该卷的总容量
- 复制卷的特点
- 卷中所有的服务器均保存一个完整的副本
- 卷的副本数量可以由用户创建的时候决定
- 至少有两个块服务器或者更多服务器
- 具备冗余性
## 创建名为rep-volume的复制卷,文件将同时存储两个副本,分别在server1:/dir1、server2:/dir2两个Brick中
gluster volume create rep-volume replica 2 transport tcp server1:/dir1 server:/dir2
3.4 分布式条带卷
- 兼顾分布式卷和条带卷的功能
- 只要勇于大文件访问处理
- 至少最少需要4台服务器
- 创建分布式条带卷
gluster volume create dis-stripe stripe 2 server1:/dir1 server2:/dir2 server3:/dir3 server4:/dir4
3.5 分布式复制卷
- 兼顾分布式卷和复制卷的功能
- 用于需要冗余的情况
- 创建分布式复制卷
gluster volume create dis-replica replica 2 server1:/dir1 server2:/dir2 server3:/dir3 server4:/dir4
四、实验部署
4.1 实验环境
node1:192.168.10.10
node2:192.168.10.20
node3:192.168.10.30
node4:192.168.10.40
client:192.168.10.50
4.2 在四台node节点上添加映射
vi /etc/hosts
192.168.10.10 node1
192.168.10.20 node2
192.168.10.30 node3
192.168.10.40 node4
4.3 所有node节点安装glusterfs相应软件,启动服务并设置时间同步
[root@node1 ~]# unzip gfsrepo.zip
[root@node1 ~]# yum clean all
[root@node1 ~]# yum makecache
[root@node1 ~]# vi /etc/yum.repos.d/gfs.repo
[gfs]
name=gfs
baseurl=file:///root/gfsrepo
gpgcheck=0
enabled=1
[root@node1 ~]# yum -y install glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma
[root@node1 ~]# systemctl start glusterd
[root@node1 ~]# systemctl enable glusterd
[root@node1 ~]# systemctl status glusterd
[root@node1 ~]# ntpdate ntp1.aliyun.com
4.4 选择任意节点构建存储池
[root@node1 ~]# gluster peer probe node2
peer probe: success.
[root@node1 ~]# gluster peer probe node3
peer probe: success.
[root@node1 ~]# gluster peer probe node4
peer probe: success.
[root@node1 ~]# gluster peer status ## 查看节点关系
4.5 所有node节点上都添加四块硬盘并完成挂载
init6 ## 重启
[root@node1 ~]# fdisk -l
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b00a4
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 125829119 61864960 8e Linux LVM
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/sdd:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/sdc:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/sde:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
## 所有节点上的硬盘格式化
[root@node1 ~]# mkfs.ext4 /dev/sdb
[root@node1 ~]# mkfs.ext4 /dev/sdc
[root@node1 ~]# mkfs.ext4 /dev/sdd
[root@node1 ~]# mkfs.ext4 /dev/sde
## 所有节点上创建挂载点并挂载
[root@node1 ~]# mkdir {/b1,/c1,/d1,/e1}
[root@node1 ~]# mount /dev/sdb /b1
[root@node1 ~]# mount /dev/sdc /c1
[root@node1 ~]# mount /dev/sdd /d1
[root@node1 ~]# mount /dev/sde /e1
[root@node1 ~]# df -Th
/dev/sdb ext4 20G 45M 19G 1% /b1
/dev/sdc ext4 20G 45M 19G 1% /c1
/dev/sdd ext4 20G 45M 19G 1% /d1
/dev/sde ext4 20G 45M 19G 1% /e1
4.6 客户机配置
## 添加地址映射
vi /etc/hosts
192.168.10.10 node1
192.168.10.20 node2
192.168.10.30 node3
192.168.10.40 node4
[root@client ~]# unzip gfsrepo.zip
[root@client ~]# yum clean all
[root@client ~]# yum makecache
[root@client ~]# vi /etc/yum.repos.d/gfs.repo
[gfs]
name=gfs
baseurl=file:///root/gfsrepo
gpgcheck=0
enabled=1
[root@client ~]# yum -y install glusterfs glusterfs-fuse
[root@client ~]# dd if=/dev/zero of=/test1.txt bs=1M count=40
[root@client ~]# dd if=/dev/zero of=/test2.txt bs=1M count=40
[root@client ~]# dd if=/dev/zero of=/test3.txt bs=1M count=40
[root@client ~]# dd if=/dev/zero of=/test4.txt bs=1M count=40
[root@client ~]# dd if=/dev/zero of=/test5.txt bs=1M count=40
4.7 创建卷
4.7.1 创建分布式卷
[root@node1 ~]# gluster volume create dis-vol node1:/b1 node2:/b1 force
volume create: dis-vol: success: please start the volume to access data
[root@node1 ~]# gluster volume info dis-vol
Volume Name: dis-vol
Type: Distribute
Volume ID: 215c49ee-4695-4720-94f4-e9c3d70ce513
Status: Created
Snapshot Count: 0
Number of Bricks: 2
Transport-type: tcp
Bricks:
Brick1: node1:/b1
Brick2: node2:/b1
Options Reconfigured:
transport.address-family: inet
nfs.disable: on
[root@node1 ~]# gluster volume start dis-vol
volume start: dis-vol: success
[root@node1 ~]# gluster volume info dis-vol
Volume Name: dis-vol
Type: Distribute
Volume ID: 215c49ee-4695-4720-94f4-e9c3d70ce513
Status: Started
Snapshot Count: 0
Number of Bricks: 2
Transport-type: tcp
Bricks:
Brick1: node1:/b1
Brick2: node2:/b1
Options Reconfigured:
transport.address-family: inet
nfs.disable: on
### client端:客户端挂载分布式卷到/test/dis/下,将创建的五个测试文件拷贝到/test/dis/下
[root@client ~]# mkdir -p /test/dis
[root@client ~]# mount.glusterfs node1:dis-vol /test/dis/
[root@client ~]# cp /test* /test/dis/
### node1:查看发布状态
[root@node1 ~]# ll -h /b1
总用量 121M
drwx------ 2 root root 16K 1月 6 11:26 lost+found
-rw-r--r-- 2 root root 40M 1月 6 11:53 test1.txt
-rw-r--r-- 2 root root 40M 1月 6 11:53 test3.txt
-rw-r--r-- 2 root root 40M 1月 6 11:53 test4.txt
### node2:
[root@node2 ~]# ll -h /b1
总用量 81M
drwx------ 2 root root 16K 1月 6 11:26 lost+found
-rw-r--r-- 2 root root 40M 1月 6 11:53 test2.txt
-rw-r--r-- 2 root root 40M 1月 6 11:53 test5.txt
4.7.2 创建复制卷
[root@node1 ~]# gluster volume create rep-vol replica 2 node3:/b1 node4:/b1 force
volume create: rep-vol: success: please start the volume to access data
[root@node1 ~]# gluster volume start rep-vol
volume start: rep-vol: success
[root@node1 ~]# gluster volume status rep-vol
Status of volume: rep-vol
Gluster process TCP Port RDMA Port Online Pid
------------------------------------------------------------------------------
Brick node3:/b1 49152 0 Y 14160
Brick node4:/b1 49152 0 Y 3215
client端:
[root@client ~]# mkdir /test/rep-vol
[root@client ~]# cp /test* /test/rep-vol/
[root@client ~]# mount.glusterfs node1:rep-vol /test/rep-vol/
node3:
[root@node3 ~]# ll -h /b1
总用量 201M
drwx------ 2 root root 16K 1月 6 11:26 lost+found
-rw-r--r-- 2 root root 40M 1月 6 12:07 test1.txt
-rw-r--r-- 2 root root 40M 1月 6 12:07 test2.txt
-rw-r--r-- 2 root root 40M 1月 6 12:07 test3.txt
-rw-r--r-- 2 root root 40M 1月 6 12:07 test4.txt
-rw-r--r-- 2 root root 40M 1月 6 12:07 test5.txt
node4:
[root@node4 ~]# ll -h /b1
总用量 201M
drwx------. 2 root root 16K 1月 6 11:27 lost+found
-rw-r--r--. 2 root root 40M 1月 6 12:07 test1.txt
-rw-r--r--. 2 root root 40M 1月 6 12:07 test2.txt
-rw-r--r--. 2 root root 40M 1月 6 12:07 test3.txt
-rw-r--r--. 2 root root 40M 1月 6 12:07 test4.txt
-rw-r--r--. 2 root root 40M 1月 6 12:07 test5.txt
4.7.3 创建分布式复制卷
[root@node1 ~]# gluster volume create dis-rep replica 2 node1:/e1 node2:/e1 node3:/
e1 node4:/e1 force
volume create: dis-rep: success: please start the volume to access data
[root@node1 ~]# gluster volume start dis-rep
volume start: dis-rep: success
[root@node1 ~]# gluster volume status dis-rep
Status of volume: dis-rep
Gluster process TCP Port RDMA Port Online Pid
------------------------------------------------------------------------------
Brick node1:/e1 49153 0 Y 14889
Brick node2:/e1 49153 0 Y 14299
Brick node3:/e1 49153 0 Y 14273
Brick node4:/e1 49153 0 Y 3340
client端:
[root@client ~]# mkdir /test/dis-rep
[root@client ~]# cp /test* /test/dis-rep/
[root@client ~]# mount.glusterfs node1:dis-rep /test/dis-rep/
node1和node2:
[root@node1 ~]# ll -h /e1
总用量 121M
drwx------ 2 root root 16K 1月 6 11:27 lost+found
-rw-r--r-- 2 root root 40M 1月 6 12:13 test1.txt
-rw-r--r-- 2 root root 40M 1月 6 12:13 test3.txt
-rw-r--r-- 2 root root 40M 1月 6 12:13 test4.txt
node3和node4:
[root@node3 ~]# ll -h /e1
总用量 81M
drwx------ 2 root root 16K 1月 6 11:27 lost+found
-rw-r--r-- 2 root root 40M 1月 6 12:13 test2.txt
-rw-r--r-- 2 root root 40M 1月 6 12:13 test5.txt
4.8 破坏测试
[root@client ~]# ll -h /test/dis/
总用量 121M
drwx------. 2 root root 4.0K 1月 6 11:26 lost+found
-rw-r--r--. 1 root root 40M 1月 6 11:53 test1.txt
-rw-r--r--. 1 root root 40M 1月 6 11:53 test3.txt
-rw-r--r--. 1 root root 40M 1月 6 11:53 test4.txt
[root@client ~]# ll -h /test/dis-rep/
总用量 201M
drwx------. 2 root root 4.0K 1月 6 11:27 lost+found
-rw-r--r--. 1 root root 40M 1月 6 12:13 test1.txt
-rw-r--r--. 1 root root 40M 1月 6 12:13 test2.txt
-rw-r--r--. 1 root root 40M 1月 6 12:13 test3.txt
-rw-r--r--. 1 root root 40M 1月 6 12:13 test4.txt
-rw-r--r--. 1 root root 40M 1月 6 12:13 test5.txt
[root@client ~]# ll -h /test/rep-vol/
总用量 201M
drwx------. 2 root root 4.0K 1月 6 11:27 lost+found
-rw-r--r--. 1 root root 40M 1月 6 12:07 test1.txt
-rw-r--r--. 1 root root 40M 1月 6 12:07 test2.txt
-rw-r--r--. 1 root root 40M 1月 6 12:07 test3.txt
-rw-r--r--. 1 root root 40M 1月 6 12:07 test4.txt
-rw-r--r--. 1 root root 40M 1月 6 12:07 test5.txt
4.9 访问控制
## 拒绝某个用户
[root@node1 ~]# gluster volume set dis-rep auth.reject 192.168.10.50
volume set: success
## 允许某个用户
[root@node1 ~]# gluster volume set dis-rep auth.allow 192.168.10.50
volume set: success