1、集群规划
集群有3个节点和1个nfs服务节点,各资源IP如下,其中nfs服务节点同时还是集群的时间戳服务器。
- nfs:192.168.2.250
- node1:192.168.2.251
- node2:192.168.2.252
- node1:192.168.2.253
- mysql_vip:192.168.2.254
配置所有节点的hosts文件,将各节点的主机名改成hosts文件对应的主机名。
# vim /etc/hosts
nfs 192.168.2.250
node1 192.168.2.251
node2 192.168.2.252
node3 192.168.2.253
# hostnamectl set-hostname nfs|node1|node2|node3
2、所有节点安装chrony
# yum install chrony ntpdate
# sed -i "/^server/d" /etc/chrony.conf
# vim /etc/chrony.conf
# 【nfs节点配置】
server ntp1.aliyun.com burst
allow 192.168.2.0/24
# 【其余节点配置】
server 192.168.2.250 burst
# systemctl start chronyd
# systemctl enable chronyd
3、nfs节点配置nfs服务,其余节点安装nfs客户端软件
- 【nfs节点】
# yum install nfs-utils rpcbind mysql
# mkdir /data
# vim /etc/exports
/data 192.168.2.0/24(rw,sync,insecure,no_subtree_check)
# systemctl start rpcbind nfs-server
# systemctl enable rpcbind nfs-server
# systemctl disable --now firewalld
# setenfoce 0
# vim /etc/selinux/config
SELINUX=disabled
- 【其余节点】
# yum install nfs-utils
# systemctl disable --now firewalld
# setenfoce 0
# vim /etc/selinux/config
SELINUX=disabled
- 【尝试挂载】
# mkdir /data
# mount -t nfs 192.168.2.250:/data /data
- 【卸载】
# umount /data
4、node节点安装corosync、pacemaker、mariadb、crmsh
- 【各node节点安装软件】
# yum install corosync pacemaker mariadb-server
- 【配置corosync】
# vim /etc/corosync/corosync.conf
totem {
version: 2
crypto_cipher: aes256
crypto_hash: sha1
interface {
ringnumber: 0
bindnetaddr: 192.168.2.0
mcastaddr: 239.255.10.24
mcastport: 5405
ttl: 1
}
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
logfile: /var/log/cluster/corosync.log
to_syslog: no
debug: off
timestamp: on
logger_subsys {
subsys: QUORUM
debug: off
}
}
quorum {
provider: corosync_votequorum
}
nodelist {
node {
ring0_addr: node1
nodeid: 1
}
node {
ring0_addr: node2
nodeid: 2
}
node {
ring0_addr: node3
nodeid: 3
}
}
- 【生成authkey】
# corosync_keygen
- 【复制/etc/corosync/目录下所有文件到其余node节点】
# scp -r /etc/corosync/ root@node2:/etc/
# scp -r /etc/corosync/ root@node3:/etc/
- 【各node节点启动corosync及pacemaker并验证】
# systemctl start corosync pacemaker
# systemctl enable corosync pacemaker
# ss -tunl
udp UNCONN 0 0 192.168.2.251:5404 *:*
udp UNCONN 0 0 239.255.10.24:5405 *:*
udp UNCONN 0 0 192.168.2.251:5405 *:*
# corosync-cfgtool -s
Printing ring status.
Local node ID 1
RING ID 0
id = 192.168.2.251
status = ring 0 active with no faults
# corosync-cmapctl | grep members
runtime.totem.pg.mrp.srp.members.1.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.1.ip (str) = r(0) ip(192.168.2.251)
runtime.totem.pg.mrp.srp.members.1.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.1.status (str) = joined
runtime.totem.pg.mrp.srp.members.2.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.2.ip (str) = r(0) ip(192.168.2.252)
runtime.totem.pg.mrp.srp.members.2.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.2.status (str) = joined
runtime.totem.pg.mrp.srp.members.3.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.3.ip (str) = r(0) ip(192.168.2.253)
runtime.totem.pg.mrp.srp.members.3.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.3.status (str) = joined
- 【各node节点设置mariadb开机自启】
# systemctl enable mariadb
- 【各node节点配置mariadb】
# vim /etc/my.cnf
[mysqld]
datadir=/data
skip_name_resolve=ON
5、安装crmsh
下载crmsh安装包合集:https://download.youkuaiyun.com/download/limaofu/12414491
- 【安装crmsh】
# mkdir crmsh
# cd crmsh
# ls -l
total 880
-rw-r--r--. 1 root root 763644 May 12 22:56 crmsh-3.0.0-6.2.noarch.rpm
-rw-r--r--. 1 root root 95480 May 12 22:56 crmsh-scripts-3.0.0-6.2.noarch.rpm
-rw-r--r--. 1 root root 36732 May 12 22:56 python-parallax-1.0.1-29.1.noarch.rpm
# yum install ./*.rpm
- 【测试crmsh并关闭集群的STONITH功能】
# crm
crm(live)# configure
crm(live)configure# property stonith-enabled=false
crm(live)configure# verify
crm(live)configure# commit
6、配置mysql虚拟IP资源
crm(live)configure# primitive dbvip ocf:IPaddr params ip="192.168.2.254" cidr_netmask="24" broadcast="192.168.2.255" op start timeout=20s op stop timeout=20s op monitor interval=10s timeout=20s
crm(live)configure# verify
crm(live)configure# commit
7、配置nfs资源
crm(live)configure# primitive dbdata ocf:Filesystem device="nfs:/data/share" directory="/data" fstype="nfs" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
crm(live)configure# verify
crm(live)configure# commit
8、配置mariadb服务资源
crm(live)configure# primitive db systemd:mariadb op start timeout=100s op stop timeout=100s op monitor interval=60s timeout=100s
crm(live)configure# verify
crm(live)configure# commit
9、配置各资源启动顺序
crm(live)configure# order dbdata_db_dbvip inf: dbdata db dbvip
crm(live)configure# verify
crm(live)configure# commit
10、配置此三个资源为同一个组共同调度
crm(live)configure# group nfs_mariadb_vip dbdata db dbvip
crm(live)configure# verify
crm(live)configure# commit
11、在资源运行的节点,初始化mariadb数据库
- 【查看资源运行在那个节点】
# crm status
Stack: corosync
Current DC: node1 (version 1.1.21-4.el7-f14e36fd43) - partition with quorum
Last updated: Wed May 13 11:43:03 2020
Last change: Wed May 13 09:31:36 2020 by root via crm_attribute on node2
3 nodes configured
3 resources configured
Online: [ node1 node2 node3 ]
Full list of resources:
Resource Group: nfs_mariadb_vip
dbdata (ocf::heartbeat:Filesystem): Started node1
db (systemd:mariadb): Started node1
dbip (ocf::heartbeat:IPaddr): Started node1
切换到资源运行的节点,此处为node1
- 【初始化mariadb】
# mysql_secure_installation
- 【配置mysql的root用户可以远程登录】
# mysql -uroot -p
MariaDB [(none)]> grant all on *.* to 'root'@'%' identified by 'password';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> \q
12、验证高可用
在资源运行的节点下,此处为node1节点
- 【手动让节点下线】
crm(live)# node
crm(live)node# standby
crm(live)node# exit
# watch -n 3 'crm status'
- 【手动让节点上线】
# crm node online