最近在学习分布式存储,用到了开源工具GlusterFS,就写点东西吧。
新手上路,多多指教-
1.准备工作
操作系统 | IP | 主机名 |
|
centos 7.4 | 10.0.1.20 | pc1 |
|
centos 7.4 | 10.0.1.21 | pc2 |
|
centos 7.4 | 10.0.1.222 | pc3 |
|
centos 7.4 | 10.0.1.17 | pc4 |
|
其中pc1,pc2,pc4为服务端,pc3为客户端。
2.环境准备:(pc1,pc2,pc4同时操作)
[root@localhost ~]# df -h
文件系统 容量 已用 可用已用% 挂载点
/dev/mapper/cl-root 13G 4.0G 8.3G 33% /
devtmpfs 819M 0 819M 0% /dev
tmpfs 835M 220K 835M 1% /dev/shm
tmpfs 835M 8.9M 826M 2% /run
tmpfs 835M 0 835M 0% /sys/fs/cgroup
/dev/sda1 1014M 174M 841M 18% /boot
tmpfs 167M 32K 167M 1% /run/user/1000
tmpfs 167M 0 167M 0% /run/user/0
2.1关闭SELinux
sed 's/=premissive/=disabled/'/etc/selinux/config
setenforce 0
2.2 关闭防火墙
systemctl stop firewall
systemctl disable firewall
2.3 启动glusterd
systemctl enable glusterd
systemctl start glusterd
systemctl status glusterd
2.4 修改主机配置文件
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.1.20 pc1
10.0.1.21 pc2
10.0.1.222 pc3
10.0.1.17 pc4
注:不同时在所有服务器上执行这一步将会在后面添加资源池的时候造成麻烦。 如:peer probe: failed: Probe returned with 传输端点尚未连接
2.5 测试服务器连通情况
for i in {1..4}
do
ping -c3 pc$i &> /dev/null && echo "$i up "
done
3 gluster 配置
3.1 gluster 命令帮助
[root@localhost ~]# gluster peer help
peer detach { <HOSTNAME> |<IP-address> } [force] - detach peer specified by <HOSTNAME>
peer help - Help command for peer
peer probe { <HOSTNAME> |<IP-address> } - probe peer specified by <HOSTNAME>
peer status - list status of peers
pool list - list all the nodes in the pool(including localhost)
3.2 添加和删除节点
该步骤在一台服务器端执行即可,服务器节点之间会相互连通。
示例在pc1服务器上进行。
添加节点
[root@localhost ~]# gluster peer probe pc2
peer probe: success.
[root@localhost ~]# gluster peer probe pc3
peer probe: success.
[root@localhost ~]# gluster peer probe pc4
peer probe: success.
查看节点情况
[root@localhost ~]# gluster peer status
Number of Peers: 3
Hostname: pc2
Uuid: a1686087-b50b-475e-b44c-2040b567b684
State: Peer in Cluster (Connected)
Hostname: pc3
Uuid: 85e3061e-fb8e-40fc-a029-7775de76d74d
State: Peer in Cluster (Connected)
Hostname: pc4
Uuid: 37894c9b-99aa-49a0-a6bc-1c56ab43cb64
State: Peer in Cluster (Connected)
删除节点
[root@localhost ~]# gluster peer detach pc3
peer detach: success
[root@localhost ~]# gluster peer status
Number of Peers: 2
Hostname: pc4
Uuid: 37894c9b-99aa-49a0-a6bc-1c56ab43cb64
State: Peer in Cluster (Connected)
Hostname: pc2
Uuid: a1686087-b50b-475e-b44c-2040b567b684
State: Peer in Cluster (Connected)
3.3创建卷
此时存储池里面有三台服务器:pc1,pc2,pc4。
分别在这三台服务器上建立/dev/shm/glusterfs/test1目录:
[root@localhost ~]# mkdir/dev/shm/glusterfs/test1
然后执行下面指令,同样在一台服务器上执行就行,这里依然是pc1:
[root@localhost ~]# gluster volume creategv0 replica 3 pc1:/dev/shm/glusterfs/test1 pc2:/dev/shm/glusterfs/test1pc4:/dev/shm/glusterfs/test1
volume create: gv0: success: please startthe volume to access data
这条命令的意思是使用Replicated的方式,建立一个名为gv0的卷(Volume),存储块(Brick)为3个,分别为pc1:/dev/shm/glusterfs/test1 和pc2:/dev/shm/glusterfs/test1 和pc4:/dev/shm/glusterfs/test1。
这里容易出现两种报错,一种是建立存储块Brick的位置是挂载点,已经被挂载了,解决方式是在该目录下再建立一个目录然后选用;另一种报错是创建的Brick在系统盘,这在gluster的默认情况下是不允许的,生产环境也尽可能与系统盘分开。如必须如此,在指令后方加入 force,表示强制执行。
这里还有其他卷类型,可参照进行建立:
常用卷类型
分布(distributed)
复制(replicate)
条带(striped)
基本卷:
(1) distribute volume:分布式卷
(2) stripe volume:条带卷
(3) replica volume:复制卷
复合卷:
(4) distribute stripe volume:分布式条带卷
(5) distribute replica volume:分布式复制卷
(6) stripe replica volume:条带复制卷
(7) distribute stripe replicavolume:分布式条带复制卷
删除卷指令:gluster volume delete (这里不需要执行)
3.4 启动卷
通过3.3我们建立了名为gv0的逻辑卷,现在启动它:
[root@localhost ~]# gluster volume start gv0
volume start: gv0: success
然后查看它的状态:
[root@localhost ~]# gluster volume status
Status of volume: gv0
Gluster process TCP Port RDMA Port Online Pid
------------------------------------------------------------------------------
Brick pc1:/dev/shm/glusterfs/test1 49152 0 Y 25900
Brick pc2:/dev/shm/glusterfs/test1 49152 0 Y 7852
Brick pc4:/dev/shm/glusterfs/test1 49152 0 Y 6563
Self-heal Daemon on localhost N/A N/A Y 25920
Self-heal Daemon on pc2 N/A N/A Y 7873
Self-heal Daemon on pc4 N/A N/A Y 6585
Task Status of Volume gv0
------------------------------------------------------------------------------
There are no active volume tasks
停止卷指令:gluster volume stop gv0(这里不执行)
扩展卷:
[root@localhost ~]# gluster peer probe pc3
peer probe: success.
[root@localhost ~]# gluster volume add-brickgv0 pc3:/dev/shm/glusterfs/t1/ pc3:/dev/shm/glusterfs/t2pc3:/dev/shm/glusterfs/t3
volume add-brick: success
由于这个卷的副本数设置为3,所以每次扩展都需要增加3的倍数的Brick,此次增加3个Brick。
收缩卷:(如果Brick里面有数据需要先迁移到其他Brick里面)
[root@localhost ~]# gluster volumeremove-brick gv0 pc3:/dev/shm/glusterfs/t1 pc3:/dev/shm/glusterfs/t2pc3:/dev/shm/glusterfs/t3 start
volume remove-brick start:
success
ID: d7443bc9-18a5-4de3-823c-59177833437c
4.客户端配置
根据前面的准备,这里的客户端是pc3。
4.1挂载卷
在pc3上创建一个目录以便挂载卷:
[root@localhost ~]# mkdir /mnt/test
把前面生成的逻辑卷gv0挂载到客户端:
[root@localhost ~]# mount -t glusterfspc1:/gv0 /mnt/test
[root@localhost ~]#
查看挂载结果:
[root@localhost ~]# df -h
文件系统 容量 已用 可用已用% 挂载点
/dev/mapper/cl-root 50G 11G 40G 22% /
devtmpfs 852M 0 852M 0% /dev
tmpfs 868M 348K 867M 1% /dev/shm
tmpfs 868M 8.9M 859M 2% /run
tmpfs 868M 0 868M 0% /sys/fs/cgroup
/dev/mapper/cl-home 65G 900M 64G 2% /home
/dev/sda1 1014M 174M 841M 18% /boot
tmpfs 174M 12K 174M 1% /run/user/1000
pc1:/gv0 835M 256K 835M 1% /mnt/test
发现gv0卷已经挂载在/mnt/test上了。
5.测试
client端 pc3成功挂载逻辑卷gv0之后,在挂载目录/mnt/test 建立文件以测试GlusterFS是否正常工作。
[root@localhost ~]# cd /mnt/test/
[root@localhost test]# ls
[root@localhost test]# touch f1 f2
[root@localhost test]# ls
f1 f2
因为创建卷gv0的时候使用的是镜像存储(Replicated),所以在gv0上写入的文件应该同时出现在两个服务器的/dev/shm/glusterfs/test1目录上。
在pc1和pc2以及pc4查看目录/dev/shm/glusterfs/test1,可以看到三个服务器均出现这两个文件:
[root@localhost ~]# ls /dev/shm/glusterfs/test1/
f1 f2
测试结果证明glusterfs的基本配置效果良好。