高可用NFS(Network File System)解决方案可以确保在服务器节点故障时,存储服务能够自动切换到备用节点,从而保证业务连续性。以下是几种常见的实现方式:
# 在两台服务器上执行
sudo apt-get install drbd8-utils heartbeat nfs-kernel-server # Ubuntu/Debian
sudo yum install drbd kmod-drbd heartbeat nfs-utils # RHEL/CentOS
编辑/etc/drbd.d/nfs.res:
resource nfs {
protocol C;
startup {
wfc-timeout 15;
degr-wfc-timeout 60;
}
disk {
on-io-error detach;
}
net {
cram-hmac-alg sha1;
shared-secret "your-secret";
}
on node1 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.1:7788;
meta-disk internal;
}
on node2 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.2:7788;
meta-disk internal;
}
}
初始化并启动DRBD:
sudo drbdadm create-md nfs
sudo drbdadm up nfs
# 在主节点执行
sudo drbdadm primary --force nfs
sudo mkfs.ext4 /dev/drbd0
编辑/etc/ha.d/ha.cf:
logfile /var/log/ha-log
keepalive 2
deadtime 10
warntime 5
initdead 20
udpport 694
auto_failback on
node node1 node2
编辑/etc/ha.d/haresources:
node1 IPaddr::192.168.1.100/24/eth0 drbddisk::nfs Filesystem::/dev/drbd0::/nfs::ext4 nfs-kernel-server
在两台服务器上编辑/etc/exports:
/nfs 192.168.1.0/24(rw,sync,no_subtree_check)
sudo apt-get install pacemaker corosync fence-agents pcs # Ubuntu/Debian
sudo yum install pacemaker corosync fence-agents pcs # RHEL/CentOS
sudo pcs cluster auth node1 node2
sudo pcs cluster setup --name nfs_cluster node1 node2
sudo pcs cluster start --all
sudo pcs resource create nfs_drbd ocf:linbit:drbd drbd_resource=nfs op monitor interval=60s
sudo pcs resource master nfs_drbd_clone nfs_drbd master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
sudo pcs resource create nfs_fs Filesystem device="/dev/drbd0" directory="/nfs" fstype="ext4"
sudo pcs resource create nfs_ip IPaddr2 ip=192.168.1.100 cidr_netmask=24
sudo pcs resource create nfs_export exportfs clientspec="192.168.1.0/24" options="rw,sync,no_subtree_check" directory="/nfs" fsid=1
sudo pcs resource create nfs_daemon nfsserver nfs_shared_infodir="/nfs/nfsinfo"
sudo pcs constraint colocation add nfs_fs with nfs_drbd_clone INFINITY with-rsc-role=Master
sudo pcs constraint order promote nfs_drbd_clone then start nfs_fs
sudo pcs constraint colocation add nfs_ip with nfs_fs INFINITY
sudo pcs constraint colocation add nfs_export with nfs_fs INFINITY
sudo pcs constraint colocation add nfs_daemon with nfs_fs INFINITY
sudo pcs cluster stop node1
sudo pcs status
sudo pcs cluster start node1
# 监控DRBD状态
sudo drbd-overview
# 监控集群状态
sudo pcs status
# 手动切换主节点
sudo pcs resource move nfs_drbd_clone node2
# 维护模式
sudo pcs property set maintenance-mode=true
以上方案可以根据实际环境和需求进行调整,对于关键业务系统,建议在测试环境充分验证后再部署到生产环境。