glusterfs 搭建
环境准备
服务器:
序号 | 服务器IP | 系统 | 主机名 | 系统盘 | 数据盘 |
---|---|---|---|---|---|
1 | 192.168.9.136 | centos7.4 | glusterfs-01 | 20G | 100G |
2 | 192.168.9.235 | centos7.4 | glusterfs-02 | 20G | 100G |
3 | 192.168.9.166 | centos7.4 | glusterfs-03 | 20G | 100G |
注意:
①、至少拥有三个节点;
②、每个虚拟机上至少有两个虚拟磁盘,一个用于操作系统安装,一个用于服务GlusterFS存储(sdb),需要将GlusterFS存储与OS安装分开;
③、在每台服务器上设置NTP,以使文件系统程序正常运行;
④、GlusterFS将其动态生成的配置文件存储在/var/lib/glusterd;
⑤、本次测试的briks是在/data/glusterfs目录;
⑥、请提前关闭selinux和firewalld。
配置 hosts
cat >> /etc/hosts <<-EOF
192.168.9.136 glusterfs-01
192.168.9.235 glusterfs-02
192.168.9.166 glusterfs-03
EOF
注意:在所有的server节点及client节点都要配置。
安装
在三个节点都安装glusterfs
# yum install centos-release-gluster -y
# yum install -y glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma -y
启动
# systemctl start glusterd
# systemctl is-active glusterd
# systemctl enable glusterd
配置
将节点都加入集群
直接在glusterfs-01机器上执行:
# gluster peer probe glusterfs-01
# gluster peer probe glusterfs-02
# gluster peer probe glusterfs-03
查看集群状态
# gluster peer status
Number of Peers: 2
Hostname: glusterfs-02
Uuid: 525d16fd-5ba6-48d6-97ab-4fb8e5c93fda
State: Peer in Cluster (Connected)
Hostname: glusterfs-03
Uuid: 49482e13-6f45-4056-9e85-ae5178f5ab2a
State: Peer in Cluster (Connected)
创建gluster卷(所有的节点都创建):
# mkdir -pv /data/glusterfs/gv0
查看volume状态
# gluster volume info
No volumes present
glusterfs卷类型
- Distributed:分布式卷,文件通过 hash 算法随机分布到由 bricks 组成的卷上。

- Replicated: 复制式卷,类似 RAID 1,replica 数必须等于 volume 中 brick 所包含的存储服务器数,可用性高。

- Dispersed: 分散卷,分散卷基于纠错码,将文件编码后条带化分散存储在卷的多个块中,并提供一定冗余性。分散卷可以提高磁盘存储利用率,但性能有所下降。分散卷中的冗余值表示允许多少块失效而不中断对卷的读写操作;分散卷中的冗余值必须大于0,总块数应当大于2倍的冗余值,也就意味着分散卷至少要由3个块组成。在创建分散卷时如果未指定冗余值,系统将自动计算该值并提示。
注意:
分散卷可用存储空间计算公式如下:<Usable size> = <Brick size> * (#Bricks - Redundancy)
- Distributed Replicated: 分布式的复制卷,volume 中 brick 所包含的存储服务器数必须是 replica 的倍数(>=2倍),兼顾分布式和复制式的功能。

- Distributed Striped: 分布式的条带卷,volume中 brick 所包含的存储服务器数必须是 stripe 的倍数(>=2倍),兼顾分布式和条带式的功能。
创建GlusterFS磁盘:
# gluster volume create StorageClass transport rdma glusterfs-01:/data/glusterfs/gv0 glusterfs-02:/data/glusterfs/gv0 glusterfs-03:/data/glusterfs/gv0 force
volume create: StorageClass: success: please start the volume to access data
# gluster volume info
Volume Name: StorageClass
Type: Distribute
Volume ID: df77f5e0-473f-4eda-ace1-3cd1a4148aba
Status: Created
Snapshot Count: 0
Number of Bricks: 3
Transport-type: rdma
Bricks:
Brick1: glusterfs-01:/data/glusterfs/gv0
Brick2: glusterfs-02:/data/glusterfs/gv0
Brick3: glusterfs-03:/data/glusterfs/gv0
Options Reconfigured:
storage.fips-mode-rchecksum: on
nfs.disable: on
# gluster pool list
UUID Hostname State
876be8d5-8ecd-4a1c-bc8f-e9d0f1f377c0 glusterfs-02 Connected
1345eb1f-4b94-4c4d-a0ff-d25f0628a57c glusterfs-03 Connected
385dc353-191a-4b88-b60c-4644fdb699c1 localhost Connected
启动glusterfs存储卷
# gluster volume start StorageClass
volume start: StorageClass: success
调优
开启 指定 volume 的配额: (StorageClass为volume名称)
# gluster volume quota StorageClass enable
volume quota : success
限制 StorageClass 中 / (既总目录) 最大使用 100GB 空间
# gluster volume quota StorageClass limit-usage / 100GB
volume quota : success
设置 cache 4GB
# gluster volume set StorageClass performance.cache-size 4GB
volume set: success
开启 异步 , 后台操作
# gluster volume set StorageClass performance.flush-behind on
volume set: success
设置 io 线程 16
# gluster volume set StorageClass performance.io-thread-count 16
volume set: success
设置 回写 (写数据时间,先写入缓存内,再写入硬盘)
# gluster volume set StorageClass performance.write-behind on
volume set: success
部署GlusterFS客户端并mount GlusterFS文件系统
client机器(需要使用glusterfs分布式文件系统的机器):
192.168.9.27
192.168.9.28
192.168.9.29
在上面3台client机器上安装客户端并挂载(注意:客户端同样需要将gluster的解析写入hosts文件中)
# yum install -y glusterfs glusterfs-fuse
创建挂载目录
# mkdir -pv /opt/glusterfs
挂载glusterfs文件系统
[root@k8s-master-01 glusterfs]# mount -t glusterfs glusterfs-01:StorageClass /opt/glusterfs
[root@k8s-master-01 glusterfs]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 200G 63G 137G 32% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 852M 7.0G 11% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 497M 128M 370M 26% /boot
overlay 200G 63G 137G 32% /var/lib/docker/overlay2/
glusterfs-01:StorageClass 100G 0 100G 0% /opt/glusterfs
测试
# for i in `seq -w 1 100`; do cp -rp /var/log/messages /opt/glusterfs/copy-test-$i; done
首先,检查客户端安装点:
# ls -lA /opt/glusterfs/copy* | wc -l
您应该看到返回了100个文件。接下来,检查每个服务器上的GlusterFS块安装点:
# ls -lA /data/glusterfs/gv0/copy*
使用我们在此处列出的方法,您应该在每个服务器上看到100个文件。如果没有复制,则在仅分发的卷(此处未详细介绍)中,您应该在每个卷上看到大约33个文件。
其他维护明命令:
1. 查看GlusterFS中所有的volume:
# gluster volume list
2. 删除GlusterFS磁盘:
# gluster volume stop StorageClass #停止名字为 StorageClass 的磁盘
# gluster volume delete StorageClass #删除名字为 StorageClass 的磁盘
注: 删除 磁盘 以后,必须删除 磁盘( /data/glusterfs ) 中的 ( .glusterfs/ .trashcan/ )目录。
否则创建新 volume 相同的 磁盘 会出现文件 不分布,或者 类型 错乱 的问题。
3. 卸载某个节点GlusterFS磁盘
# gluster peer detach glusterfs-02
4. 设置访问限制,按照每个volume 来限制
# gluster volume set StorageClass auth.allow 10.6.0.*,10.7.0.*
5. 添加GlusterFS节点:
# gluster peer probe glusterfs-04
# gluster volume add-brick StorageClass glusterfs-04:/data/glusterfs
注:如果是复制卷或者条带卷,则每次添加的Brick数必须是replica或者stripe的整数倍
6. 配置卷
# gluster volume set
7. 缩容volume:
先将数据迁移到其它可用的Brick,迁移结束后才将该Brick移除:
# gluster volume remove-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs start
在执行了start之后,可以使用status命令查看移除进度:
# gluster volume remove-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs status
不进行数据迁移,直接删除该Brick:
# gluster volume remove-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs commit
注意,如果是复制卷或者条带卷,则每次移除的Brick数必须是replica或者stripe的整数倍。
扩容:
# gluster volume add-brick StorageClass glusterfs-04:/data/glusterfs
8. 修复命令:
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs commit -force
9. 迁移volume:
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs start
pause 为暂停迁移
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs pause
abort 为终止迁移
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs abort
status 查看迁移状态
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs status
迁移结束后使用commit 来生效
# gluster volume replace-brick StorageClass glusterfs-01:/data/glusterfs glusterfs-02:/data/glusterfs commit
10. 均衡volume:
# gluster volume StorageClass lay-outstart
# gluster volume StorageClass start
# gluster volume StorageClass startforce
# gluster volume StorageClass status
# gluster volume StorageClass stop