RHEL6.6-x86-64
drbd原理详解请参考我的另一篇文章http://mingxiao.blog.51cto.com/8124243/1655765
node1 192.168.1.7
node2 192.168.1.8
双机互信
进192.168.1.7 # ssh-keygen -t rsa ~/.ssh/id_rsa # ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 进192.168.1.8 # ssh-keygen -t rsa ~/.ssh/id_rsa # ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
编辑/etc/hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.7 node1.mingxiao.info node1 192.168.1.8 node2.mingxiao.info node2
# ssh /etc/hosts node2:/etc/
时间同步 # ntpdate time.windows.com # ssh node2 'ntpdate time.windows.com' # crontab -e */5 * * * * /usr/sbin/ntpdate time.windows.com && /dev/null
配置corosync
# yum install corosync pacemaker
# rpm -ivh crmsh-2.1-1.6.x86_64.rpm
编辑/etc/corosync/corosync.conf
# Please read the corosync.conf.5 manual page compatibility: whitetank totem { version: 2 secauth: off threads: 0 interface { ringnumber: 0 bindnetaddr: 192.168.1.0 mcastaddr: 239.255.1.1 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: AMF debug: off } } service { ver: 0 name: pacemaker } sisexec { user: root group: root }
# scp /etc/corosync/corosync.conf node2:/etc/corosync/
# /etc/init.d/corosync start
# ssh node2 '/etc/init.d/corosync start'
请确保corosync启动正常
# crm status
Last updated: Sun May 3 18:53:33 2015 Last change: Sun May 3 12:53:05 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 0 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ]
crm(live)configure# property no-quorum-policy=ignore crm(live)configure# property stonith-enabled=false
配置drbd
# yum install drbd84-utils kmod-drbd84 # ssh node2 'yum -y install drbd84-utils kmod-drbd84'
node1和node2创建两个一样的分区,用fdisk完成,这里为sdb1,分区过程不再说明。
编辑/etc/drbd.d/global_common.conf
global { usage-count no; } common { handlers { pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f"; } startup { # wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb } options { # cpu-mask on-no-data-accessible } disk { on-io-error detach; } net { cram-hmac-alg "sha1"; shared-secret "mydrbdlab"; } syncer { rate 200M; } }
编辑/etc/drbd.d/drbd.res
resource mydrbd { device /dev/drbd0; disk /dev/sdb1; meta-disk internal; on node1.mingxiao.info { address 192.168.1.7:7789; } on node2.mingxiao.info { address 192.168.1.8:7789; } }
# drbdadm create-md mydrbd # ssh node2 'drbdadm create-md mydrbd'
node1和node2节点执行
# /etc/init.d/drbd start
node1
# drbd-overview 0:mydrbd/0 Connected Secondary/Secondary UpToDate/UpToDate
将node1提升为主节点
# drbdadm -- --overwrite-data-of-peer primary mydrbd
# mkfs.ext4 /dev/drbd0 # mkdir /mydata # ssh node2 'mkdir /mydata # mount /dev/drbd0 /mydata # drbd-overview 0:mydrbd/0 Connected Primary/Secondary UpToDate/UpToDate # umount /mydata # drbdadm secondary mydrbd
配置drbd资源和主从属性
crm(live)configure# primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mydrbd \ op start interval=0 timeout=240 \ op stop interval=0 timeout=100 \ crm(live)configure# ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
查看状态,可看到主节点是node1,从节点是node2
crm(live)# status Last updated: Sun May 3 19:55:32 2015 Last change: Sun May 3 19:55:19 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 2 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ] Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ node1.mingxiao.info ] Slaves: [ node2.mingxiao.info ]
配置文件系统资源
crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext4 op start timeout=60 op stop timeout=60 crm(live)configure# colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master 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 Last updated: Sun May 3 19:59:13 2015 Last change: Sun May 3 19:58:54 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 3 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ] Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ node1.mingxiao.info ] Slaves: [ node2.mingxiao.info ] mystore(ocf::heartbeat:Filesystem):Started node1.mingxiao.info
node1初始化mysql
# tar xf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz -C /usr/local/mysql
# groupadd -r -g 306 mysql # useradd -g 306 -r -u 306 mysql # mkdir /mydata/data # chown -R mysql.mysql /mydata/data # chown -R mysql.mysql /usr/local/mysql
# cd /usr/local/mysql # scripts/mysql_install_db --user=mysql --datadir=/mydata/data # chown -R root /usr/local/mysql/*
提供SysV风格服务脚本
# cp support-files/mysql.server /etc/init.d/mysqld
提供MySQL配置文件
# cp my.cnf /etc/my.cnf # vim /etc/my.cnf basedir = /usr/local/mysql datadir = /mydata/data port = 3306 sock=/var/lib/mysql/mysql.sock
编辑/etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
编辑/etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# su
man帮助文档,编辑/etc/man.config
MANPATH /usr/local/mysql/man
头文件
ln -sv /usr/local/mysql/include/ /usr/include/mysql
mysql> use mysql; mysql> GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'xiaoming'; mysql> CREATE DATABASE xiaoming; mysql> show database; mysql> show DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | xiaoming | +--------------------+ 5 rows in set (0.00 sec)
将node1转为备用节点,可看到node2变为主节点。
# crm node standby # crm node online # crm status Last updated: Sun May 3 20:09:58 2015 Last change: Sun May 3 20:09:56 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 3 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ] Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ node2.mingxiao.info ] Slaves: [ node1.mingxiao.info ] mystore(ocf::heartbeat:Filesystem):Started node2.mingxiao.info
node2配置mysql
# groupadd -r -g 306 mysql # useradd -g 306 -r -u 306 mysql # tar xf mysql-5.6.14-linux-glibc2.5-x86_64 /usr/local/mysql # cd /usr/local/mysql # chown -R root:mysql ./
提供SysV风格服务脚本
# cp support-files/mysql.server /etc/init.d/mysqld
提供MySQL配置文件
# cp my.cnf /etc/my.cnf # vim /etc/my.cnf basedir = /usr/local/mysql datadir = /mydata/data port = 3306 sock=/var/lib/mysql/mysql.sock
编辑/etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
编辑/etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# su
man帮助文档,编辑/etc/man.config
MANPATH /usr/local/mysql/man
头文件
ln -sv /usr/local/mysql/include/ /usr/include/mysql
由于node1已经初始化过mysql,这里不用重新初始化,可以直接启动,可看到在node1创建的数据库仍存在。
# service mysqld start Starting MySQL.. SUCCESS! mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | xiaoming | +--------------------+ 5 rows in set (0.02 sec)
配置mysqld资源:
crm(live)configure# primitive mysqld lsb:mysqld crm(live)configure# colocation mysqld_with_mystore inf: mysqld mystore 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 Last updated: Sun May 3 20:19:41 2015 Last change: Sun May 3 20:19:34 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 4 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ] Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ node2.mingxiao.info ] Slaves: [ node1.mingxiao.info ] mystore(ocf::heartbeat:Filesystem):Started node2.mingxiao.info mysqld(lsb:mysqld):Started node2.mingxiao.info
配置IP,从此IP访问可连接至mysql
crm(live)configure# primitive myip IPaddr2 params ip=192.168.1.20 nic=eth1 cidr_netmask=24 crm(live)configure# colocation myip_with_mysqld inf: mysqld myip crm(live)# status Last updated: Sun May 3 20:36:26 2015 Last change: Sun May 3 20:36:23 2015 Stack: classic openais (with plugin) Current DC: node2.mingxiao.info - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 5 Resources configured Online: [ node1.mingxiao.info node2.mingxiao.info ] Master/Slave Set: ms_mysqldrbd [mysqldrbd] Masters: [ node2.mingxiao.info ] Slaves: [ node1.mingxiao.info ] mystore(ocf::heartbeat:Filesystem):Started node2.mingxiao.info mysqld(lsb:mysqld):Started node2.mingxiao.info myip(ocf::heartbeat:IPaddr2):Started node2.mingxiao.info
一切都已配置完毕
node backup.mingxiao.info \ attributes standby=off node master.mingxiao.info \ attributes standby=off primitive myip IPaddr2 \ params ip=192.168.1.22 nic=eth1 cidr_netmask=24 primitive mysqld lsb:mysqld \ meta target-role=Started primitive mysqldrbd ocf:linbit:drbd \ params drbd_resource=mydrbd \ op start interval=0 timeout=240 \ op stop interval=0 timeout=100 primitive mystore Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype=ext4 \ op start timeout=60 interval=0 \ op stop timeout=60 interval=0 \ meta target-role=Started ms ms_mysqldrbd mysqldrbd \ meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true colocation myip_with_ms_mysqlddrbd inf: ms_mysqldrbd:Master myip colocation mysqld_with_mystore inf: mysqld mystore colocation mystore_with_ms_mysqldrbd inf: mystore ms_mysqldrbd:Master order mysqld_after_mystore Mandatory: mystore mysqld order mystore_after_ms_mysqldrbd Mandatory: ms_mysqldrbd:promote mystore:start property cib-bootstrap-options: \ dc-version=1.1.11-97629de \ cluster-infrastructure="classic openais (with plugin)" \ expected-quorum-votes=2 \ no-quorum-policy=ignore \ stonith-enabled=false #vim:set syntax=pcmk
脑裂处理办法:
将Node1设置为主节点并挂载测试
[root@node1 ~]# drbdadm primary mydrbd
[root@node1 ~]# mount /dev/drbd0 /mydata
将Node2设置为从节点并丢弃资源数据
[root@node2 ~]# drbdadm secondary mydrbd
[root@node2 ~]# drbdadm -- --discard-my-data connect mydrbd
在Node1主节点上手动连接资源
[root@node1 ~]# drbdadm connect mydrbd