天天看點

IV 11 MySQL+corosync+drbd

操作:

在《drbd》基礎上進行操作

(1)将drbd配置為corosync高可用的資源,能實作主從角色的自動切換

注意:作為高可用的資源,必須要接受CRM管理,不能開機自啟動

node2-side:

 [root@node2~]# drbd-overview

 0:mydrbd  Connected Primary/Secondary UpToDate/UpToDate C r----- /mydata ext3 2.0G 36M 1.9G 2%

[root@node2 ~]# umount /mydata(注意,切換角色前要先解除安裝FS)

[root@node2 ~]# drbdadm secondary mydrbd

[root@node2 ~]# drbd-overview

 0:mydrbd  Connected Secondary/Secondary UpToDate/UpToDate C r-----

[root@node2 ~]# service drbd stop

[root@node2 ~]# ssh node1 'service drbd stop'

[root@node2 ~]# chkconfig drbd off

[root@node2 ~]# chkconfig --list drbd

drbd              0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@node2 ~]# ssh node1 'chkconfig drbd off'

[root@node2 ~]# ssh node1 'chkconfig --list drbd'

準備一台機器作為跳闆機(node3上分别要配置與node1、node2的雙機互信,node1和node2準備好yum源)

node3-side:

[root@node3 ~]# ll /root/corosync | awk'{print $9}'

cluster-glue-1.0.6-1.6.el5.i386.rpm

cluster-glue-libs-1.0.6-1.6.el5.i386.rpm

corosync-1.2.7-1.1.el5.i386.rpm

corosynclib-1.2.7-1.1.el5.i386.rpm

heartbeat-3.0.3-2.3.el5.i386.rpm

heartbeat-libs-3.0.3-2.3.el5.i386.rpm

libesmtp-1.0.4-5.el5.i386.rpm

pacemaker-1.0.12-1.el5.centos.i386.rpm

pacemaker-libs-1.0.12-1.el5.centos.i386.rpm

resource-agents-1.0.3-2.6.el5.i386.rpm

[root@node3 ~]# for I in {1..2};do ssh node$I 'mkdir /root/corosync';scp /root/corosync/* node$I:/root/corosync/;ssh node$I 'yum -y --nogpgcheck localinstall /root/corosync/*.rpm';done

node1-side:

 [root@node1~]# cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf

[root@node1 ~]# vim /etc/corosync/corosync.conf

compatibility: whitetank

totem {

       version: 2

        secauth: on

        threads: 2

       interface {

                ringnumber: 0

                bindnetaddr: 192.168.41.0

                mcastaddr: 226.94.1.8

                mcastport: 5405

       }

}

logging {

       fileline: off

       to_stderr: no

        to_logfile: yes

        to_syslog: no

        logfile: /var/log/cluster/corosync.log

       debug: off

       timestamp: on

       logger_subsys {

                subsys: AMF

                debug: off

amf {

       mode: disabled

service {

        ver: 0

        name: pacemaker

aisexec {

       user: root

       group: root

[root@node1 ~]# mkdir /var/log/cluster

[root@node1 ~]# ssh node2 'mkdir/var/log/cluster'

[root@node1 ~]# corosync-keygen

[root@node1 ~]# scp -p /etc/corosync/{authkey,corosync.conf} node2:/etc/corosync

[root@node1 ~]# service corosync start

Starting Corosync Cluster Engine (corosync):               [  OK  ]

[root@node1 ~]# ssh node2 'service corosync start'

Starting Corosync Cluster Engine(corosync): [  OK  ]

[root@node1 ~]# vim .bashrc

alias grep='grep --color=auto'(添加此行)

[root@node1 ~]# . .bashrc

[root@node1 ~]# grep -e "CorosyncCluster Engine" -e "configuration file"/var/log/cluster/corosync.log

[root@node1 ~]# grep TOTEM !$

[root@node1 ~]# grep ERROR: !$(有關于STONITH報錯資訊,下文會在crm互動模式下配置)

[root@node1 ~]# grep pcmk_startup !$

[root@node1 ~]# crm configure

INFO: building help index

crm(live)configure# (在此模式下按兩下<TAB>鍵會有提示)

crm(live)configure# verify(是STONITH的報錯)

crm(live)configure# property stonith-enabled=false(在property後按兩下<TAB>會有提示,同樣在等号處按兩下<TAB>也有提示)

crm(live)configure# verify

crm(live)configure# property no-quorum-policy=ignore(無法定票數的政策)

crm(live)configure# rsc_defaults resource-stickiness=100(資源粘性)

crm(live)configure# verify(無報錯資訊就能送出了)

crm(live)configure# commit

注意:drbd配置為高可用資源,有兩項(primitive主資源;master/slave類型的clone類資源)

crm(live)configure# cd

crm(live)# ra

crm(live)ra# providers drbd

heartbeat linbit

crm(live)ra# meta drbd

crm(live)ra# meta ocf:linbit:drbd(檢視parameters有哪些,還有operations’s defaults(advisory minimum),注意不能小于建議的最小值)

crm(live)ra# meta ocf:heartbeat:drbd

crm(live)ra# up

crm(live)# configure

crm(live)configure# help primitive

crm(live)configure# primitive mysqldrbd ocf:heartbeat:drbd params drbd_resource=mydrbd op start timeout=240 op stop timeout=100 op monitor role=Master interval=10 timeout=20 op monitor role=Slave  interval=20 timeout=20

crm(live)configure# help ms(The `ms`command creates a master/slave resource type)

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# edit(如果有問題可通過edit編輯,删除或修改某行,儲存後重新定義等)

crm(live)configure# show

crm(live)configure# bye

bye

[root@node1 ~]# crm status

……

Online: [ node1.magedu.com node2.magedu.com]

Master/Slave Set: ms_mysqldrbd

    Masters: [ node1.magedu.com ]

    Slaves: [ node2.magedu.com ]

[root@node1 ~]# crm node standby

Online: [ node2.magedu.com ]

    Masters: [ node2.magedu.com ]

    Stopped: [ mysqldrbd:0 ]

[root@node1 ~]# drbd-overview

 0:mydrbd  Unconfigured . . . .

[root@node1 ~]# ssh node2 'drbd-overview'

 0:mydrbd  WFConnection Primary/Unknown UpToDate/DUnknown C r-----

[root@node1 ~]# crm node online

 0:mydrbd  Connected Secondary/Primary UpToDate/UpToDate C r-----

注:master-max(how many copies of the resource can be promoted to masterstatus.defaults to 1)最多多少個主資源,不是雙主模型,要麼主從要麼從從,是以最多1個主資源

master-node-max(how manycopies of the resource can be promoted to master status on a single node.defaultsto 1)最多1個主節點

clone-max(how many copies ofthe resource to start.defaults to the number of nodes in the cluster)主和從都是克隆資源,是以最多兩個

clone-node-max(how manycopies of the resource can be started on a single node.default 1)在某一個節點上最多啟動多少個克隆資源

notify(when stopping orstarting a copy of the clone,tell all the other copies beforehand and when theaction was successful.allowed values:false,ture)

注:crm(live)configure# show xml(在檢視xml配置檔案時,注意rsc和with-rsc)

rsc(the colocationsource.if the constraint cannot be satisfied,the cluster may decide not toallow the resource to run at all)

with-rsc(the colocationtarget.the cluster will decide where to put this resource first and then decidewhere to put the resource in the rsc field)

以上隻定義了drbd作為高可用資源(兩個node主從角色的轉換),要想使用還要挂載

(2)将自動挂載FS這個功能添加為高可用資源

crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext3 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(如果出問題,清理兩個node狀态crm(live)node#clearstate node1.magedu.com,crm(live)node# clearstate node2.magedu.com;清理資源狀态crm(live)resource#cleanup mystore,crm(live)resource# cleanup ms_mysqldrbd;然後兩個node重新開機服務)

2 Nodes configured, 2 expected votes

2 Resources configured.

============

 mystore (ocf::heartbeat:Filesystem):   Started node1.magedu.com

    Slaves: [ node1.magedu.com ]

 mystore (ocf::heartbeat:Filesystem):   Started node2.magedu.com

(3)将mysql服務加入高可用資源

以下幾步兩個node都要執行(建立使用者群組,一定要注意兩個節點的UID要相同、GID要相同,因為通路的是一個共享存儲;解壓;建立軟連結;改安裝目錄下的屬主屬組;複制配置檔案和啟動腳本)

[root@node2 ~]# groupadd -g 3306 mysql

[root@node2 ~]# useradd -u 3306 -g mysql -s/sbin/nologin -M mysql

[root@node2 ~]# tar xf mysql-5.5.45-linux2.6-i686.tar.gz -C /usr/local/

[root@node2 ~]# cd /usr/local/

[root@node2 local]# ln -sv mysql-5.5.45-linux2.6-i686/ mysql

create symbolic link `mysql' to`mysql-5.5.45-linux2.6-i686/'

[root@node2 ~]# cd mysql

[root@node2 mysql]# chown -R root.mysql ./

[root@node2 mysql]# cp support-files/my-large.cnf /etc/my.cnf

[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@node2 mysql]# vim /etc/my.cnf

[mysqld]

datadir = /mydata/data

innodb_file_per_table = 1

node2-side(注意在目前master節點上初始化,由#crm  status得知目前主節點是node2):

[root@node2 ~]# mkdir /mydata/data

[root@node2 ~]# chown -R mysql.mysql /mydata/data

[root@node2 ~]# ll /mydata

total 32

drwxr-xr-x 2 mysql mysql  4096 Nov 6 21:09 data

-rw-r--r-- 1 root  root    74 Nov  6 17:11 issue

drwx------ 2 root  root 16384 Nov  6 17:10 lost+found

[root@node2 ~]# cd /usr/local/mysql

[root@node2 mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data

[root@node2 mysql]# service mysqld start

Starting MySQL....                                         [  OK  ]

[root@node2 mysql]# bin/mysql

mysql> CREATE DATABASE mydb;

Query OK, 1 row affected (0.02 sec)

mysql> SHOW DATABASES;

+--------------------+

| Database           |

| information_schema |

| mydb               |

mysql> \q

Bye

[root@node2 mysql]# service mysqld stop

Shutting down MySQL.                                       [  OK  ]

[root@node2 mysql]# chkconfig mysqld off

[root@node2 mysql]# chkconfig --list mysqld

mysqld            0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@node2 ~]# crm node standby(将主node切換為node1)

[root@node2 ~]# crm node online

[root@node2 ~]# crm status

[root@node1 mysql]# service mysqld start

[root@node1 mysql]# bin/mysql

mysql> SHOW DATABASES;(檢視有無node2建立的庫mydb)

[root@node1 mysql]# service mysqld stop

[root@node1 mysql]# chkconfig mysqld off

[root@node1 mysql]# chkconfig --list mysqld

[root@node1 mysql]# crm configure

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# show xml(注意這段資訊<rsc_order first="mystore" id="mysqld_after_mystore" score="INFINITY" then="mysqld"/>)

crm(live)configure# exit

[root@node1 mysql]# crm status

3 Resources configured.

 mysqld   (lsb:mysqld):    Started node1.magedu.com

mysql> DROP DATABASES mydb;

mysql> CREATE DATABASE hellodrbd;

(4)将VIP加入高可用資源

[root@node2 mysql]# crm status

| hellodrbd          |

mysql> GRANT  ALL  ON  *.* TO  'root'@'%’ IDENTIFIED BY  'redhat';

mysql> FLUSH PRIVILEGES;

[root@node2 mysql]# crm configure

crm(live)configure# primitive myip ocf:heartbeat:IPaddr params ip=192.168.41.222 nic=eth0 cidr_netmask=24

crm(live)configure# colocation myip_with_ms_mysqldrbd inf: myip ms_mysqldrbd:Master

crm(live)configure# verify(若有問題,清理節點,清理各資源,重新開機服務)

node node1.magedu.com \

         attributes standby="off"

node node2.magedu.com \

primitive myip ocf:heartbeat:IPaddr \

         params ip="192.168.41.222" nic="eth0" cidr_netmask="24"

primitive mysqld lsb:mysqld

primitive mysqldrbd ocf:heartbeat:drbd \

         params drbd_resource="mydrbd" \

         op start interval="0" timeout="240" \

         op stop interval="0" timeout="100" \

         op monitor interval="10" role="Master" timeout="20"\

         op monitor interval="20" role="Slave" timeout="20"

primitive mystore ocf:heartbeat:Filesystem\

         params device="/dev/drbd0" directory="/mydata"fstype="ext3" \

         op start interval="0" timeout="60" \

         op stop interval="0" timeout="60"

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_mysqldrbd inf: myipms_mysqldrbd:Master

colocation mysqld_with_mystore inf: mysqldmystore

colocation mystore_with_ms_mysqldrbd inf:mystore ms_mysqldrbd:Master

order mysqld_after_mystore inf: mystoremysqld

order mystore_after_ms_mysqldrbd inf:ms_mysqldrbd:promote mystore:start

property $id="cib-bootstrap-options"\

         dc-version="1.0.12-unknown"\

         cluster-infrastructure="openais"\

         expected-quorum-votes="2"\

         stonith-enabled="false"\

         no-quorum-policy="ignore"\

         last-lrm-refresh="1446895492"

rsc_defaults $id="rsc-options" \

         resource-stickiness="100"

4 Resources configured.

 mysqld   (lsb:mysqld):    Started node2.magedu.com

 myip       (ocf::heartbeat:IPaddr):  Started node2.magedu.com

[root@node3 ~]# mysql -uroot -p-h192.168.41.222(檢視庫hellodrbd)

Enter password:

| mysql              |

| performance_schema |

| test               |

5 rows in set (0.05 sec)

[root@node2 mysql]# crm node standby

[root@node2 mysql]# crm node online

 myip       (ocf::heartbeat:IPaddr):  Started node1.magedu.com

[root@node3 ~]# mysql -u root -p -h192.168.41.222(節點間切換後,對外依然可以通路到同樣的庫)

5 rows in set (0.03 sec)

本文轉自 chaijowin 51CTO部落格,原文連結:http://blog.51cto.com/jowin/1720102,如需轉載請自行聯系原作者