使用 GlusterFS创建NFS集群
0x01 安装 GlusterFS
- 在两台 Ubuntu 服务器上安装 GlusterFS:
sudo apt-get update
sudo apt-get install -y glusterfs-server
- 启动 GlusterFS 服务
sudo systemctl start glusterd
sudo systemctl enable glusterd
0x02 配置 GlusterFS
- 在第一台节点(node1)上将第二台节点(node2)添加到集群中:
sudo gluster peer probe <node2-ip>
- 检查集群状态
sudo gluster peer status
- 创建一个分布式或复制卷(假设存储路径为 /data/gluster)
sudo mkdir -p /data/gluster
# 分布式卷(性能优先)
sudo gluster volume create gv0 transport tcp <node1>:/data/gluster <node2>:/data/gluster
# 复制卷(高可用优先)
sudo gluster volume create gv0 replica 2 transport tcp <node1>:/data/gluster <node2>:/data/gluster
# 启动卷
sudo gluster volume start gv0
- 设置卷权限(可选):
# 全部允许
sudo gluster volume set gv0 auth.allow "*"
# 配置只能某个网段访问
sudo gluster volume set gv0 auth.allow "192.168.0.*"
- 验证配置,查看当前卷配置
sudo gluster volume info gv0
0x03 通过 keepalived 创建虚拟 IP(VIP)
通过 Keepalived 创建一个虚拟 IP(VIP),可以让客户端通过该 VIP 访问 GlusterFS 卷 gv0。
需要注意的是:在使用 Keepalived 配置虚拟 IP(VIP)时,通常需要关闭网卡的源目地址检查功能(rp_filter),以确保 VIP 能够正常工作。
- 在两台机子上安装 keepalived
sudo apt-get update
sudo apt-get install -y keepalived
- 配置 keepalived
主节点配置:
sudo vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mypassword
}
virtual_ipaddress {
192.168.0.11
}
}
备节点配置:
sudo vi /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass mypassword
}
virtual_ipaddress {
192.168.0.11
}
}
- 启动 Keepalived,在两台服务器上启动并设置 Keepalived 开机自启
sudo systemctl start keepalived
sudo systemctl enable keepalived
- 验证 VIP
# 1. 在主节点运行以下命令,确认 VIP 已分配
ip addr show | grep 192.168.0.11
# 2. 测试故障转移,手动停止主节点上的 Keepalived 服务
sudo systemctl stop keepalived
# 3. 检查备份节点是否接管了 VIP
ip addr show
0x04 通过 NFS 方式挂载
需要通过标准 NFS 协议访问 GlusterFS 卷,可以使用 NFS-Ganesha 作为网关。
- 每个节点都安装 NFS-Ganesha,使 GlusterFS 卷支持 NFS 访问。
sudo apt-get install -y nfs-ganesha-gluster
- 编辑 NFS-Ganesha 的配置文件
/etc/ganesha/ganesha.conf,添加对 GlusterFS 卷的支持。
sudo vi /etc/ganesha/ganesha.conf
EXPORT_DEFAULTS {
Access_Type = RW;
}
EXPORT {
Export_Id = 1;
Path = "/gv0";
Pseudo = "/gv0";
Disable_ACL = True;
Protocols = "3","4";
Access_Type = RW;
Squash = No_root_squash;
Sectype = "sys";
Transports = "TCP";
FSAL {
Name = "GLUSTER";
Hostname = "192.168.0.11";
Volume = "gv0";
}
}
LOG {
Default_Log_Level = WARN;
}
- 确保 NFS-Ganesha 在 GlusterFS 服务完全启动并就绪后延迟 5 秒再启动,以避免因依赖服务未完全初始化导致的启动失败。
sudo vi /usr/lib/systemd/system/nfs-ganesha.service
添加以下内容:
[Unit]
After=glusterd.service
Requires=glusterd.service
[Service]
ExecStartPre=/bin/sleep 5
- 启动 NFS-Ganesha
注意:启动 nfs-ganesha 会和 nfs 端口冲突,需要先停掉 nfs。
sudo systemctl daemon-reload
sudo systemctl reload nfs-ganesha
sudo systemctl restart nfs-ganesha
sudo systemctl enable nfs-ganesha
# 查看日志
sudo tail -f /var/log/ganesha/ganesha.log
- 客户端通过 NFS 进行挂载
sudo mkdir -p /mnt/nfs
sudo mount -t nfs 192.168.0.11:/gv0 /mnt/nfs
0x05 通过 GlusterFS 挂载
在客户端上挂载 GlusterFS 卷作为 NFS 共享:
参考资料
通过 nfs-ganesha 实现 nfs 客户端挂载 glusterfs 虚拟卷: