基于corosync和pacemaker实现drbd+mysql高可用
基于corosync和pacemaker实现drbd+mysql高可用环境主机-centos7 node1:172.25.0.29 node2:172.25.0.30
Mysql,drbd已经安装好,我们重新初始化一下资源:搭建参考文档:
http://xiaozhagn.blog.51cto.com/13264135/1975397
Corosync和Pacemaker这里搭建不再演示了,详细可以参考文档:
http://xiaozhagn.blog.51cto.com/13264135/1976185
一、
1、初始化了资源
# crm
crm(live)# status
Stack: corosync
Current DC: node2 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Wed Oct 25 23:16:23 2017
Last change: Wed Oct 25 23:02:04 2017 by root via cibadmin on node1
2 nodes configured
0 resources configured
Online: [ node1 node2 ]
No resources
2、在node1和node2启用和禁用资源:
手动启用资源,之前我关闭过,现在重启,关闭过的需要这样做
#drbdadm up mysql ####我已经搭建的drbd名字叫mysql
#drbdadm up mysql
# drbd-overview
NOTE: drbd-overview will be deprecated soon.
Please consider using drbdtop.
1:mysql/0Connected Secondary/Secondary UpToDate/UpToDate
# cat /proc/drbd
version: 8.4.10-1 (api:1/proto:86-101)
GIT-hash: a4d5de01fffd7e4cde48a080e2c686f9e8cebf4c build by mockbuild@, 2017-09-15 14:23:22
1: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
ns:0 nr:12 dw:12 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
You have new mail in /var/spool/mail/root
把node1升为主
#drbdadm primary mysql
# drbd-overview
NOTE: drbd-overview will be deprecated soon.
Please consider using drbdtop.
1:mysql/0Connected Primary/Secondary UpToDate/UpToDate
###node1已经升为主模式了
node2上:
# drbd-overview
NOTE: drbd-overview will be deprecated soon.
Please consider using drbdtop.
1:mysql/0Connected Secondary/Secondary UpToDate/UpToDate
# drbd-overview
NOTE: drbd-overview will be deprecated soon.
Please consider using drbdtop.
1:mysql/0Connected Secondary/Primary UpToDate/UpToDate
You have new mail in /var/spool/mail/root
##也可以看到node1升为主了
二、高可用实现drbd+mysql
1、我们在node1上增加drbd资源,以下操作都在node1上:
# crm
crm(live)# status
Stack: corosync
Current DC: node1 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 19:47:56 2017
Last change: Thu Oct 26 18:16:45 2017 by root via cibadmin on node1
2 nodes configured
0 resources configured
Online: [ node1 node2 ]
No resources ###没有资源
crm(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 ###添加drbd资源
在pacemaker定义克隆资源的专用属性:
Clone-max:最多克隆出的资源分数
Clone-node-max:在单个节点上运行积分克隆
notify:当一份克隆资源启动或者停止是否通知其他副本。
Globally-unique:每一分副本是否是否是唯一性(false,true)
Ordered:所有副本是否串行启动。
interleave:
Master-max:最多启动积分master资源
master-node-max:同一个节点最多运行积分master资源类型
drbd需要定义资源,现在我们来定义一下drbd
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# show
node 1: node1 \
attributes standby=off
node 2: node2
primitive mysqldrbd ocf:linbit:drbd \
params drbd_resource=mysql \
op start timeout=240 interval=0 \
op stop timeout=100 interval=0 \
op monitor role=Master interval=20 timeout=30 \
op monitor role=Slave interval=30 timeout=30
ms ms_mysqldrbd mysqldrbd \
meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
property cib-bootstrap-options: \
have-watchdog=false \
dc-version=1.1.16-12.el7_4.4-94ff4df \
cluster-infrastructure=corosync \
cluster-name=mycluster \
stonith-enabled=false \
migration-limit=1
crm(live)configure# commit ##提交
2、接下来我们要增加文件系统资源
crm(live)configure#primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd1 directory=/mnt fstype=xfs op start timeout=60 op stop timeout=60 指定drbd磁盘要挂载的目录
crm(live)configure#verify
3、给文件系统类型和drbd做亲缘性绑定(inf为证书为接近,当位负数时候为分离)。
crm(live)configure# colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master
crm(live)configure# verify
4、做顺序约束,当drbd起来之后才对文件系统进行绑定:
crm(live)configure# order mystore_after_ms_mysqldrbd mandatory: ms_mysqldrbd:promote mystore:start
crm(live)configure# verify
crm(live)configure# commit
配完后,我们查看一下当前的资源:
crm(live)# status
Stack: corosync
Current DC: node1 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 20:17:20 2017
Last change: Thu Oct 26 20:17:02 2017 by root via cibadmin on node1
2 nodes configured
3 resources configured
Online: [ node1 node2 ]
Full list of resources:
Master/Slave Set: ms_mysqldrbd
Masters: [ node1 ]
Slaves: [ node2 ]
mystore(ocf::heartbeat:Filesystem):Started node1
###可以发现drbd已经在node1上起来了
##查看下node1我们可以发现已经挂载了/mnt目录:
# df -h
Filesystem SizeUsed Avail Use% Mounted on
/dev/mapper/cl-root 18G2.5G 16G14% /
devtmpfs 226M 0226M 0% /dev
tmpfs 237M119M119M51% /dev/shm
tmpfs 237M 13M224M 6% /run
tmpfs 237M 0237M 0% /sys/fs/cgroup
/dev/sda1 1014M197M818M20% /boot
tmpfs 48M 0 48M 0% /run/user/0
/dev/drbd1 2.0G 62M2.0G 4% /mnt
5、接下来增加一下mysql资源,并作亲缘性绑定:
crm(live)configure# primitive mysqld systemd:mariadb op start timeout=100 op stop timeout=100
crm(live)configure# colocation mysqld_with_mystore inf: mysqld mystore
crm(live)configure# verify
crm(live)configure# commit
6、作顺序约束,先挂载文件系统,然后在启动msyqld资源
crm(live)configure# order mysqld_after_mystore mandatory: mystore mysqld
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# cd
crm(live)# status
Stack: corosync
Current DC: node1 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 20:26:39 2017
Last change: Thu Oct 26 20:26:29 2017 by root via cibadmin on node1
2 nodes configured
4 resources configured
Online: [ node1 node2 ]
Full list of resources:
Master/Slave Set: ms_mysqldrbd
Masters: [ node1 ]
Slaves: [ node2 ]
mystore(ocf::heartbeat:Filesystem):Started node1
mysqld(systemd:mariadb):Started node1
####到这里我们看到mysql已经启动了
进入数据库我们也可以,看到我们创建的数据库小张
# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (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 |
| mysql |
| performance_schema |
| test |
| xiaozhang |
+--------------------+
5 rows in set (0.03 sec)
7、我们要增加VIP资源,让它作为虚拟IP调度
# crm
crm(live)#configure
crm(live)configure#primitive myvip ocf:heartbeat:IPaddr params ip="172.25.0.100" op monitor interval=20 timeout=20 on-fail=restart ###添加172.25.0.100作为调度虚拟ip
crm(live)configure#colocation vip_with_ms_mysqldrbd inf: ms_mysqldrbd:Master myvip ##虚拟ip命名为myip
crm(live)configure#verify
crm(live)configure#commit
查看一下资源
crm(live)configure# cd
crm(live)# status
Stack: corosync
Current DC: node2 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 21:09:18 2017
Last change: Thu Oct 26 21:07:38 2017 by root via cibadmin on node1
2 nodes configured
5 resources configured
Online: [ node1 node2 ]
Full list of resources:
Master/Slave Set: ms_mysqldrbd
Masters: [ node1 ]
Slaves: [ node2 ]
mystore(ocf::heartbeat:Filesystem):Started node1
mysqld(systemd:mariadb):Started node1
myvip(ocf::heartbeat:IPaddr):Started node1
###可以发现三个服务都已经在node1上起来了
8、最后所有的工作都完成之后,我们测试一下vip:
先进入数据库我们要给maridb授权
####现在我们授权:
# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (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)]> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
现在我们用vip来访问以下
#mysql -uroot -p123456 -h 172.25.0.100
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (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 |
| mysql |
| performance_schema |
| test |
| xiaozhang |
+--------------------+
5 rows in set (0.00 sec)
##可以发现通过vip访问看到我们的数据库
9、接下来我们要测试drbd+mariadb的高可用,下面我们把node1设置为standby,然后在node2访问:
crm(live)# node standby
crm(live)# status
Stack: corosync
Current DC: node2 (version 1.1.16-12.el7_4.4-94ff4df) - partition with quorum
Last updated: Thu Oct 26 21:22:38 2017
Last change: Thu Oct 26 21:22:15 2017 by root via crm_attribute on node1
2 nodes configured
5 resources configured
Node node1: standby
Online: [ node2 ]
Full list of resources:
Master/Slave Set: ms_mysqldrbd
Masters: [ node2 ]
Stopped: [ node1 ]
mystore(ocf::heartbeat:Filesystem):Started node2
mysqld(systemd:mariadb):Started node2
myvip(ocf::heartbeat:IPaddr):Started node2
###查看一下状态,我们可以发现,所以服务已经切换到node2上面去了
# ip addr
1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:64:00:b1 brd ff:ff:ff:ff:ff:ff
inet 172.25.0.30/24 brd 172.25.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 172.25.0.100/24 brd 172.25.0.255 scope global secondary ens33
node2上也可以看到虚拟ip了
接下来我们要访问以下虚拟ip,看看是否可以访问到数据库
#mysql -uroot -p123456 -h 172.25.0.100
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (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 |
| mysql |
| performance_schema |
| test |
| xiaozhang |
+--------------------+
5 rows in set (0.08 sec)
###我们发现vipy已经漂移过来,数据也漂移了过来,证明我们的drbd+mariadb高可用已经实现了。赞一个。
页:
[1]