主要安装pacemaker和corosync,pacemaker是集群的管理软件,corosync是心跳套件。
给server1和server5上安装
[root@server5 mfs]# yum install pacemaker pcs psmisc policycoreutils-python -y
[root@server1 mfs]# yum install pacemaker pcs psmisc policycoreutils-python -y
并且两个节点开启pcsd服务用来管理集群
root@server5 mfs]# systemctl enable --now pcsd.service
root@server1 mfs]# systemctl enable --now pcsd.service
接下来在两个节点设置密码。
[root@server5 mfs]# passwd hacluster
[root@server1 mfs]# passwd hacluster
下来作认证
[root@server1 ~]# pcs cluster auth server1 server5 ##自动作认证
Username: hacluster ##用户是安装完软件就会有的
Password: ##密码要自己设置
server5: Authorized
server1: Authorized
下来将两个机器加入集群。
[root@server1 ~]# pcs cluster setup --name mycluster server1 server5 ##下面这些动作都是自动的,自动拉取认证等信息,等待即可
Destroying cluster on nodes: server1, server5...
server1: Stopping Cluster (pacemaker)...
server5: Stopping Cluster (pacemaker)...
server1: Successfully destroyed cluster
server5: Successfully destroyed cluster
Sending 'pacemaker_remote authkey' to 'server1', 'server5'
server1: successful distribution of the file 'pacemaker_remote authkey'
server5: successful distribution of the file 'pacemaker_remote authkey'
Sending cluster config files to the nodes...
server1: Succeeded
server5: Succeeded
Synchronizing pcsd certificates on nodes server1, server5...
server5: Success
server1: Success
Restarting pcsd on the nodes in order to reload the certificates...
server5: Success
server1: Success
下来就是启动集群了
[root@server1 ~]# pcs status ##查看集群状态,现在还没有启动,所以状态为错误的
Error: cluster is not currently running on this node
[root@server1 ~]# pcs cluster start --all ##将所有节点的集群进行启动,主要为corosync和pacemaker
server1: Starting Cluster (corosync)...
server5: Starting Cluster (corosync)...
server5: Starting Cluster (pacemaker)...
server1: Starting Cluster (pacemaker)...
注:最好将这两个设置成开机自启,不然每次启动需要再手动启动。
设置完成后就完成了。
[root@server1 ~]# pcs status
Cluster name: mycluster
WARNINGS:
No stonith devices and stonith-enabled is not false
Stack: corosync
Current DC: server5 (version 1.1.20-5.el7_7.2-3c4c782f70) - partition with quorum
Last updated: Wed Mar 25 19:54:31 2020
Last change: Wed Mar 25 19:46:23 2020 by hacluster via crmd on server5
2 nodes configured
0 resources configured
Online: [ server1 server5 ] ##两台机器
No resources
Daemon Status:
corosync: active/enabled ##软件都已经启动
pacemaker: active/enabled
pcsd: active/enabled
[root@server1 local]# pcs resource create vip ocf:heartbeat:IPaddr2 ip=192.168.122.100 ##创建一个虚拟ip
[root@server1 local]# pcs resource show
vip (ocf::heartbeat:IPaddr2): Started server1##虚拟ip已经在server1上启动
[root@server1 local]# ip addr
ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:39:f6:f7 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.11/24 brd 192.168.122.255 scope global noprefixroute ens3
valid_lft forever preferred_lft forever
inet 192.168.122.100/24 brd 192.168.122.255 scope global secondary ens3 ##虚拟ip已经出来了
当我们将server1 standby后vip会自动转移到server5上
[root@server5 mfs]# pcs status
vip (ocf::heartbeat:IPaddr2): Started server5
设置完成后在所有节点上将解析改到vip节点上去。删除原来server1上mfsmaster的解析。并且停掉所有的存储节点。
vim /etc/hosts
192.168.122.100 mfsmaster
[root@server3 ~]# systemctl stop moosefs-chunkserver.service
下来我们要将存储数据进行共享
共享我们在server4上做,使用iscsi
首先给server4上添加一个盘,再下载targetcli并启动
[root@server4 mfs]# yum install targetcli -y
[root@server4 mfs]# systemctl start target.service
使用iscsi将server4上的盘共享出去。
[root@server4 mfs]# targetcli
/> backstores/block create mydisk /dev/vda ##将磁盘加入
/iscsi> create iqn.2020-03.org.passyt:mydisk ##起名
/iscsi> iqn.2020-03.org.passyt:mydisk/tpg1/luns create /backstores/block/mydisk
/iscsi> iqn.2020-03.org.passyt:mydisk/tpg1/acls create iqn.2020-03.org.passyt:client
共享完成在server1和5安装iscsi,发现磁盘
[root@server1 local]# yum install iscsi-* -y
[root@server1 local]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2020-03.org.passyt:client ##将名字改变
[root@server1 local]# iscsiadm -m discovery -t st -p 192.168.122.14 ##发现磁盘
[root@server1 local]# iscsiadm -m node -l ##登陆
[root@server1 local]# fdisk /dev/sdb ##将磁盘进行分区
[root@server1 local]# mkfs.xfs /dev/sdb1 ##格式化分区
将上面的操作在server5上也进行iscsi的发现和登陆
[root@server1 local]# mount /dev/sdb1 /mnt/ ##将共享磁盘挂载
[root@server1 local]# cd /var/lib/mfs/
[root@server1 mfs]# ls
changelog.6.mfs changelog.7.mfs metadata.crc metadata.mfs metadata.mfs.back.1 stats.mfs
[root@server1 mfs]# cp -p * /mnt/ ##将所有文件复制到共享设备中
[root@server1 mfs]# chown mfs.mfs /mnt/ ##更改设备权限
现在进行测试在server1上的共享设备卸载,在server5上再进行挂载。
[root@server5 mfs]# mount /dev/sdb1 /var/lib/mfs/
[root@server5 mfs]# cd /var/lib/mfs/
[root@server5 mfs]# ls
changelog.6.mfs changelog.7.mfs metadata.crc metadata.mfs metadata.mfs.back.1 stats.mfs
[root@server5 mfs]# systemctl start moosefs-master ##启动服务,没报错说明没问题
测试完成将master停止并且解除挂载
[root@server1 system]# pcs resource create mfsdata ocf:heartbeat:Filesystem device=/dev/sdb1 directory=/var/lib/mfs/ fstype=xfs op monitor interval=60s ##设置挂载目录,挂载点,挂载类型和监控存储时间
[root@server1 system]# pcs resource create mfsmaster systemd:moosefs-master ##创建一个master 并设置启动脚本
[root@server1 system]# pcs resource group add mfsgroup vip mfsdata mfsmaster ##创建一个mfsgroup组,将所有东西加入这个组,后面写的顺序就是组启动的顺序
[root@server1 system]# pcs status
Resource Group: mfsgroup ##加入同一个组后所有东西就跑到一个节点中去了
vip (ocf::heartbeat:IPaddr2): Started server5
mfsdata (ocf::heartbeat:Filesystem): Started server5
mfsmaster (systemd:moosefs-master): Started server5
作完上面的东西就可以将存储全部打开
[root@server2 mfs]# systemctl start moosefs-chunkserver
这样高可用就创建完成
我们将server5standby
[root@server5 system]# pcs node standby
[root@server1 system]# pcs status
Resource Group: mfsgroup
vip (ocf::heartbeat:IPaddr2): Started server5
mfsdata (ocf::heartbeat:Filesystem): Stopping server5
mfsmaster (systemd:moosefs-master): Stopped
[root@server1 system]# pcs status
Resource Group: mfsgroup
vip (ocf::heartbeat:IPaddr2): Started server1
mfsdata (ocf::heartbeat:Filesystem): Started server1
mfsmaster (systemd:moosefs-master): Started server1
这样就将server5上的数据迁移到了server1上
完成之后我们害怕的是有的节点突然内核奔溃,所以我们需要创建fence机制。
在真机上安装fence
[root@localhost ~]# yum install fence-virtd fence-virtd-libvirt.x86_64 fence-virtd-multicast.x86_64
[root@localhost ~]# fence_virtd -c ##创建
查看有没有/etc/cluster这个目录,如果没有就创建一个
[root@localhost cluster]# dd if=/dev/urandom of=/etc/cluster/fence_xvm.key bs=128 count=1 ##给文件中创建随机数,创建完成需要重启fence
[root@localhost cluster]# systemctl restart fence_virtd.service
在server1和5上安装fence-virt并且创建/etc/cluster目录
[root@server1 ~]# yum install fence-virt
再将真机中的fence_xvm.key文件发送到server1和5中
[root@server1 ~]# pcs stonith create vmfence fence_xvm pcmk_host_map="server1:1;server5:5"##创建fence映射
这样测试的时候当内核崩溃或者网络停止后会将节点直接重启。