Chinaunix首页 | 论坛 | 博客
  • 博客访问: 136504
  • 博文数量: 25
  • 博客积分: 389
  • 博客等级: 一等列兵
  • 技术积分: 838
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-23 14:18
文章存档

2013年(20)

2012年(5)

分类: 架构设计与优化

2013-03-09 13:34:16

corosync/openais+pacemaker+nfs提供mysql高可用
拓扑图:
安装配置前准备:
  1. 设定172.16.11.11的节点名称为node1,172.16.11.12的节点名称为node2;
  2. 修改172.16.11.11和172.16.11.12的主机名称,使其节点名称与主机名称保持一致;
  3. # hostname node1
  4. # hostname node2
  5. 将主机名称写进node1和node2的配置文件中:
  6. # sed -i s@HOSTNAME=.*@HOSTNAME=node1@g /etc/sysconfig/network
  7. # sed -i s@HOSTNAME=.*@HOSTNAME=node2@g /etc/sysconfig/network
  8. 编辑node1和node2的/etc/hosts文件添加如下内容:
  9. 172.16.11.12 node2
  10. 172.16.11.11 node1
  11. 配置node1和node2的ssh互联互通;
  12. 在node1配置如下:
  13. # ssh-keyken -t rsa #生成一对密钥;
  14. # ssh-copy-id -i ./.ssh/id_rsa.pub root@node2 #将密钥放到node2节点上;
  15. 在node2配置如下:
  16. # ssh-keygen -t rsa #生成一对密钥;
  17. # ssh-copy-id -i ./.ssh/id_rsa.pub root@node1 #将密钥放到node1节点上;
  18. 修改node1和node2的时间保持一致
  19. # hwclock -s 将系统时间设置与硬件时间一致;
  20. 如果时间同步不了可以直接使用date命令修改即可;
  21. date命令格式:
  22. # date 月日时分年.
一、配置nfs服务器:
  1. # fdisk /dev/sda
  2. 创建两个大小为20G,类型为8e的分区,分区文件分别为/dev/sda5和/dev/sda6
  3. # partprobe /dev/sda
  4. # pvcreate /dev/sda{5,6}
  5. # vgcreate myvg /dev/sda{5,6}
  6. # lvcreate -L 20G -n mydata myvg
  7. # mke2fs -j -L MYDATA /dev/myvg/mydata
  8. 创建共享目录
  9. # mkdir /sharedata
  10. 将lvs开机自动挂载至/sharedata目录下
  11. # vim /etc/fstab
  12. 添加如下行
  13. LABEL=MYDATA /sharedata ext3 defaults 0 0
  14. # mount -a
  15. 添加mysql用户
  16. # useradd -r -u 150 mysql
  17. 修改/sharedata目录的属主和属组为mysql用户,让mysql用户对/sharedata目录具有读写权限;
  18. # chown -R mysql:mysql /sharedata/
  19. 通过nfs共享/sharedata/目录
  20. # vim /etc/exports
  21. 添加如下行
  22. /sharedata 172.16.11.11(rw,no_root_squash) 172.16.11.12(rw,no_root_squash)
  23. # service nfs start
  24. 让nfs开机自动启动
  25. # chkconfig nfs on
  26. # showmount -e localhost
  27. Export list for localhost:
  28. /sharedata 172.16.11.12,172.16.11.11
  29. 为node1和node2添加mysql用户并挂载nfs的共享目录
  30. # useradd -r -u 150 mysql
  31. 创建/data/mydata目录,让nfs服务器上的共享目录挂载在该目录下
  32. # mkdir -pv /data/mydata
  33. # mount -t nfs 172.16.11.21:/sharedata /data/mydata
  34. # ls /data/mydata/
  35. lost+found
  36. 验证一下mysql用户对/data/mydata目录的读写权限
  37. # su - mysql
  38. su: warning: cannot change directory to /home/mysql: No such file or directory
  39. -bash-3.2$ cd /data/mydata/
  40. -bash-3.2$ ls
  41. lost+found
  42. -bash-3.2$ touch test.txt
  43. -bash-3.2$ ls
  44. lost+found test.txt
  45. -bash-3.2$ rm test.txt
  46. -bash-3.2$ ls
  47. lost+found
  48. -bash-3.2$
  49. 以上操作验证mysql用户对/data/mydata目录的读写权限正常
二、node1和node2安装mysql
  1. node1安装mysql-5.5.24-linux2.6-i686.tar.gz
  2. #tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/
  3. #cd /usr/local/
  4. #ln -sv mysql-5.5.24-linux2.6-i686 mysql
  5. #cd mysql
  6. #chown -R mysql:mysql .
  7. #scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
  8. #chown -R root .
  9. 提供配置文件并进行相应修改
  10. #cp support-files/my-large.cnf /etc/my.cnf
  11. #vim /etc/my.cnf
  12. datadir = /data/mydata
  13. thread_concurrency = 2
  14. 为mysql提供sysv服务脚本
  15. #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
  16. 添加至服务列表
  17. #chkconfig --add mysqld
  18. # service mysqld start
  19. 修改PATH环境变量,让系统可以直接使用mysql的相关命令;
  20. #vim /etc/profile
  21. PATH=/usr/local/mysql/bin:$PATH
  22. #export PATH=/usr/local/mysql/bin:$PATH
  23. #mysql 连上mysql成功
  24. 停掉mysql服务并使其不开机自动启动
  25. # service mysqld stop
  26. # chkconfig mysqld off
  27. # chkconfig --list mysqld 一定要确保开机不自动启动
  28. mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  29. 卸载nfs
  30. # umount /data/mydata/
  31. node2安装mysql-5.5.24-linux2.6-i686.tar.gz
  32. #tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/
  33. #cd /usr/local/
  34. #ln -sv mysql-5.5.24-linux2.6-i686 mysql
  35. #cd mysql
  36. #chown -R root:mysql .
  37. 提供配置文件并进行相应修改
  38. #cp support-files/my-large.cnf /etc/my.cnf
  39. #vim /etc/my.cnf
  40. datadir = /data/mydata
  41. thread_concurrency = 2
  42. 为mysql提供sysv服务脚本
  43. #cp support-files/mysql.server /etc/rc.d/init.d/mysqld
  44. 添加至服务列表
  45. #chkconfig --add mysqld
  46. # service mysqld start
  47. 修改PATH环境变量,让系统可以直接使用mysql的相关命令;
  48. #vim /etc/profile
  49. PATH=/usr/local/mysql/bin:$PATH
  50. #export PATH=/usr/local/mysql/bin:$PATH
  51. #mysql 连上mysql成功
  52. 停掉mysql服务并使其不开机自动启动
  53. # service mysqld stop
  54. # chkconfig mysqld off
  55. # chkconfig --list mysqld 一定要确保开机不自动启动
  56. mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  57. 卸载nfs
  58. # umount /data/mydata/
三、安装配置集群服务

安装需要的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
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
cluster-glue-1.0.6-1.6.el5.i386.rpm
cluster-glue-libs-1.0.6-1.6.el5.i386.rpm
resource-agents-1.0.4-1.1.el5.i386.rpm
libesmtp-1.0.4-5.el5.i386.rpm
  1. 下载上述rpm包至node1和node2的/root目录下,然后分别在node1和node2上安装上述rpm包;
  2. 安装corosync
  3. # yum -y --nogpgcheck localinstall *.rpm
  4. node1和node2提供配置文件
  5. # cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
  6. # vim /etc/corosync/corosync.conf
  7. 修改如下选项:
  8. secauth: on
  9. bindnetaddr: 172.16.11.1
  10. mcastaddr: 226.100.7.1
  11. to_syslog: no
  12. 添加如下选项:
  13. service {
  14.         ver: 0
  15.         name: pacemaker
  16. }
  17. aisexec {
  18.     user: root
  19.     group: root
  20. }
  21. 生成节点间通信认证密钥:
  22. # corosync-keygen //会自动的在/etc/corosync目录下生成一个二进制格式的key文件,权限为400
  23. 复制/etc/corosync/下的authkey、corosync.conf至node2的/etc/corosync/目录下
  24. # scp authkey corosync.conf node2:/etc/corosync/
  25. node1和node2节点创建日志文件目录:
  26. # mkdir -p /var/log/cluster/
  27. # ssh node2 'mkdir -p /var/log/cluster/'
  28. 启动corosync服务:
  29. # service corosync start
  30. # ssh node2 '/etc/rc.d/init.d/corosync start'
  31. 查看corosync引擎是否正常启动:
  32. # grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log
  33. 查看初始化成员节点通知是否正常发出:
  34. # grep TOTEM /var/log/cluster/corosync.log
  35. 检查启动过程中是否有错误产生:
  36. # grep ERROR: /var/log/cluster/corosync.log | grep -v unpack_resources
  37. 查看pacemaker是否正常启动:
  38. # grep pcmk_startup /var/log/cluster/corosync.log
  39. 使用如下命令查看集群节点的启动状态:
  40. # crm status
  41. ============
  42. Last updated: Tue Aug 7 12:43:40 2012
  43. Stack: openais
  44. Current DC: node1 - partition with quorum
  45. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
  46. 2 Nodes configured, 2 expected votes
  47. 1 Resources configured.
  48. ============
  49.  
  50. Online: [ node1 node2 ]
四、添加集群资源
node1上添加集群资源(集群资源在任意一个节点配置即可,这里选择node1节点)

在添加集群资源前首先对进行简单的配置
  1. 禁用stonith:
  2. # crm configure property stonith-enabled=false
  3. 修改忽略quorum不能满足的集群状态检查:
  4. # crm configure property no-quorum-policy=ignore
  5. 为资源指定默认黏性值:
  6. # crm configure rsc_defaults resource-stickiness=100 
添加资源
  1. 添加访问mysql的ip
  2. # crm configure primitive vip ocf:heartbeat:IPaddr params ip=172.16.11.1
  3. 添加nfs文件系统
  4. # crm configure primitive mynfs ocf:heartbeat:Filesystem params device="172.16.11.21:/sharedata" directory="/data/mydata" fstype="nfs" op start timeout=60s op stop timeout=60s
  5. # crm configure commit
  6. 查看定义的资源
  7. # crm configure show
  8. node node1
  9.     attributes standby="off"
  10. node node2
  11.     attributes standby="off"
  12. primitive mynfs ocf:heartbeat:Filesystem
  13.     params device="172.16.11.21:/sharedata" directory="/data/mydata" fstype="nfs"
  14.     op start interval="0" timeout="60s"
  15.     op stop interval="0" timeout="60s"
  16. primitive vip ocf:heartbeat:IPaddr
  17.     params ip="172.16.11.1"
  18. property $id="cib-bootstrap-options"
  19.     dc-version="1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f"
  20.     cluster-infrastructure="openais"
  21.     expected-quorum-votes="2"
  22.     stonith-enabled="false"
  23.     last-lrm-refresh="1344248837"
  24.     no-quorum-policy="ignore"
  25. 查看资源工作状态
  26. # crm status
  27. ============
  28. Last updated: Tue Aug 7 14:14:06 2012
  29. Stack: openais
  30. Current DC: node2 - partition with quorum
  31. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
  32. 2 Nodes configured, 2 expected votes
  33. 2 Resources configured.
  34. ============
  35.  
  36. Online: [ node1 node2 ]
  37.  
  38.  vip (ocf::heartbeat:IPaddr): Started node1
  39.  mynfs (ocf::heartbeat:Filesystem): Started node2
  40. 此时mynfs资源在node2上启动,去node2节点查看/data/mydata目录
  41. # ls /data/mydata/
  42. ibdata1 ib_logfile0 ib_logfile1 lost+found mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index node1.err node2.err performance_schema test testdb
  43. node2上有数据文件,而node1没有数据文件
  44. 让node2变为被动节点
  45. # crm node standby
  46. 此时查看资源工作状态
  47. # crm status
  48. ============
  49. Last updated: Tue Aug 7 14:21:08 2012
  50. Stack: openais
  51. Current DC: node2 - partition with quorum
  52. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
  53. 2 Nodes configured, 2 expected votes
  54. 2 Resources configured.
  55. ============
  56.  
  57. Node node2: standby
  58. Online: [ node1 ]
  59.  
  60.  vip (ocf::heartbeat:IPaddr): Started node1
  61.  mynfs (ocf::heartbeat:Filesystem): Started node1
  62. 此时mynfs工作在node1节点上
  63. 去node1节点的/data/mydata目录验证一下
  64. # ls /data/mydata/
  65. ibdata1 ib_logfile0 ib_logfile1 lost+found mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index node1.err node2.err performance_schema test testdb
  66. 此是node1的/data/mydata/目录已经有数据文件,而node2的/data/mydata/目录中没有任何数据文件
  67. 添加mysqld服务
  68. vip、mynfs和mysqld资源的启动顺序为vip-->mynfs-->mysqld
  69. # crm configure primitive mysqld lsb:mysqld
  70. # crm configure commit
  71. 定义排列约束
  72. # crm configure collocation mysqld_with_mynfs_vip inf: vip mynfs mysqld
  73. 定义顺序约束
  74. # crm configure order mysqld_after_mynfs_after_vip mandatory: vip mynfs mysqld
  75. # crm configure commit
  76. 查看集群信息库中定义的collocation和order约束
  77. # crm configure show xml
  78. //collocation约束配置信息//
  79.  <rsc_colocation id="mysqld_with_mynfs_vip" score="INFINITY">
  80.         <resource_set id="mysqld_with_mynfs_vip-0">
  81.           <resource_ref id="vip"/>
  82.           <resource_ref id="mynfs"/>
  83.           <resource_ref id="mysqld"/>
  84. //order约束配置信息//
  85. <rsc_order id="mysqld_after_mynfs_after_vip" score="INFINITY">
  86.         <resource_set id="mysqld_after_mynfs_after_vip-0">
  87.           <resource_ref id="vip"/>
  88.           <resource_ref id="mynfs"/>
  89.           <resource_ref id="mysqld"/>
  90. 查看资源工作状态
  91. # crm status
  92. ============
  93. Last updated: Tue Aug 7 14:59:46 2012
  94. Stack: openais
  95. Current DC: node2 - partition with quorum
  96. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
  97. 2 Nodes configured, 2 expected votes
  98. 3 Resources configured.
  99. ============
  100.  
  101. Online: [ node1 node2 ]
  102.  
  103.  vip (ocf::heartbeat:IPaddr): Started node1
  104.  mynfs (ocf::heartbeat:Filesystem): Started node1
  105.  mysqld (lsb:mysqld): Started node1
  106. 让node1节点切换为备用状态
  107. # crm node1 standby
  108. 查看资源工作状态
  109. # crm status
  110. ============
  111. Last updated: Tue Aug 7 15:03:26 2012
  112. Stack: openais
  113. Current DC: node2 - partition with quorum
  114. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f
  115. 2 Nodes configured, 2 expected votes
  116. 3 Resources configured.
  117. ============
  118.  
  119. Node node1: standby
  120. Online: [ node2 ]
  121.  
  122.  vip (ocf::heartbeat:IPaddr): Started node2
  123.  mynfs (ocf::heartbeat:Filesystem): Started node2
  124.  mysqld (lsb:mysqld): Started node2
  125. 此时mysqld工作在node2节点上,去node2节点验证一下
  126. # mysql
  127. Welcome to the MySQL monitor. Commands end with ; or g.
  128. Your MySQL connection id is 1
  129. Server version: 5.5.24-log MySQL Community Server (GPL)
  130.  
  131. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  132.  
  133. Oracle is a registered trademark of Oracle Corporation and/or its
  134. affiliates. Other names may be trademarks of their respective
  135. owners.
  136.  
  137. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
  138.  
  139. mysql> show databases;
  140. +---------------------+
  141. | Database |
  142. +---------------------+
  143. | information_schema |
  144. | #mysql50#lost+found |
  145. | mysql |
  146. | performance_schema |
  147. | test |
  148. | testdb |
  149. +---------------------+
  150. 6 rows in set (0.06 sec)
  151.  
  152. mysql>
  153.  由此资源已经成功生效;
以上就是基于corosync+pacemaker+nfs实现mysql的过程。
阅读(1982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~