大纲
一、系统环境及所需软件包
二、高可用环境准备工作
三、DRBD的安装与基本配置
四、MySQL的安装与基本配置
五、Corosync的安装与基本配置
六、基于crm配置资源
一、系统环境及所需软件包
系统环境
CentOS5.8 x86_64
node1.network.com node1 172.16.1.101
node2.network.com node2 172.16.1.105
软件包
drbd83-8.3.15-2.el5.centos.x86_64.rpm
kmod-drbd83-8.3.15-3.el5.centos.x86_64.rpm
mysql-5.5.28-linux2.6-x86_64.tar.gz
corosync-1.2.7-1.1.el5.x86_64.rpm
pacemaker-1.0.12-1.el5.centos.x86_64.rpm
拓扑图
二、高可用环境准备工作
1、时间同步
[root@node1 ~]# ntpdate s2c.time.edu.cn[root@node2 ~]# ntpdate s2c.time.edu.cn可根据需要在每个节点上定义crontab任务[root@node1 ~]# which ntpdate/sbin/ntpdate[root@node1 ~]# echo "*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null" >> /var/spool/cron/root [root@node1 ~]# crontab -l*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null
2、主机名称要与uname -n保持一致,并通过/etc/hosts解析
node1[root@node1 ~]# hostname node1.network.com[root@node1 ~]# uname -nnode1.network.com[root@node1 ~]# sed -i 's@\(HOSTNAME=\).*@\1node1.network.com@g' /etc/sysconfig/networknode2[root@node2 ~]# hostname node2.network.com[root@node2 ~]# uname -nnode2.network.com[root@node2 ~]# sed -i 's@\(HOSTNAME=\).*@\1node2.network.com@g' /etc/sysconfig/networknode1添加hosts解析[root@node1 ~]# vim /etc/hosts[root@node1 ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.16.1.101 node1.network.com node1172.16.1.105 node2.network.com node2拷贝此hosts文件至node2[root@node1 ~]# vim /etc/hosts[root@node1 ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.16.1.101 node1.network.com node1172.16.1.105 node2.network.com node2[root@node1 ~]# scp /etc/hosts node2:/etc/The authenticity of host 'node2 (172.16.1.105)' can't be established.RSA key fingerprint is 13:42:92:7b:ff:61:d8:f3:7c:97:5f:22:f6:71:b3:24.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'node2,172.16.1.105' (RSA) to the list of known hosts.root@node2's password: hosts 100% 233 0.2KB/s 00:00
3、ssh互信通信
node1[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''Generating public/private rsa key pair./root/.ssh/id_rsa already exists.Overwrite (y/n)? n # 我这里已经生成过了[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub node2root@node2's password: Now try logging into the machine, with "ssh 'node2'", and check in: .ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@node1 ~]# setenforce 0[root@node1 ~]# ssh node2 'ifconfig'eth0 Link encap:Ethernet HWaddr 00:0C:29:D6:03:52 inet addr:172.16.1.105 Bcast:255.255.255.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fed6:352/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9881 errors:0 dropped:0 overruns:0 frame:0 TX packets:11220 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5898514 (5.6 MiB) TX bytes:1850217 (1.7 MiB)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1112 (1.0 KiB) TX bytes:1112 (1.0 KiB) 同理node2也需要做同样的双击互信,一样的操作,此处不再演示
4、关闭iptables和selinux
node1
[root@node1 ~]# service iptables stop[root@node1 ~]# vim /etc/sysconfig/selinux [root@node1 ~]# cat /etc/sysconfig/selinux# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - SELinux is fully disabled.#SELINUX=permissiveSELINUX=disabled# SELINUXTYPE= type of policy in use. Possible values are:# targeted - Only targeted network daemons are protected.# strict - Full SELinux protection.SELINUXTYPE=targeted
node2
[root@node2 ~]# service iptables stop[root@node2 ~]# vim /etc/sysconfig/selinux [root@node2 ~]# cat /etc/sysconfig/selinux# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - SELinux is fully disabled.#SELINUX=permissiveSELINUX=disabled# SELINUXTYPE= type of policy in use. Possible values are:# targeted - Only targeted network daemons are protected.# strict - Full SELinux protection.SELINUXTYPE=targeted
三、DRBD的安装与基本配置
1、配置163源(也可以自行下载两个软件包,我这里直接使用yum源)
node1
[root@node1 ~]# wget http://mirrors.163.com/.help/CentOS5-Base-163.repo[root@node1 ~]# yum repolist Loaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfile * epel: mirrors.hustunique.comrepo id repo name statusaddons CentOS-5 - Addons - 163.com 0base CentOS-5 - Base - 163.com 3,667epel Extra Packages for Enterprise Linux 5 - x86_64 6,755extras CentOS-5 - Extras - 163.com 266updates CentOS-5 - Updates - 163.com 593repolist: 11,281
node2
[root@node2 ~]# wget http://mirrors.163.com/.help/CentOS5-Base-163.repo[root@node1 ~]# yum repolist Loaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfile * epel: mirrors.hustunique.comrepo id repo name statusaddons CentOS-5 - Addons - 163.com 0base CentOS-5 - Base - 163.com 3,667epel Extra Packages for Enterprise Linux 5 - x86_64 6,755extras CentOS-5 - Extras - 163.com 266updates CentOS-5 - Updates - 163.com 593repolist: 11,281
2、安装drbd与kmod-drbd
node1[root@node1 ~]# yum install -y drbd83 kmod-drbd83node2[root@node2 ~]# yum install -y drbd83 kmod-drbd83
3、准备分区作为drbd设备
node1[root@node1 ~]# fdisk /dev/hdaThe number of cylinders for this disk is set to 44384.There is nothing wrong with that, but this is larger than 1024,and could in certain setups cause problems with:1) software that runs at boot time (e.g., old versions of LILO)2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): nCommand action e extended p primary partition (1-4)pPartition number (1-4): 1First cylinder (1-44384, default 1): Using default value 1Last cylinder or +size or +sizeM or +sizeK (1-44384, default 44384): +1GCommand (m for help): pDisk /dev/hda: 21.4 GB, 21474836480 bytes15 heads, 63 sectors/track, 44384 cylindersUnits = cylinders of 945 * 512 = 483840 bytes Device Boot Start End Blocks Id System/dev/hda1 1 2068 977098+ 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@node1 ~]# partprobe /dev/hda[root@node1 ~]# cat /proc/partitions major minor #blocks name 3 0 20971520 hda 3 1 977098 hda1 8 0 20971520 sda 8 1 104391 sda1 8 2 20860402 sda2 253 0 18776064 dm-0 253 1 2064384 dm-1 node2[root@node2 ~]# fdisk /dev/hda Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel. Changes will remain in memory only,until you decide to write them. After that, of course, the previouscontent won't be recoverable.The number of cylinders for this disk is set to 44384.There is nothing wrong with that, but this is larger than 1024,and could in certain setups cause problems with:1) software that runs at boot time (e.g., old versions of LILO)2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)Command (m for help): nCommand action e extended p primary partition (1-4)pPartition number (1-4): 1First cylinder (1-44384, default 1): Using default value 1Last cylinder or +size or +sizeM or +sizeK (1-44384, default 44384): +1GCommand (m for help): pDisk /dev/hda: 21.4 GB, 21474836480 bytes15 heads, 63 sectors/track, 44384 cylindersUnits = cylinders of 945 * 512 = 483840 bytes Device Boot Start End Blocks Id System/dev/hda1 1 2068 977098+ 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@node2 ~]# partprobe /dev/hda[root@node2 ~]# cat /proc/partitions major minor #blocks name 3 0 20971520 hda 3 1 977098 hda1 8 0 20971520 sda 8 1 104391 sda1 8 2 20860402 sda2 253 0 18776064 dm-0 253 1 2064384 dm-1
4、编辑主配置文件
[root@node1 ~]# cat /etc/drbd.conf ## please have a a look at the example configuration file in# /usr/share/doc/drbd83/drbd.conf#[root@node1 ~]# cp /usr/share/doc/drbd83-8.3.15/drbd.conf /etc/drbd.conf cp: overwrite `/etc/drbd.conf'? y[root@node1 ~]# cat /etc/drbd.conf # You can find an example in /usr/share/doc/drbd.../drbd.conf.exampleinclude "drbd.d/global_common.conf";include "drbd.d/*.res";编辑全局属性定义配置文件[root@node1 ~]# vim /etc/drbd.d/global_common.conf [root@node1 ~]# cat /etc/drbd.d/global_common.confglobal { usage-count no; # 关闭用户数量统计 # minor-count dialog-refresh disable-ip-verification}common { protocol C; handlers { # These are EXAMPLE handlers only. # They may have severe implications, # like hard resetting the node under certain circumstances. # Be careful when chosing your poison. 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"; # fence-peer "/usr/lib/drbd/crm-fence-peer.sh"; # split-brain "/usr/lib/drbd/notify-split-brain.sh root"; # out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root"; # before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k"; # after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh; } startup { # wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb } disk { on-io-error detach; # 一旦io发生错误,就拆掉磁盘 # on-io-error fencing use-bmbv no-disk-barrier no-disk-flushes # no-disk-drain no-md-flushes max-bio-bvecs } net { cram-hmac-alg "sha1"; # 指定加密算法 shared-secret "qYQ1cwOFC6E="; # 指定共享的密钥 # sndbuf-size rcvbuf-size timeout connect-int ping-int ping-timeout max-buffers # max-epoch-size ko-count allow-two-primaries cram-hmac-alg shared-secret # after-sb-0pri after-sb-1pri after-sb-2pri data-integrity-alg no-tcp-cork } syncer { rate 300M; # 指定速率 # rate after al-extents use-rle cpu-mask verify-alg csums-alg }}
5、定义一个资源文件/etc/drbd.d/mysql.res
[root@node1 ~]# vim /etc/drbd.d/mysql.res[root@node1 ~]# cat /etc/drbd.d/mysql.resresource mysql { on node1.network.com { device /dev/drbd0; disk /dev/hda1; address 172.16.1.101:7789; meta-disk internal; } on node2.network.com { device /dev/drbd0; disk /dev/hda1; address 172.16.1.105:7789; meta-disk internal; }}将配置文件与资源文件拷贝至node2节点[root@node1 ~]# scp -r /etc/drbd.* node2:/etc/drbd.conf 100% 133 0.1KB/s 00:00 global_common.conf 100% 1688 1.7KB/s 00:00 mysql.res 100% 292 0.3KB/s 00:00
6、在两个节点上初始化已定义的资源并启动服务
初始化资源,两个节点同时执行[root@node1 ~]# drbdadm create-md mysqlWriting meta data...initializing activity logNOT initialized bitmapNew drbd meta data block successfully created.[root@node2 ~]# drbdadm create-md mysqlWriting meta data...initializing activity logNOT initialized bitmapNew drbd meta data block successfully created.启动drbd服务,两个节点同时执行[root@node1 ~]# service drbd startStarting DRBD resources: [ d(mysql) s(mysql) n(mysql) ]......[root@node2 ~]# service drbd startStarting DRBD resources: [ d(mysql) s(mysql) n(mysql) ].查看启动状态[root@node1 ~]# cat /proc/drbd version: 8.3.15 (api:88/proto:86-97)GIT-hash: 0ce4d235fc02b5c53c1c52c53433d11a694eab8c build by mockbuild@builder10.centos.org, 2013-03-27 16:01:26 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----- ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:977028也可以使用drbd-overview命令来查看[root@node1 ~]# drbd-overview 0:mysql Connected Secondary/Secondary Inconsistent/Inconsistent C r----- 从上面的信息中可以看出此时两个节点均处于Secondary状态。于是,我们接下来需要将其中一个节点设置为Primary。在要设置为Primary的节点上执行如下命令[root@node1 ~]# drbdadm -- --overwrite-data-of-peer primary mysql而后再次查看状态,可以发现数据同步过程已经开始[root@node1 ~]# cat /proc/drbd version: 8.3.15 (api:88/proto:86-97)GIT-hash: 0ce4d235fc02b5c53c1c52c53433d11a694eab8c build by mockbuild@builder10.centos.org, 2013-03-27 16:01:26 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n- ns:169252 nr:0 dw:0 dr:177024 al:0 bm:10 lo:4 pe:12 ua:64 ap:0 ep:1 wo:b oos:809220 [==>.................] sync'ed: 17.6% (809220/977028)K finish: 0:00:09 speed: 83,904 (83,904) K/sec 等数据同步完成以后再次查看状态,可以发现节点已经处于实时状态,且节点已经有了主次[root@node1 ~]# drbd-overview # Primary/Secondary表示当前节点为主,对等节点为从 0:mysql Connected Primary/Secondary UpToDate/UpToDate C r-----
7、创建文件系统并挂载
文件系统的挂载只能在Primary节点进行,因此,也只有在设置了主节点后才能对drbd设备进行格式化[root@node1 ~]# mke2fs -j /dev/drbd0 mke2fs 1.39 (29-May-2006)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)122368 inodes, 244257 blocks12212 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2516582408 block groups32768 blocks per group, 32768 fragments per group15296 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 25 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.创建挂载目录[root@node1 ~]# mkdir /mydata挂载drbd设备,注意,只有主节点才能挂载,当前node1节点是主节点[root@node1 ~]# mount /dev/drbd0 /mydata/[root@node1 ~]# ls /mydata/lost+found
四、MySQL的安装与基本配置
1、两节点上创建mysql用户
node1
[root@node1 ~]# groupadd -g 3306 mysql[root@node1 ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M -d /mydata/data mysql[root@node1 ~]# id mysqluid=3306(mysql) gid=3306(mysql) groups=3306(mysql)
node2
[root@node1 ~]# groupadd -g 3306 mysql[root@node1 ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M -d /mydata/data mysql[root@node1 ~]# id mysqluid=3306(mysql) gid=3306(mysql) groups=3306(mysql)
2、安装并初始化MySQL
node1
[root@node1 ~]# tar xf mysql-5.5.28-linux2.6-x86_64.tar.gz -C /usr/local/[root@node1 ~]# cd /usr/local/[root@node1 local]# ln -sv mysql-5.5.28-linux2.6-x86_64 mysqlcreate symbolic link `mysql' to `mysql-5.5.28-linux2.6-x86_64'[root@node1 local]# cd mysql[root@node1 mysql]# chown -R root.mysql ./*[root@node1 mysql]# lltotal 132drwxr-xr-x 2 root mysql 4096 Jan 10 12:01 bin-rw-r--r-- 1 root mysql 17987 Aug 29 2012 COPYINGdrwxr-xr-x 4 root mysql 4096 Jan 10 12:01 datadrwxr-xr-x 2 root mysql 4096 Jan 10 12:01 docsdrwxr-xr-x 3 root mysql 4096 Jan 10 12:01 include-rw-r--r-- 1 root mysql 7604 Aug 29 2012 INSTALL-BINARYdrwxr-xr-x 3 root mysql 4096 Jan 10 12:01 libdrwxr-xr-x 4 root mysql 4096 Jan 10 12:02 mandrwxr-xr-x 10 root mysql 4096 Jan 10 12:02 mysql-test-rw-r--r-- 1 root mysql 2552 Aug 29 2012 READMEdrwxr-xr-x 2 root mysql 4096 Jan 10 12:02 scriptsdrwxr-xr-x 27 root mysql 4096 Jan 10 12:01 sharedrwxr-xr-x 4 root mysql 4096 Jan 10 12:02 sql-benchdrwxr-xr-x 2 root mysql 4096 Jan 10 12:02 support-files准备数据目录,请确保此时/dev/drbd0设备已挂载node1[root@node1 ~]# mkdir /mydata/data[root@node1 ~]# chown -R mysql.mysql /mydata/data[root@node1 mysql]# ll -d /mydata/datadrwxr-xr-x 2 mysql mysql 4096 Jan 10 12:04 /mydata/data此时再在node1上初始化mysql[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/dataInstalling MySQL system tables...OKFilling help tables...OKTo start mysqld at boot time you have to copysupport-files/mysql.server to the right place for your systemPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !To do so, start the server, then issue the following commands:./bin/mysqladmin -u root password 'new-password'./bin/mysqladmin -u root -h node1.network.com password 'new-password'Alternatively you can run:./bin/mysql_secure_installationwhich will also give you the option of removing the testdatabases and anonymous user created by default. This isstrongly recommended for production servers.See the manual for more instructions.You can start the MySQL daemon with:cd . ; ./bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.plcd ./mysql-test ; perl mysql-test-run.plPlease report any problems with the ./bin/mysqlbug script![root@node1 mysql]# ll /mydata/data/total 24drwx------ 2 mysql root 4096 Jan 10 12:05 mysqldrwx------ 2 mysql mysql 4096 Jan 10 12:05 performance_schemadrwx------ 2 mysql root 4096 Jan 10 12:05 test拷贝配置文件和服务脚本[root@node1 mysql]# cp support-files/my-large.cnf /etc/my.cnf [root@node1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld修改mysql的配置文件[root@node1 mysql]# vim /etc/my.cnf [root@node1 mysql]# grep -A 1 "datadir" /etc/my.cnf datadir = /mydata/datainnodb_file_per_table = 1将两文件拷贝至node2[root@node1 ~]# scp /etc/rc.d/init.d/mysqld node2:/etc/rc.d/init.d/mysqld 100% 10KB 10.4KB/s 00:00 [root@node1 ~]# scp /etc/my.cnf node2:/etc/my.cnf 100% 4713 4.6KB/s 00:00
node2
[root@node2 ~]# tar xf mysql-5.5.28-linux2.6-x86_64.tar.gz -C /usr/local/[root@node2 ~]# cd /usr/local/[root@node2 local]# ln -sv mysql-5.5.28-linux2.6-x86_64 mysqlcreate symbolic link `mysql' to `mysql-5.5.28-linux2.6-x86_64'[root@node2 local]# cd mysql[root@node2 mysql]# chown -R root.mysql ./*[root@node2 mysql]# lltotal 132drwxr-xr-x 2 root mysql 4096 Jan 7 19:31 bin-rw-r--r-- 1 root mysql 17987 Aug 29 2012 COPYINGdrwxr-xr-x 4 root mysql 4096 Jan 7 19:31 datadrwxr-xr-x 2 root mysql 4096 Jan 7 19:30 docsdrwxr-xr-x 3 root mysql 4096 Jan 7 19:31 include-rw-r--r-- 1 root mysql 7604 Aug 29 2012 INSTALL-BINARYdrwxr-xr-x 3 root mysql 4096 Jan 7 19:31 libdrwxr-xr-x 4 root mysql 4096 Jan 7 19:31 mandrwxr-xr-x 10 root mysql 4096 Jan 7 19:31 mysql-test-rw-r--r-- 1 root mysql 2552 Aug 29 2012 READMEdrwxr-xr-x 2 root mysql 4096 Jan 7 19:31 scriptsdrwxr-xr-x 27 root mysql 4096 Jan 7 19:31 sharedrwxr-xr-x 4 root mysql 4096 Jan 7 19:31 sql-benchdrwxr-xr-x 2 root mysql 4096 Jan 7 19:31 support-files在node1节点上已经初始化过了,所以这里不需要再初始化
3、测试mysql的数据目录是否会同步
[root@node1 mysql]# service mysqld startStarting MySQL..... [ OK ][root@node1 mysql]# /usr/local/mysql/bin/mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.28-log MySQL Community Server (GPL)Type 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema | | mysql | | performance_schema | | test | +--------------------+4 rows in set (0.13 sec)mysql> CREATE DATABASE mydb; # 创建个测试库Query OK, 1 row affected (0.00 sec)mysql> \qBye[root@node1 mysql]# service mysqld stopShutting down MySQL. [ OK ]卸载目录,并降级为备从点[root@node1 mysql]# umount /dev/drbd0 [root@node1 mysql]# drbdadm secondary mysql再将node2提升为主节点,并挂载数据目录,看能否看到刚才创建的测试库[root@node2 mysql]# drbdadm primary mysql[root@node2 mysql]# mount /dev/drbd0 /mydata/启动mysql[root@node2 mysql]# service mysqld startStarting MySQL... [ OK ][root@node2 mysql]# /usr/local/mysql/bin/mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.28-log MySQL Community Server (GPL)Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mydb || mysql || performance_schema || test |+--------------------+5 rows in set (0.07 sec)mysql> DROP DATABASE mydb; # 确实可以实现数据同步,现在删除这个测试库Query OK, 0 rows affected (0.04 sec)mysql> \qBye将drbd设备作为mysql的数据目录是可行的,已经测试完毕,接下来就可以配置corosync和pacemaker了,在这之前还有一个操作作为高可用服务,资源一定不能开机自启动,所以这里我们关闭两个节点上的drbd和mysql自启动node2[root@node2 ~]# service mysqld stopShutting down MySQL. [ OK ][root@node2 ~]# chkconfig mysqld off[root@node2 ~]# service drbd stopStopping all DRBD resources: .[root@node2 ~]# chkconfig drbd off因为node1在刚才的测试中是从节点,所以node1上的mysqld服务都是关闭的[root@node1 mysql]# chkconfig mysqld off[root@node1 mysql]# service drbd stopStopping all DRBD resources: .[root@node1 mysql]# chkconfig drbd off接下来开始配置corosync
五、corosync的安装与基本配置
1、配置yum源(也可以自行下载两个软件包,我这里直接使用yum源)
node1
[root@node1 ~]# cd /etc/yum.repos.d/[root@node1 yum.repos.d]# wget http://clusterlabs.org/rpm/epel-5/clusterlabs.repo [root@node1 yum.repos.d]# yum install -y pacemaker corosync
node2
[root@node2 ~]# cd /etc/yum.repos.d/[root@node2 yum.repos.d]# wget http://clusterlabs.org/rpm/epel-5/clusterlabs.repo [root@node2 yum.repos.d]# yum install -y pacemaker corosync
2、编辑corosync的主配置文件
[root@node1 ~]# cd /etc/corosync/ [root@node1 corosync]# ls corosync.conf.example service.d uidgid.d[root@node1 corosync]# cp corosync.conf.example corosync.conf [root@node1 corosync]# vi corosync.conf# Please read the corosync.conf.5 manual page compatibility: whitetanktotem { version: 2 # 配置文件版本号,目前合法的版本号是2,不能修改 secauth: on # 开启安全认证功能,当使用aisexec时,会非常消耗CPU threads: 2 # 线程数,根据CPU个数和核心数确定 interface { ringnumber: 0 # 冗余环号,节点有多个网卡是可定义对应网卡在一个环内 bindnetaddr: 172.16.1.0 # 注意,这里填的是网络地址,不是某个ip mcastaddr: 226.94.19.37 # 心跳信息传递的组播地址 mcastport: 5405 # 心跳信息组播使用端口 } }logging { fileline: off # 指定要打印的行 to_stderr: no # 是否发送到标准错误输出,即显示器,建议不要开启 to_logfile: yes # 定义是否记录到日志文件 to_syslog: no # 定义是否记录到syslog,与logfile只启用一个即可 logfile: /var/log/cluster/corosync.log # 定义日志文件的保存位置 debug: off # 是否开启debug功能 timestamp: on # 是否打印时间戳,利于定位错误,但会消耗CPU logger_subsys { subsys: AMF debug: off } }service { ver: 0 name: pacemaker # 定义pacemaker作为corosync的插件运行,启动sync时则会同时启动pacemaker # use_mgmtd: yes }aisexec { user: root group: root }amf { mode: disabled }注:各指令详细信息可man corosync.conf
3、生成authkey文件
[root@node1 corosync]# corosync-keygen Corosync Cluster Engine Authentication key generator.Gathering 1024 bits for key from /dev/random.Press keys on your keyboard to generate entropy.Writing corosync key to /etc/corosync/authkey.[root@node1 corosync]# lltotal 40-r-------- 1 root root 128 Jan 8 14:50 authkey # 权限为400-rw-r--r-- 1 root root 536 Jan 8 14:48 corosync.conf-rw-r--r-- 1 root root 436 Jul 28 2010 corosync.conf.exampledrwxr-xr-x 2 root root 4096 Jul 28 2010 service.ddrwxr-xr-x 2 root root 4096 Jul 28 2010 uidgid.d
4、将主配置文件和密钥认证文件拷贝至node2节点
[root@node1 corosync]# scp -p authkey corosync.conf node2:/etc/corosync/authkey 100% 128 0.1KB/s 00:00 corosync.conf 100% 536 0.5KB/s 00:00 由于我们定义了记录到日志文件中,所以此处还得创建那个日志目录[root@node1 corosync]# mkdir /var/log/cluster[root@node1 corosync]# ssh node2 'mkdir /var/log/cluster'
5、启动corosync
首先启动corosync,再查看启动过程信息,这里以node1为例[root@node1 ~]# service corosync startStarting Corosync Cluster Engine (corosync): [ OK ][root@node1 ~]# ssh node2 'service corosync start'Starting Corosync Cluster Engine (corosync): [ OK ]查看corosync引擎是否正常启动[root@node1 corosync]# grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log Jan 08 15:37:22 corosync [MAIN ] Corosync Cluster Engine ('1.2.7'): started and ready to provide service.Jan 08 15:37:22 corosync [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.查看初始化成员节点通知是否正常发出[root@node1 corosync]# grep TOTEM /var/log/cluster/corosync.logJan 08 15:37:22 corosync [TOTEM ] Initializing transport (UDP/IP).Jan 08 15:37:22 corosync [TOTEM ] Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).Jan 08 15:37:22 corosync [TOTEM ] The network interface [172.16.1.101] is now up.Jan 08 15:37:22 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.Jan 08 15:37:30 corosync [TOTEM ] A processor joined or left the membership and a new membership was formed.检查启动过程中是否有错误产生[root@node1 corosync]# grep ERROR: /var/log/cluster/corosync.log Jan 08 15:37:46 node1.network.com pengine: [3688]: ERROR: unpack_resources: Resource start-up disabled since no STONITH resources have been definedJan 08 15:37:46 node1.network.com pengine: [3688]: ERROR: unpack_resources: Either configure some or disable STONITH with the stonith-enabled optionJan 08 15:37:46 node1.network.com pengine: [3688]: ERROR: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity查看pacemaker是否正常启动[root@node1 corosync]# grep pcmk_startup /var/log/cluster/corosync.log Jan 08 15:37:22 corosync [pcmk ] info: pcmk_startup: CRM: InitializedJan 08 15:37:22 corosync [pcmk ] Logging: Initialized pcmk_startupJan 08 15:37:22 corosync [pcmk ] info: pcmk_startup: Maximum core file size is: 18446744073709551615Jan 08 15:37:22 corosync [pcmk ] info: pcmk_startup: Service: 9Jan 08 15:37:22 corosync [pcmk ] info: pcmk_startup: Local hostname: node1.network.com查看集群节点状态[root@node1 ~]# crm status============Last updated: Fri Jan 8 18:32:18 2016Stack: openaisCurrent DC: node1.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes0 Resources configured.============Online: [ node1.network.com node2.network.com ]
6、配置资源的默认属性
随便哪个节点上运行crm都可以,会自动同步操作至DC[root@node1 ~]# crmcrm(live)# status ============Last updated: Fri Jan 8 19:48:51 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes0 Resources configured.============Online: [ node1.network.com node2.network.com ]crm(live)# configure crm(live)configure# verify crm_verify[10513]: 2016/01/08_19:48:54 ERROR: unpack_resources: Resource start-up disabled since no STONITH resources have been definedcrm_verify[10513]: 2016/01/08_19:48:54 ERROR: unpack_resources: Either configure some or disable STONITH with the stonith-enabled optioncrm_verify[10513]: 2016/01/08_19:48:54 ERROR: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrityErrors found during check: config not validcrm(live)configure# property stonith-enabled=false # 禁用stonith设备crm(live)configure# property no-quorum-policy=ignore # 不具备法定票数时采取的动作crm(live)configure# rsc_defaults target-role=stopped # 配置资源完成之后默认采取的动作crm(live)configure# rsc_defaults resource-stickiness=100 # 设置默认的资源黏性crm(live)configure# verify crm(live)configure# commit 查看配置文件中生成的定义crm(live)configure# show node node1.network.comnode node2.network.comproperty $id="cib-bootstrap-options" \ dc-version="1.0.12-unknown" \ cluster-infrastructure="openais" \ expected-quorum-votes="2" \ stonith-enabled="false" \ no-quorum-policy="ignore"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
六、基于crm配置资源
1、定义drbd主从资源
首先定义一个主资源crm(live)configure# primitive mysqldrbd ocf:heartbeat:drbd params drbd_resource=mysql ignore_deprecation=true op start timeout=240s op stop timeout=100s op monitor role=Master interval=10s timeout=20s op monitor role=Slave interval=20s timeout=20s 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=truecrm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# show node node1.network.comnode node2.network.comprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"ms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"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"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
2、定义mysqlstore资源
crm(live)configure# primitive mysqlstore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext3 op start timeout=60s op stop timeout=60s crm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# show node node1.network.comnode node2.network.comprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"ms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"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"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
3、定义mysqld资源
crm(live)configure# primitive mysqld lsb:mysqld crm(live)configure# verify 查看配置信息crm(live)configure# show node node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"ms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"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"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
4、定义mysqlip资源
crm(live)configure# primitive mysqlip ocf:heartbeat:IPaddr params ip=172.16.1.110 cidr_netmask=16 nic=eth0 crm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# shownode node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlip ocf:heartbeat:IPaddr \ params ip="172.16.1.110" cidr_netmask="16" nic="eth0"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"ms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"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"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
5、定义mysqlservice组资源
crm(live)configure# group mysqlservice mysqlstore mysqlip mysqldcrm(live)configure# verify crm(live)configure# commit查看配置信息crm(live)configure# shownode node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlip ocf:heartbeat:IPaddr \ params ip="172.16.1.110" cidr_netmask="16" nic="eth0"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"group mysqlservice mysqlstore mysqlip mysqldms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"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"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
6、定义排列约束
由于我们定义了一个组资源mysqlservice,所以mysqlstore、mysqlip、mysqld一定是允许在同一个节点的所以我们只需要将主从资源中的主资源ms_mysqldrbd与这个组定义为排列约束即可crm(live)configure# colocation mysqlservice_with_ms_mysqldrbd inf: mysqlservice ms_mysqldrbd:Mastercrm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# show node node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlip ocf:heartbeat:IPaddr \ params ip="172.16.1.110" cidr_netmask="16" nic="eth0"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"group mysqlservice mysqlstore mysqlip mysqldms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"colocation mysqlservice_with_ms_mysqldrbd inf: mysqlservice ms_mysqldrbd:Masterproperty $id="cib-bootstrap-options" \ dc-version="1.0.12-unknown" \ cluster-infrastructure="openais" \ expected-quorum-votes="2" \ stonith-enabled="false" \ no-quorum-policy="ignore"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
7、定义顺序约束
定义顺序约束,先启动ms_mysqldrbd,再启动mysqlservice组资源 crm(live)configure# order ms_mysqldrbd_before_mysqlservice mandatory: ms_mysqldrbd:promote mysqlservice:startcrm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# shownode node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlip ocf:heartbeat:IPaddr \ params ip="172.16.1.110" cidr_netmask="16" nic="eth0"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"group mysqlservice mysqlstore mysqlip mysqldms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"colocation mysqlservice_with_ms_mysqldrbd inf: mysqlservice ms_mysqldrbd:Masterorder ms_mysqldrbd_before_mysqlservice inf: ms_mysqldrbd:promote mysqlservice:startproperty $id="cib-bootstrap-options" \ dc-version="1.0.12-unknown" \ cluster-infrastructure="openais" \ expected-quorum-votes="2" \ stonith-enabled="false" \ no-quorum-policy="ignore"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100" 定义顺序约束,对于mysqlservice组资源,先启动mysqlstore,再启动mysqlip,再启动mysqld服务crm(live)configure# order mysqlstore_before_mysqlip_before_mysqld mandatory: mysqlstore mysqlip mysqldcrm(live)configure# verify crm(live)configure# commit 查看配置信息crm(live)configure# show node node1.network.comnode node2.network.comprimitive mysqld lsb:mysqldprimitive mysqldrbd ocf:heartbeat:drbd \ params drbd_resource="mysql" ignore_deprecation="true" \ op start interval="0" timeout="240s" \ op stop interval="0" timeout="100s" \ op monitor interval="10s" role="Master" timeout="20s" \ op monitor interval="20s" role="Slave" timeout="20s"primitive mysqlip ocf:heartbeat:IPaddr \ params ip="172.16.1.110" cidr_netmask="16" nic="eth0"primitive mysqlstore ocf:heartbeat:Filesystem \ params device="/dev/drbd0" directory="/mydata" fstype="ext3" \ op start interval="0" timeout="60s" \ op stop interval="0" timeout="60s"group mysqlservice mysqlstore mysqlip mysqldms ms_mysqldrbd mysqldrbd \ meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"colocation mysqlservice_with_ms_mysqldrbd inf: mysqlservice ms_mysqldrbd:Masterorder ms_mysqldrbd_before_mysqlservice inf: ms_mysqldrbd:promote mysqlservice:startorder mysqlstore_before_mysqlip_before_mysqld inf: mysqlstore mysqlip mysqldproperty $id="cib-bootstrap-options" \ dc-version="1.0.12-unknown" \ cluster-infrastructure="openais" \ expected-quorum-votes="2" \ stonith-enabled="false" \ no-quorum-policy="ignore"rsc_defaults $id="rsc-options" \ target-role="stopped" \ resource-stickiness="100"
8、启动配置好的资源
crm(live)configure# cdcrm(live)# resource 启动ms_mysqldrbd主从资源crm(live)resource# start ms_mysqldrbd 启动mysqlservice组资源crm(live)resource# start mysqlservice crm(live)resource# cd查看运行状态crm(live)# status ============Last updated: Sun Jan 10 13:37:14 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Online: [ node1.network.com node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node1.network.com ] Slaves: [ node2.network.com ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node1.network.com mysqlip (ocf::heartbeat:IPaddr): Started node1.network.com mysqld (lsb:mysqld): Started node1.network.com 再来查看下drbd的主从节点[root@node1 ~]# drbd-overview 0:mysql Connected Primary/Secondary UpToDate/UpToDate C r----- /mydata ext3 940M 47M 845M 6% 查看是否挂载drbd设备[root@node1 ~]# mount | tail -1/dev/drbd0 on /mydata type ext3 (rw)查看mysqlip是否启用[root@node1 ~]# ip addr show | grep "172.16.1.110" inet 172.16.1.110/16 brd 172.16.255.255 scope global eth0:0 查看mysqld服务是否启动[root@node1 ~]# ss -tnl | grep "3306"0 0 *:3306 *:*
9、测试高可用性
此时资源是运行在node1节点上的[root@node1 ~]# crmcrm(live)# status ============Last updated: Sun Jan 10 13:42:33 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Online: [ node1.network.com node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node1.network.com ] Slaves: [ node2.network.com ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node1.network.com mysqlip (ocf::heartbeat:IPaddr): Started node1.network.com mysqld (lsb:mysqld): Started node1.network.com让node1节点下线,看资源是否会自动转移crm(live)# node standby node1.network.com查看运行状态crm(live)# status ============Last updated: Sun Jan 10 13:44:09 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Node node1.network.com: standbyOnline: [ node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node2.network.com ] Stopped: [ mysqldrbd:0 ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node2.network.com mysqlip (ocf::heartbeat:IPaddr): Started node2.network.com mysqld (lsb:mysqld): Started node2.network.com可以看到已成功转移,node2已经挂载了drbd设备 再让node1节点上线,发现资源并不会转移,因为我们定义了资源黏性为100crm(live)# node online node1.network.comcrm(live)# status ============Last updated: Sun Jan 10 13:45:26 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Online: [ node1.network.com node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node2.network.com ] Slaves: [ node1.network.com ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node2.network.com mysqlip (ocf::heartbeat:IPaddr): Started node2.network.com mysqld (lsb:mysqld): Started node2.network.com
10、测试远程访问mysql
可以看到此时资源是运行在node2节点上的[root@node2 ~]# crm status============Last updated: Sun Jan 10 13:49:48 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Online: [ node1.network.com node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node2.network.com ] Slaves: [ node1.network.com ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node2.network.com mysqlip (ocf::heartbeat:IPaddr): Started node2.network.com mysqld (lsb:mysqld): Started node2.network.com现在在node2节点上直接连上mysql,然后给mysql授权允许远程访问 [root@node2 ~]# /usr/local/mysql/bin/mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.5.28-log MySQL Community Server (GPL)Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'redhat';Query OK, 0 rows affected (0.07 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema | | mysql | | performance_schema | | test | +--------------------+4 rows in set (0.11 sec) mysql> CREATE DATABASE mydb; # 创建个测试库,看等下主从切换时,另外个节点能否访问Query OK, 1 row affected (0.13 sec)mysql> \qBye现在我们切换至node1,并让node2成为从节点[root@node1 ~]# crm node standby node2.network.com[root@node1 ~]# crm node online node2.network.com[root@node1 ~]# crm status============Last updated: Sun Jan 10 13:55:37 2016Stack: openaisCurrent DC: node2.network.com - partition with quorumVersion: 1.0.12-unknown2 Nodes configured, 2 expected votes2 Resources configured.============Online: [ node1.network.com node2.network.com ] Master/Slave Set: ms_mysqldrbd Masters: [ node1.network.com ] Slaves: [ node2.network.com ] Resource Group: mysqlservice mysqlstore (ocf::heartbeat:Filesystem): Started node1.network.com mysqlip (ocf::heartbeat:IPaddr): Started node1.network.com mysqld (lsb:mysqld): Started node1.network.com 谁用tcp协议连接mysql,看是否能访问到刚才创建的测试库[root@node1 ~]# /usr/local/mysql/bin/mysql -uroot -h 172.16.1.110 -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.28-log MySQL Community Server (GPL)Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mydb || mysql || performance_schema || test |+--------------------+5 rows in set (0.17 sec)mysql> DROP DATABASE mydb; # 测试成功,删除此测试库Query OK, 0 rows affected (0.25 sec)mysql> \qBye
到此,一个 Corosync + Pacemaker + DRBD + mysql的高可用解决方案配置完成