通过HA实现服务器的高可用性,即通过corosync+drbd+pacemaker实现MySQL服务器的高可用集群。
案例应用实现主要步骤:
一.准备工作:
二、DRBD安装配置步骤
三、mysql安装与配置
四、corosync+pacemaker的安装和配置
五、集群管理
实现详细步骤:
一.准备工作:
1.1 修改node1.linuxidc.com主机名,IP地址和系统时间
[root@node1 ~]# hostname
node1.linuxidc.com
[root@node1 ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=node1.linuxidc.com
[root@node1 ~]# hwclock -s
[root@node1 ~]# date
Tue Feb 7 20:24:22 CST 2012
[root@node1 ~]#
[root@node1 ~]# setup
[root@node1 ~]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[root@node1 ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:AE:83:D1
inet addr:192.168.101.81 Bcast:192.168.101.255 Mask:255.255.255.0
1.2 修改node2.linuxidc.com主机名,IP地址和系统时间
[root@node2 ~]# hostname
node2.linuxidc.com
[root@node2 ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=node2.linuxidc.com
[root@node2 ~]# hwclock -s
[root@node2 ~]# date
Tue Feb 7 20:25:49 CST 2012
[root@node2 ~]#
[root@node2 ~]# setup
[root@node2 ~]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: [ OK ] [root@node2 ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:D1:D4:32 inet addr:192.168.101.82 Bcast:192.168.101.255 Mask:255.255.255.0
1.3 在node1和node2上配置hosts文件和ssh密钥信息
有利于以后在一个节点对另一节点直接操作。
1.3.1 在node1上修改/etc/hosts文件
[root@node1 ~]# echo "192.168.101.81 node1.linuxidc.com node1" >>/etc/hosts [root@node1 ~]# echo "192.168.101.82 node2.linuxidc.com node2" >>/etc/hosts
1.3.1 在node1上修改/etc/hosts文件
[root@node2 ~]# echo "192.168.101.81 node1.linuxidc.com node1" >>/etc/hosts [root@node2 ~]# echo "192.168.101.82 node2.linuxidc.com node2" >>/etc/hosts
1.3.3 在node1上配置ssh密钥信息
[root@node1 ~]# ssh-keygen -t rsa #一直输入空格 [root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2.linuxidc.com
1.3.4 在node2上配置ssh密钥信息
[root@node2 ~]# ssh-keygen -t rsa #一直输入空格 [root@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1.linuxidc.com [root@node2 ~]# ssh node1 'ifconfig' #将看到node2上的信息 The authenticity of host 'node1 (192.168.101.81)' can't be established. RSA key fingerprint is d4:f1:06:3b:a0:81:fd:85:65:20:9e:a1:ee:46:a6:8b. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'node1' (RSA) to the list of known hosts. eth0 Link encap:Ethernet HWaddr 00:0C:29:AE:83:D1 inet addr:192.168.101.81 Bcast:192.168.101.255 Mask:255.255.255.0
1.4 下载相关软件包:(这里我将下载的软件包放在/root/ha/下面了,共18个)
[root@node1 ~]# cd ha/ [root@node1 ha]# 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 drbd83-8.3.8-1.el5.CentOS.i386.rpm heartbeat-3.0.3-2.3.el5.i386.rpm heartbeat-libs-3.0.3-2.3.el5.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm ldirectord-1.0.1-1.el5.i386.rpm libesmtp-1.0.4-5.el5.i386.rpm mysql-5.5.15-linux2.6-i686.tar.gz openais-1.1.3-1.6.el5.i386.rpm openaislib-1.1.3-1.6.el5.i386.rpm pacemaker-1.1.5-1.1.el5.i386.rpm pacemaker-cts-1.1.5-1.1.el5.i386.rpm pacemaker-libs-1.1.5-1.1.el5.i386.rpm perl-TimeDate-1.16-5.el5.noarch.rpm resource-agents-1.0.4-1.1.el5.i386.rpm 这里不需要ldirectord-1.0.1-1.el5.i386.rpm,故将其删除 [root@node1 ha]# rm ldirectord-1.0.1-1.el5.i386.rpm rm: remove regular file `ldirectord-1.0.1-1.el5.i386.rpm'? y [root@node1 ha]# ssh node2 'mkdir /root/ha' [root@node1 ha]# scp *.rpm node2:/root/ha/#将相关软件包移动到node2上 [root@node1 ha]# scp *.rpm node2:/root/ha/ cluster-glue-1.0.6-1.6.el5.i386.rpm 100% 265KB 265.0KB/s 00:00 cluster-glue-libs-1.0.6-1.6.el5.i386.rpm 100% 130KB 130.1KB/s 00:00 corosync-1.2.7-1.1.el5.i386.rpm 100% 166KB 166.1KB/s 00:00 corosynclib-1.2.7-1.1.el5.i386.rpm 100% 155KB 154.8KB/s 00:00 drbd83-8.3.8-1.el5.centos.i386.rpm 100% 217KB 216.7KB/s 00:00 heartbeat-3.0.3-2.3.el5.i386.rpm 100% 162KB 161.7KB/s 00:00 heartbeat-libs-3.0.3-2.3.el5.i386.rpm 100% 283KB 282.8KB/s 00:00 kmod-drbd83-8.3.8-1.el5.centos.i686.rpm 100% 123KB 123.0KB/s 00:00 libesmtp-1.0.4-5.el5.i386.rpm 100% 59KB 59.0KB/s 00:00 openais-1.1.3-1.6.el5.i386.rpm 100% 202KB 202.2KB/s 00:00 openaislib-1.1.3-1.6.el5.i386.rpm 100% 92KB 92.4KB/s 00:00 pacemaker-1.1.5-1.1.el5.i386.rpm 100% 778KB 778.1KB/s 00:00 pacemaker-cts-1.1.5-1.1.el5.i386.rpm 100% 203KB 203.1KB/s 00:00 pacemaker-libs-1.1.5-1.1.el5.i386.rpm 100% 324KB 324.2KB/s 00:00 perl-TimeDate-1.16-5.el5.noarch.rpm 100% 32KB 32.1KB/s 00:00 resource-agents-1.0.4-1.1.el5.i386.rpm 100% 380KB 379.5KB/s 00:00 [root@node1 ha]# scp mysql-5.5.15-linux2.6-i686.tar.gz node2:/root/ha/ mysql-5.5.15-linux2.6-i686.tar.gz 100% 155MB 10.3MB/s 00:15
1.5 配置本地yum数据库
[root@node1 ha]# [root@node1 ha]# mkdir /mnt/cdrom/ [root@node1 ha]# mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/cdrom is write-protected, mounting read-only [root@node1 ha]# yum list all [root@node1 ha]# [root@node1 ha]# scp /etc/yum.repos.d/server.repo node2:/etc/yum.repos.d/ server.repo 100% 647 0.6KB/s 00:00 [root@node1 ha]# [root@node1 ha]# ssh node2 'mkdir /mnt/cdrom/' [root@node1 ha]# ssh node2 'mount /dev/cdrom /mnt/cdrom/' [root@node1 ha]# ssh node2 'yum list all'
1.6 在node1上新增磁盘
[root@node1 ha]# cd [root@node1 ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1318 10482412+ 83 Linux /dev/sda3 1319 1579 2096482+ 82 Linux swap / Solaris [root@node1 ~]# fdisk /dev/sda p/n/p//+1000M/p/w [root@node1 ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1318 10482412+ 83 Linux /dev/sda3 1319 1579 2096482+ 82 Linux swap / Solaris /dev/sda4 1580 1702 987997+ 83 Linux [root@node1 ~]# partprobe /dev/sda [root@node1 ~]# cat /proc/partitions major minor #blocks name 8 0 20971520 sda 8 1 104391 sda1 8 2 10482412 sda2 8 3 2096482 sda3 8 4 987997 sda4 [root@node1 ~]#
1.7 在node2上新增磁盘
[root@node2 ha]# cd [root@node2 ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1318 10482412+ 83 Linux /dev/sda3 1319 1579 2096482+ 82 Linux swap / Solaris [root@node2 ~]# fdisk /dev/sda p/n/p//+1000M/p/w [root@node2 ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 1318 10482412+ 83 Linux /dev/sda3 1319 1579 2096482+ 82 Linux swap / Solaris /dev/sda4 1580 1702 987997+ 83 Linux [root@node2 ~]# partprobe /dev/sda [root@node2 ~]# cat /proc/partitions major minor #blocks name 8 0 20971520 sda 8 1 104391 sda1 8 2 10482412 sda2 8 3 2096482 sda3 8 4 987997 sda4 [root@node2 ~]#
二、DRBD安装配置步骤
在node1和node2做以下操作:
我下载的软件包是:(我放在/root/ha/下了)
drbd83-8.3.8-1.el5.centos.i386.rpm
kmod-drbd83-8.3.8-1.el5.centos.i686.rpm
2.1、安装DRBD 套件
[root@node1 ~]# cd ha/ [root@node1 ha]# ls [root@node1 ha]# yum localinstall -y drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm –nogpgcheck ... Installed: drbd83.i386 0:8.3.8-1.el5.centos kmod-drbd83.i686 0:8.3.8-1.el5.centos Complete! [root@node1 ha]# cd [root@node2 ~]# cd ha/ [root@node2 ha]# ls [root@node2 ha]# yum localinstall -y drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm –nogpgcheck ... Installed: drbd83.i386 0:8.3.8-1.el5.centos kmod-drbd83.i686 0:8.3.8-1.el5.centos Complete! [root@node2 ha]# cd
2.2、加载DRBD 模块
[root@node1 ~]# modprobe drbd [root@node1 ~]# lsmod |grep drbd drbd 228528 0 [root@node1 ~]# [root@node1 ~]# ssh node2 'modprobe drbd' [root@node1 ~]# ssh node2 'lsmod |grep drbd' drbd 228528 0 [root@node1 ~]#
2.3、修改配置文件
drbd.conf配置文件DRBD运行时,会读取一个配置文件/etc/drbd.conf.这个文件里描述了DRBD设备与硬盘分区的映射关系
2.3.1 在node1上以下配置
[root@node1 ~]# cp /usr/share/doc/drbd83-8.3.8/drbd.conf /etc/ cp: overwrite `/etc/drbd.conf'? y [root@node1 ~]# cat /etc/drbd.conf # You can find an example in /usr/share/doc/drbd.../drbd.conf.example include "drbd.d/global_common.conf"; include "drbd.d/*.res"; [root@node1 ~]# cd /etc/drbd.d/ [root@node1 drbd.d]# ll total 4 -rwxr-xr-x 1 root root 1418 Jun 4 2010 global_common.conf [root@node1 drbd.d]# cp global_common.conf global_common.conf.bak #修改全局配置文件(说明略) [root@node1 drbd.d]# vim global_common.conf global { usage-count yes; } common { protocol C; 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 120; degr-wfc-timeout 100; } disk { on-io-error detach; } net { cram-hmac-alg "sha1"; shared-secret "mydrbd123"; } syncer { rate 100M; } } #修改资源配置文件(说明略) [root@node1 drbd.d]# vim /etc/drbd.d/mysql.res resource mysql { on node1.linuxidc.com { device /dev/drbd0; disk /dev/sda4; address 192.168.101.81:7898; meta-disk internal; } on node2.linuxidc.com { device /dev/drbd0; disk /dev/sda4; address 192.168.101.82:7898; meta-disk internal; } }
2.3.2 复制配置到node2上:
[root@node1 drbd.d]# scp /etc/drbd.conf node2:/etc/ drbd.conf 100% 133 0.1KB/s 00:00 [root@node1 drbd.d]# scp /etc/drbd.d/* node2:/etc/drbd.d/ global_common.conf 100% 682 0.7KB/s 00:00 global_common.conf.bak 100% 1418 1.4KB/s 00:00 mysql.res 100% 468 0.5KB/s 00:00
2.4、 检测配置文件, 创建nfs 的资源
//分别在node1和node2上初始化定义的mysql的资源
//检测配置文件(两次执行如下命令)
在node1上:
[root@node1 drbd.d]# drbdadm adjust mysql --== Thank you for participating in the global usage survey ==-- The server's response is: 0: Failure: (119) No valid meta-data signature found. ==> Use 'drbdadm create-md res' to initialize meta-data area. <== Command 'drbdsetup 0 disk /dev/sda4 /dev/sda4 internal --set-defaults --create-device --on-io-error=detach' terminated with exit code 10 [root@node1 drbd.d]# drbdadm adjust mysql drbdsetup 0 show:5: delay-probe-volume 0k => 0k out of range [4..1048576]k. [root@node1 drbd.d]# drbdadm create-md mysql Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data block successfully created. [root@node1 drbd.d]# ll /dev/drbd0 brw-r----- 1 root disk 147, 0 Feb 7 20:44 /dev/drbd0 在node2上: [root@node2 ~]# drbdadm create-md mysql --== Thank you for participating in the global usage survey ==-- The server's response is: Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data block successfully created. [root@node2 ~]# ll /dev/drbd0 brw-rw---- 1 root root 147, 0 Feb 7 20:47 /dev/drbd0
2.5 启动DRBD服务,查看DRBD状态
[root@node1 drbd.d]# service drbd start Starting DRBD resources: [ mysql Found valid meta data in the expected location, 1011703808 bytes into /dev/sda4. d(mysql) s(mysql) n(mysql) ]...... [root@node2 ~]# service drbd start Starting DRBD resources: [ mysql Found valid meta data in the expected location, 1011703808 bytes into /dev/sda4. d(mysql) s(mysql) n(mysql) ]. [root@node1 drbd.d]# service drbd status drbd driver loaded OK; device status: version: 8.3.8 (api:88/proto:86-94) GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16 m:res cs ro ds p mounted fstype 0:mysql Connected Secondary/Secondary Inconsistent/Inconsistent C [root@node1 drbd.d]# drbd-overview 0:mysql Connected Secondary/Secondary Inconsistent/Inconsistent C r---- [root@node2 ~]# service drbd status drbd driver loaded OK; device status: version: 8.3.8 (api:88/proto:86-94) GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16 m:res cs ro ds p mounted fstype 0:mysql Connected Secondary/Secondary Inconsistent/Inconsistent C [root@node2 ~]# drbd-overview 0:mysql Connected Secondary/Secondary Inconsistent/Inconsistent C r----
2.6 设置DRBD的主节点:
从上面的信息中可以看出此时两个节点均处于Secondary状态。于是,我们接下来需要将其中一个节点设置为Primary,这里将node1设置为主节点,故要在node1上执行如下命令:可以看到文件同步过程。
[root@node1 drbd.d]# drbdadm -- --overwrite-data-of-peer primary mysql [root@node1 drbd.d]# drbd-overview 0:mysql SyncSource Primary/Secondary UpToDate/Inconsistent C r---- [===>................] sync'ed: 23.6% (761048/987928)K delay_probe: 20 [root@node1 drbd.d]# drbd-overview 0:mysql SyncSource Primary/Secondary UpToDate/Inconsistent C r---- [========>...........] sync'ed: 47.6% (522296/987928)K delay_probe: 42 [root@node1 drbd.d]# [root@node1 drbd.d]# drbd-overview 0:mysql Connected Primary/Secondary UpToDate/UpToDate C r---- [root@node1 drbd.d]# [root@node1 drbd.d]# cat /proc/drbd version: 8.3.8 (api:88/proto:86-94) GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r---- ns:987928 nr:0 dw:0 dr:987928 al:0 bm:61 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0 [root@node1 drbd.d]# [root@node2 ~]# drbd-overview 0:mysql SyncTarget Secondary/Primary Inconsistent/UpToDate C r---- [=====>..............] sync'ed: 32.7% (670680/987928)K queue_delay: 0.1 ms [root@node2 ~]# drbd-overview 0:mysql SyncTarget Secondary/Primary Inconsistent/UpToDate C r---- [===========>........] sync'ed: 61.6% (384536/987928)K queue_delay: 11.6 ms [root@node2 ~]# [root@node2 ~]# drbd-overview 0:mysql Connected Secondary/Primary UpToDate/UpToDate C r---- [root@node2 ~]# [root@node2 ~]# cat /proc/drbd version: 8.3.8 (api:88/proto:86-94) GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r---- ns:0 nr:987928 dw:987928 dr:0 al:0 bm:61 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
注:Primary/Secondary 说明当前节点为主节点;Secondary/Primary 说明当前节点为从节点。使用:watch -n 1 'cat /proc/drbd'查看同步过程!
2.7 创建文件系统(只可以在primary节点上进行,这里在node1上创建)
[root@node1 drbd.d]# mkfs -t ext3 /dev/drbd0 [root@node1 drbd.d]# mkdir -pv /mnt/mysqldata mkdir: created directory `/mnt/mysqldata' [root@node1 drbd.d]# ssh node2 'mkdir -pv /mnt/mysqldata' mkdir: created directory `/mnt/mysqldata' [root@node1 drbd.d]# mount /dev/drbd0 /mnt/mysqldata/ [root@node1 drbd.d]# [root@node1 drbd.d]# cd /mnt/mysqldata/ [root@node1 mysqldata]# ll total 16 drwx------ 2 root root 16384 Feb 7 21:26 lost+found [root@node1 mysqldata]# echo "123" >f1 [root@node1 mysqldata]# touch f2 [root@node1 mysqldata]# ll total 20 -rw-r--r-- 1 root root 4 Feb 7 21:28 f1 -rw-r--r-- 1 root root 0 Feb 7 21:28 f2 drwx------ 2 root root 16384 Feb 7 21:26 lost+found [root@node1 mysqldata]#
2.8将node2设置为primary节点
[root@node1 mysqldata]# cd [root@node1 ~]# umount /mnt/mysqldata/ [root@node1 ~]# drbdadm secondary mysql [root@node1 ~]# drbd-overview 0:mysql Connected Secondary/Secondary UpToDate/UpToDate C r---- [root@node1 ~]# 将node2设置为primary节点 [root@node2 ~]# drbd-overview 0:mysql Connected Secondary/Secondary UpToDate/UpToDate C r---- [root@node2 ~]# drbdadm primary mysql [root@node2 ~]# drbd-overview 0:mysql Connected Primary/Secondary UpToDate/UpToDate C r---- [root@node2 ~]# mount /dev/drbd0 /mnt/mysqldata/ [root@node2 ~]# cd /mnt/mysqldata/ [root@node2 mysqldata]# ll total 20 -rw-r--r-- 1 root root 4 Feb 7 21:28 f1 -rw-r--r-- 1 root root 0 Feb 7 21:28 f2 drwx------ 2 root root 16384 Feb 7 21:26 lost+found
此处看到在node1上创建的文件在这里都显示了,
最后将node2的/mnt/mysqldata/卸载
[root@node2 mysqldata]# cd
[root@node2 ~]# umount /mnt/mysqldata/
至此DRBD配置成功!!!