Corosync是集群管理套件的一部分,他在传递信息的时候通过一个简单的配置文件来定义信息传递的方式和协议等,其中最主要的是可以提供心跳信息传输。
pacemaker是一个集群资源管理器,他利用集群的基础结构,例如corosync提供的信息he成员管理能力,来探测并从节点或资源级别故障中恢复,实现集群的高可用性。
环境:
centosa:192.168.40.12
centosb:192.168.40.19
实验的三点基础:主机之间ssh互信;时间同步;域名能互相通信。
两个节点停止要接收管理的服务:
systemctl stop drbd
systemctl stop mariadb
两个节点安装软件:
yum install -y pcs
yum install -y corosync
yum install -y pacemaker
安装crm:
tar -xf crmsh-2.3.2.tar
cd crmsh-2.3.2
python setup.py install
开始定义资源:
[root@centosa ~]# crmcrm(live)# configure
crm(live)configure# primitive mysqldrbd ocf:linbit:drbd params drbd_resource=mysql op start timeout=240 op stop timeout=100 op monitor role=Master interval=20 timeout=30 op monitor role=Slave interval=30 timeout=30 ##添加一个名为mysqldrbd的资源
crm(live)configure# verify ##检查是否有问题
crm(live)configure# ms ms_mysqldrbd mysqldrbd meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true ##设置主从
crm(live)configure# verify
crm(live)configure# commit ##检查没问题后保存设置
查看一下状态:
crm(live)configure# cdcrm(live)# statusStack: corosyncCurrent DC: centosb (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorumLast updated: Wed Oct 25 20:18:22 2017Last change: Wed Oct 25 20:18:04 2017 by root via crm_attribute on centosb2 nodes configured2 resources configuredOnline: [ centosa centosb ]Full list of resources: Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ centosa ] Slaves: [ centosb ]
添加一个文件挂载资源:
crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd1 directory=/data fstype=xfs op start timeout=60 op stop timeout=60
绑定文件资源和drbd主从为亲缘关系(当drbd为主时):
crm(live)configure# colocation ms_mysqldrbd_with_mystore inf: mystore ms_mysqldrbd:Master
设定文件资源和drbd启动的先后顺序(当drbd状态提升时,文件资源系统才挂载):
crm(live)configure# order mystore_after_ms_mysqldrbd Mandatory: ms_mysqldrbd:promote mystore:start
检查并保存:
crm(live)configure# verifycrm(live)configure# commit
添加mysql资源,并与文件资源绑定为亲缘关系然后设置先后顺序:
crm(live)configure# primitive mysqld systemd:mariadbcrm(live)configure# colocation mysqld_with_mystore inf: mysqld mystorecrm(live)configure# order mysqld_after_mystore Mandatory:mystore mysqldcrm(live)configure# verifycrm(live)configure# commit
添加vip资源(并与drbd的主状态绑定):
crm(live)configure# primitive vip ocf:heartbeat:IPaddr params ip=192.168.40.100 op monitor interval=20 timeout=20 on-fail=restartcrm(live)configure# colocation vip_with_ms_mysqldrbd inf: ms_mysqldrbd:Master vip
设置先启动mysqld在启动vip:
crm(live)configure# order vip_after_mysqld Mandatory: mysqld vip
保存并查看状态:
crm(live)configure# verifycrm(live)configure# commitcrm(live)configure# cd crm(live)# statusStack: corosyncCurrent DC: centosb (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorumLast updated: Wed Oct 25 20:41:40 2017Last change: Wed Oct 25 20:41:34 2017 by root via cibadmin on centosa2 nodes configured5 resources configuredOnline: [ centosa centosb ]Full list of resources: Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ centosa ] Slaves: [ centosb ] mystore(ocf::heartbeat:Filesystem):Started centosa mysqld(systemd:mariadb):Started centosa vip(ocf::heartbeat:IPaddr):Started centosa
在本地登录数据库,并创建一个数据库,然后设置给远程登录的权限:
MariaDB [(none)]> create database hatest;MariaDB [(none)]> grant all on *.* to 'root'@'%' identified by '123456';
然后在远程用vip登录:
[root@centos1 ~]# mysql -uroot -p123456 -h192.168.40.100MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || centos || hatest || mysql || performance_schema || test |+--------------------+
看到有一个添加的数据库。
把主的节点停掉:
crm(live)# node standby
然后查看状态,会发现按先后顺序切换到centosb
crm(live)# statusStack: corosyncCurrent DC: centosb (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorumLast updated: Wed Oct 25 21:45:54 2017Last change: Wed Oct 25 21:21:31 2017 by root via crm_attribute on centosb2 nodes configured5 resources configuredNode centosa: standbyOnline: [ centosb ]Full list of resources: Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ centosb ] Stopped: [ centosa ] mystore(ocf::heartbeat:Filesystem):Started centosb mysqld(systemd:mariadb):Started centosb vip(ocf::heartbeat:IPaddr):Started centosb
远程登录,查看数据库:
[root@centos1 ~]# mysql -uroot -p123456 -h192.168.40.100Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 3Server version: 5.5.56-MariaDB MariaDB ServerCopyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases -> ;+--------------------+| Database |+--------------------+| information_schema || centos || hatest || mysql || performance_schema || test |+--------------------+6 rows in set (0.00 sec)
本文出自 “运维小记” 博客,请务必保留此出处http://lsfandlinux.blog.51cto.com/13405754/1976207