本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 18.04 (Bionic Beaver)
  • Ubuntu 18.04 (Bionic Beaver)
  • Ubuntu 12.10 (Quantal Quetzal)
  • Ubuntu 12.04 LTS (Precise Pangolin)
  • Ubuntu 11.10 (Oneiric Ocelot)

本页内容

  • 环境要求
  • 开始之前
  • 安装 GlusterFS 服务端
  • 创建 GlusterFS 存储
  • 配置 GlusterFS 卷
  • 测试数据复制
  • 总结

GlusterFS 是由 Gluster Inc 开发、随后由 Redhat 接手维护的一款免费、开源的软件定义分布式存储。它提供对象、块和文件存储接口,可用于云存储、CDN、媒体流等高数据密集型工作负载。它能够以积木式方式横向扩展,存储数拍字节(petabytes)级的数据。它支持 NFS、SMB、CIFS、HTTP、FTP 等多种协议。它非常适合需要处理海量数据的各类应用场景。

在本教程中,我们将学习如何在 Ubuntu 22.04 上搭建 Replicated GlusterFS 卷(复制卷)。

环境要求

  • 两台运行 Ubuntu 22.04 的服务器,每台配备 2 GB 外接硬盘。
  • node1 配置静态 IP 地址 192.168.10.10,node2 配置静态 IP 地址 192.168.10.11。
  • 两台服务器均已配置 root 密码。

开始之前

在开始之前,建议先更新两台服务器上的所有系统软件包。你可以在两台服务器上分别运行以下命令进行更新。

apt-get update -y
apt-get upgrade -y

所有系统软件包更新完成后,重启服务器以使更改生效。

接下来,你还需要编辑两台服务器上的 /etc/hosts 文件,配置主机名解析,使每台服务器都能通过主机名与另一台通信。可以使用以下命令完成:

nano /etc/hosts

添加以下行:

192.168.10.10 node1
192.168.10.11 node2

完成后保存并关闭文件。

安装 GlusterFS 服务端

默认情况下,GlusterFS 已包含在 Ubuntu 22.04 的默认软件仓库中。你可以在两台服务器上使用以下命令安装它。

apt-get install glusterfs-server -y

GlusterFS 软件包安装完成后,使用以下命令启动 GlusterFS 服务并设置开机自启:

systemctl start glusterd
systemctl enable glusterd

你也可以使用以下命令查看 GlusterFS 服务的状态:

systemctl status glusterd

你应该看到类似以下输出:

? glusterd.service - GlusterFS, a clustered file-system server
     Loaded: loaded (/lib/systemd/system/glusterd.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-11-05 04:10:13 UTC; 7s ago
       Docs: man:glusterd(8)
    Process: 92014 ExecStart=/usr/sbin/glusterd -p /var/run/glusterd.pid --log-level $LOG_LEVEL $GLUSTERD_OPTIONS (code=exited, status=0/SUCC>
   Main PID: 92015 (glusterd)
      Tasks: 9 (limit: 4579)
     Memory: 7.1M
        CPU: 2.260s
     CGroup: /system.slice/glusterd.service
             ??92015 /usr/sbin/glusterd -p /var/run/glusterd.pid --log-level INFO

Nov 05 04:10:11 ubuntu2204 systemd[1]: Starting GlusterFS, a clustered file-system server...
Nov 05 04:10:13 ubuntu2204 systemd[1]: Started GlusterFS, a clustered file-system server.

完成后,即可进入下一步。

创建 GlusterFS 存储

要创建 GlusterFS 存储,两台服务器都需要一块外接硬盘。你还需要在两台服务器的外接硬盘(/dev/sdb)上创建分区。

要在 /dev/sdb 上创建新分区,运行以下命令:

fdisk /dev/sdb

你应该看到类似以下输出:

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x06091fe8.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-4194303, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): 

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

接下来,使用以下命令格式化该分区:

mkfs.xfs /dev/sdb1

你应该看到类似以下输出:

meta-data=/dev/sdb1              isize=512    agcount=4, agsize=131008 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=0, rmapbt=0, reflink=0
data     =                       bsize=4096   blocks=524032, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

接下来,使用以下命令在两台服务器上创建用于 GlusterFS 存储的目录:

mkdir /glustervolume

接下来,将磁盘 /dev/sdb1 挂载到 /glustervolume 目录。

mount.xfs /dev/sdb1 /glustervolume

接下来,编辑 /etc/fstab 文件,使挂载在重启后依然生效:

nano /etc/fstab

添加以下行:

/dev/sdb1 /glustervolume xfs defaults 0 0

保存并关闭文件,然后运行以下命令使更改生效:

mount -a

现在可以使用以下命令查看已挂载的目录:

df -h

你应该看到类似以下输出:

Filesystem      Size  Used Avail Use% Mounted on
tmpfs           393M  1.1M  392M   1% /run
/dev/sda2        79G  8.6G   67G  12% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           393M  4.0K  393M   1% /run/user/0
/dev/sdb1       2.0G   35M  2.0G   2% /glustervolume

配置 GlusterFS 卷

首先,需要通过将 node2 加入 node1 来创建一个可信存储池(trusted storage pool)。为此,在 node1 上运行以下命令:

gluster peer probe node2

你可以使用以下命令验证已添加的存储池:

gluster pool list

你应该看到类似以下输出:

UUID					Hostname 	State
52245177-ce2e-4217-8da5-2ee7a33dcf46	node2    	Connected 
b6da6e83-b1ea-46de-8904-1c5345b1c229	localhost	Connected 

接下来,使用以下命令在两台节点上创建 brick 目录:

mkdir /glustervolume/vol1

接下来,创建一个名为 vol1、包含两份副本(replica)的卷:

gluster volume create vol1 replica 2 node1:/glustervolume/vol1 node2:/glustervolume/vol1

接下来,使用以下命令启动该卷:

gluster volume start vol1

你可以使用以下命令查看所创建卷的状态:

gluster volume status

你应该看到类似以下输出:

Status of volume: vol1
Gluster process                             TCP Port  RDMA Port  Online  Pid
------------------------------------------------------------------------------
Brick node1:/glustervolume/vol1               49152     0          Y       7198 
Brick node2:/glustervolume/vol1               49152     0          Y       13214
Self-heal Daemon on localhost               N/A       N/A        Y       6121 
Self-heal Daemon on node2                   N/A       N/A        Y       12768
 
Task Status of Volume vol1
------------------------------------------------------------------------------
There are no active volume tasks

现在你可以使用以下命令查看所创建卷的信息:

gluster volume info vol1

你应该看到类似以下输出:

Volume Name: vol1
Type: Replicate
Volume ID: a1023sd6d-81rt-12a2-bc4e-a4gd156c5331
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: node1:/glustervolume/vol1
Brick2: node2:/glustervolume/vol1
Options Reconfigured:
transport.address-family: inet
nfs.disable: on
performance.client-io-threads: off

接下来,通过编辑 /etc/fstab 文件,将所创建的卷挂载到两台节点的 /opt 目录,使卷在服务器启动后自动可用。

nano /etc/fstab

添加以下行:

localhost:/vol1 /opt glusterfs defaults,_netdev 0 0

完成后保存并关闭文件。然后运行以下命令使更改生效:

mount -a

测试数据复制

你的 GlusterFS 服务端现已安装并配置完成。现在,是时候测试两个节点之间的数据复制了。

首先,在 node1 上创建两个文件:

touch /opt/test1 /opt/test2

现在,切换到 node2 并运行以下命令:

ls -l /opt/

你应该看到在 node1 上创建的相同文件:

total 0
-rw-r--r-- 1 root root 0 Nov  7 13:42 file1
-rw-r--r-- 1 root root 0 Nov  7 13:42 file2

现在,使用以下命令停止 node2 上的 GlusterFS 服务:

service glusterd stop

接下来,切换到 node1 并查看 peer 状态:

gluster peer status

你应该看到 node2 已断开连接:

Number of Peers: 1

Hostname: node2
Uuid: a134f146-1220-3avd-d3f6-1c2554a87sa1
State: Peer in Cluster (Disconnected)

你也可以使用以下命令查看卷的状态:

gluster volume status

你应该看到类似以下输出:

Status of volume: vol1
Gluster process                             TCP Port  RDMA Port  Online  Pid
------------------------------------------------------------------------------
Brick node1:/glustervolume/vol1               49152     0          Y       7987 
Self-heal Daemon on localhost               N/A       N/A        Y       8010 
 
Task Status of Volume vol1
------------------------------------------------------------------------------
There are no active volume tasks

接下来,在 node1 上再创建两个文件:

touch /opt/test3 /opt/test4

现在,切换到 node2 并使用以下命令启动 GlusterFS 服务:

service glusterd start

现在,查看 node2 上的 /opt 目录:

ls -l /opt/

你应该看到 node1 上的所有文件已被复制,现在都可用了。

total 0
-rw-r--r-- 1 root root 0 Nov  7 13:42 test1
-rw-r--r-- 1 root root 0 Nov  7 13:42 test2
-rw-r--r-- 1 root root 0 Nov  7 13:44 test3
-rw-r--r-- 1 root root 0 Nov  7 13:44 test4

总结

恭喜!你已成功在 Ubuntu 22.04 服务器上安装并配置了一个双节点 Replicated GlusterFS 服务端。如需了解更多信息,请阅读 GlusterFS 官方文档。如有任何疑问,欢迎向我提问。