每天学五分钟 Liunx 0000 | 存储篇:GlusterFS


GlusterFS

GlusterFS,是一个分布式文件系统,它通过 TCP/IP 或 IB(InfiniBand RDMA)网络将多个存储资源整合在一起,提供存储服务,具有很强的可扩展能力。
 

GlusterFS 工作原理

 
如上图所示。GlusterFS 的 server 端共享存储资源,client 端通过 TCP 或 IB 连接到 server,然后将 server 端的存储资源挂载到本地目录。应用(Application) 对本地目录文件的读写请求会转交给内核的 VFS,然后到 FUSE 文件系统,FUSE 将此请求转交给设备 /dev/fuse,设备 /dev/fuse 再递交给 GlusterFS 的 client 端,client 通过比对 GlusterFS client 端的配置文件,根据配置文件的定义做一些处理,最后去 server 上读取存储资源的数据,实现应用读写文件的操作。
 
从上图也可以看出,不管是 GlusterFS 和 Application 都需要经过 VFS,它是至关重要的存在。 VFS(Virtual File System,虚拟文件系统)是管理整个 Liunx 文件系统的文件系统,用户再访问文件目录的时候,不需要管这个目录用的是什么文件系统,是 ext4 还是 GlusterFS 等等,这些都是 VFS 来判断处理的,因为它不像 ext4 / GlusterFS 这种文件系统是实际存在的,所以叫虚拟文件系统。
VFS 示意图如下:
 

GlsterFS 部署

GlusterFS 部署流程:
1. GlusterFS 环境准备;
2. GlsuterFS 软件包安装,启动 GlusterFS 服务;
3. server 端创建文件系统;
4. 创建 GlusterFS volume;
5. client 端挂载 volume;
 
1. GlusterFS 环境准备
主机名
ip
硬盘
NFS
server1.local
192.168.4.141
/dev/vdb
LV:5000M lianhua, 1000M huasheng
server2.local
192.168.4.59
/dev/vdb
LV:5000M lianhua, 1000M huasheng
server3.local
192.168.4.105
/dev/vdb
LV:5000M lianhua, 1000M huasheng
client1.local
192.168.4.144
/dev/vdb
 
 
配置 hosts 文件,将主机信息写到 /etc/hosts 中,每台机器都要写:
[root@server1:/root]
# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 
192.168.4.141      server1.local
192.168.4.59       server2.local
192.168.4.105      server3.local

 

关闭 iptables 和 SELiunx,确保 server 之间能相互访问,并且 client 能访问到 server。
 
 
2. GlusterFS 软件包安装
[root@server1:/root] wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo  
[root@server1:/root] yum  -y  install centos-release-gluster312.noarch
[root@server1:/root] yum -y --enablerepo=centos-gluster*-test install glusterfs-server glusterfs-cli glusterfs-geo-replication
[root@server1:/root] yum -y --enablerepo=centos-gluster*-test install glusterfs-server glusterfs-cli glusterfs-geo-replication
[root@server1:/root] systemctl start glusterd.service
[root@server1:/root] systemctl enable glusterd.service
[root@server1:/root] systemctl status glusterd.service
# systemctl status glusterd.service
● glusterd.service - GlusterFS, a clustered file-system server
   Loaded: loaded (/etc/systemd/system/glusterd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-03-28 14:27:24 CST; 6h ago
Main PID: 4519 (glusterd)
    Tasks: 188 (limit: 4915)
   Memory: 324.8M
 
[root@server1:/root] glusterfs -V
glusterfs 3.12.15
Repository revision: git://git.gluster.org/glusterfs.git
Copyright (c) 2006-2016 Red Hat, Inc. <https://www.gluster.org/>
GlusterFS comes with ABSOLUTELY NO WARRANTY.
It is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3
or later), or the GNU General Public License, version 2 (GPLv2),
in all cases as published by the Free Software Foundation.
 
3. server 端创建文件系统
这里硬盘只有一块 vdb ,所以用 LVM 的方式创建两个 LV lianhua 和 huasheng 作为 GlusterFS 的磁盘。
<LVM 的介绍可看这里>
 
在每台 server 上,将 lianhua 和 huasheng 格式化为 ext4 文件系统,然后分别挂载到 /mnt/bricks/lianhua 和 /mnt/bricks/huasheng 目录。直接 show 结果:
[root@server1:/root]
# df -hT
Filesystem                                                               Type            Size  Used Avail Use% Mounted on
/dev/vda1                                                                ext4             40G  4.7G   33G  13% /
tmpfs                                                                    tmpfs           8.9G  3.9M  8.9G   1% /tmp
/dev/mapper/server1--vol-lianhua                                         ext4            4.6G   23M  4.3G   1% /mnt/bricks/lianhua
/dev/mapper/server1--vol-huasheng                                        ext4            4.6G   20M  4.3G   1% /mnt/bricks/huasheng
 
4. 创建 GlusterFS volume
创建 GlusterFS volume 之前,在任意一台主机中将其它 server 主机加入到 glusterFS 主机池中:
[root@server1:/root]
# gluster peer probe server2
[root@server1:/root]
# gluster peer probe server3
 
[root@server1:/root]
# gluster peer status
Number of Peers: 2
 
Hostname: server2.local
Uuid: 72a5f725-1436-46e9-8ae3-404e30fc5eac
State: Peer in Cluster (Connected)
 
Hostname: server3.local
Uuid: 4b2472bc-44a6-4e92-b3fa-6746c599c5d0
State: Peer in Cluster (Connected)

 

在任意一台主机上创建 GlusterFS volume,volume 的类型是复制式(Replicated)卷:
[root@server1:/root]
# df -hT
Filesystem                                                               Type            Size  Used Avail Use% Mounted on
/dev/vda1                                                                ext4             40G  4.7G   33G  13% /
tmpfs                                                                    tmpfs           8.9G  3.9M  8.9G   1% /tmp
/dev/mapper/server1--vol-lianhua                                         ext4            4.6G   23M  4.3G   1% /mnt/bricks/lianhua
/dev/mapper/server1--vol-huasheng                                        ext4            4.6G   20M  4.3G   1% /mnt/bricks/huasheng
 
[root@server1:/root]
# gluster volume create lianhuasheng replica 3 server1:/mnt/bricks/lianhua server2:/mnt/bricks/lianhua server3:/mnt/bricks/lianhua force
volume create: lianhuasheng: success: please start the volume to access data
[root@server1:/root]
# gluster volume start lianhuasheng
[root@server1:/root]
# gluster volume info lianhuasheng
 
Volume Name: lianhuasheng
Type: Replicate
Volume ID: 895f24cb-cf45-4cba-b50d-55422aff0909
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x 3 = 3
Transport-type: tcp
Bricks:
Brick1: server1.local:/mnt/bricks/lianhua/brick
Brick2: server2.local:/mnt/bricks/lianhua/brick
Brick3: server3.local:/mnt/bricks/lianhua/brick
Options Reconfigured:
performance.client-io-threads: off
nfs.disable: on
transport.address-family: inet
cluster.server-quorum-type: none
cluster.quorum-type: auto
cluster.quorum-reads: true
cluster.consistent-metadata: on
server.allow-insecure: on
network.ping-timeout: 42
cluster.favorite-child-policy: mtime
cluster.heal-timeout: 60
cluster.server-quorum-ratio: 51%
 
[root@server1:/root]
# mount -t glusterfs server1:/lianhuasheng /mnt/lianhuasheng
 
[root@server1:/root]
# df -hT
/dev/vda1                                                                ext4             40G  4.7G   33G  13% /
tmpfs                                                                    tmpfs           8.9G  3.9M  8.9G   1% /tmp
/dev/mapper/server1--vol-lianhua                                         ext4            4.6G   23M  4.3G   1% /mnt/bricks/lianhua
/dev/mapper/server1--vol-huasheng                                        ext4            4.6G   20M  4.3G   1% /mnt/bricks/huasheng
server1.local:/lianhuasheng                                              fuse.glusterfs  4.6G   24M  4.2G   1% /mnt/lianhuasheng
可以发现,复制式 volume 的大小等于每个文件系统的大小,即,都是 4.6G。
 

GlusterFS volume 类型

上面创建的卷类型是复制式 Replicated 的,它的示意图如下:
存储文件到 volume 中,文件会同时放到每个 Brick 中,Brick 即是主机池中每个 server 的磁盘分区。所以,volume 的容量等于每个 server 的磁盘分区容量。
 
除了 Replicated 模式外,还有 Distributed 分布式卷,Striped 条带式卷,Distributed Striped 分布式条带卷和 Distributed Replicated 分布式复制卷模式。
 

Distributed 分布式卷

存储文件到 volume 中,文件会随机存放到一台 server 中的 Brick 中。所以,volume 的容量式每个 Brick 的容量之和。
 

Striped 条带式卷

存储文件到 volume 中,文件被分成数块,存放到主机池中每台 server 的 Brick 中。
 

Distributed Striped 分布式条带卷

自己看吧,不解释了,前面几种模式的排列组合。
 

Distributed Replicated 分布式复制卷

自己看吧,不解释了,前面几种模式的排列组合。
 
5. client 端挂载 volume
使用 mount 命令将 volume 挂载到 client 的 mount point:
[root@client1:/root]
# mount -t glusterfs -o acl -o rw,backupvolfile-server=server1.local server1.local:/lianhuasheng /mnt/lianhuasheng
 
[root@client1:/root]
# df -hT
Filesystem                                                               Type            Size  Used Avail Use% Mounted on
/dev/vda1                                                                ext4             40G  4.7G   33G  13% /
tmpfs                                                                    tmpfs           8.9G  3.9M  8.9G   1% /tmp
server1.local:/lianhuasheng                                              fuse.glusterfs  4.6G  20M   4.2G   8% /mnt/lianhuasheng
 
在 client 写个文件测试下:
[root@client1:/root]
# cd /mnt/lianhuasheng/
[root@client1:/mnt/lianhuasheng]
# ls
[root@client1:/mnt/lianhuasheng]
# touch GlusterFS.log
[root@client1:/mnt/lianhuasheng]
# ll -h
total 0K
-rw-r--r-- 1 root          root             0 Mar 28 22:38 GlusterFS.log
[root@client1:/mnt/lianhuasheng]
# ssh server1.local
[root@server1:/root]
# cd /mnt/lianhuasheng/
[root@server1:/mnt/lianhuasheng]
# ll -h
-rw-r--r-- 1 root          root             0 Mar 28 22:38 GlusterFS.log
 

GlusterFS 常用命令

# 查看 GlusterFS 所有 volume
gluster volume list
 
# 删除 GlusterFS volume
gluster volume stop <volume_name>
gluster volume delete <volume_name>
 
# 卸载主机池中的主机
gluster peer detach <node_name>
 
# 添加主机到主机池
gluster peer probe <node_name>
 
# 创建 volume
gluster volume create <volume_name> replica <number of replica> <server_name>:<brick_name>
 
GlusterFS 官方文档: https://docs.gluster.org/en/latest/
 
 
 
(完)
posted @ 2020-03-28 23:08  lubanseven  阅读(281)  评论(0编辑  收藏  举报