HA部署方案:
-
hb v1 + haresourece
-
hb v2 + crm
-
hb v3 + pacemaker + cluster-glue
-
corosync + pacemaker + cluster-glue
-
cman + rgmanger
-
keepalived + script
下面讲述是利用corosync + pacemaker部署高可用集群。
corosync相关知识
corosync早期是OpenAIS的组成部分,OpenAIS定义如何实现 Messaging Layer。08年corsync被独立出来。08以前红帽自己借助于OpenAIS自主实现的cman,实现了投票机制。crosync2.0之前没有投票系统,2.0以后实现了原生态投票系统。
corosync识别节点是通过authkey来实现的,不同于heartbeat v1 v2,corosync的authkey是通过corosync-keygen命令生成的,具有更高的安全性。
一、corosync + pacemaker搭建高可用集群
环境说明:
1 2 3 4 | 高可用节点(使用2个节点): 172.16.10.22 node2.example.com node2 172.16.10.33 node3.example.com node3 vip地址:172.16.10.200 |
前提条件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1、所有节点的主机名称和对应的IP地址解析服务可以正常工作,且每个节点的主机名称需要跟 " uname -n“命令的结果保持一致; 因此,需要保证两个节点上的 /etc/hosts 文件均为下面的内容:(由ansible提供) 172.16.10.22 node2.example.com node2 172.16.10.33 node3.example.com node3 为了使得重新启动系统后仍能保持如上的主机名称,还分别需要在各节点执行类似如下的命令: Node2: # sed -i 's@\(HOSTNAME=\).*@\1node2.example.com@g' /etc/sysconfig/network # hostname node2.example.com Node3: # sed -i 's@\(HOSTNAME=\).*@\1node3.example.com@g' /etc/sysconfig/network # hostname node3.example.com 2、设置高可用节点之间的的时间同步 可以使用时间同步服务器 3、集群节点之间可以 ssh 互信通信 |
安装 corosync + pacemaker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # 使用 ansible Role 完成 crosync + pacemaker 的安装和配置 # 下面是 Role 的目录树 # /root/corosync |-- ha.yml |-- roles | |-- common | | |-- default | | |-- files | | | `-- hosts | | |-- handlers | | |-- meta | | |-- tasks | | | `-- main.yml | | |-- templates | | `-- vars | |-- crmsh | | |-- files | | | |-- crmsh-1.2.6-4.el6.x86_64.rpm | | | `-- pssh-2.3.1-2.el6.x86_64.rpm | | `-- tasks | | `-- main.yml | `-- ha | |-- default | |-- files | | |-- authkey | | `-- corosync.conf | |-- handlers | | `-- main.yml | |-- meta | |-- tasks | | `-- main.yml | |-- templates | `-- vars `-- site.yml |
下面是各个文件的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 当前所在的目录是: /root/corosync 以下出现是相对路径 # cat ha.yml 是在最终执行的Playbokks - name: install and config corosync remote_user: root hosts: hbhosts roles: - common - ha - crmsh # 这是一个空文件,是ansible格式要求 cat site.yml # comman角色的任务,实现时间同步,提供hosts文件 # cat roles/common/tasks/main.yml - name: hosts file copy: src=hosts dest= /etc/hosts - name: sync time cron : name= "sync time" minute= "*/3" job= "/usr/sbin/ntpdate 172.16.0.1 &> /dev/null" # cat roles/common/files/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.10.22 node2.example.com node2 172.16.10.33 node3.example.com node3 # 这是提供 pacemaker 配置接口的软件的安装 crmsh pssh # cat roles/crmsh/tasks/main.yml - name: copy pssh and crmsh copy: src={ { item }} dest= /tmp/ with_items: - pssh-2.3.1-2.el6.x86_64.rpm - crmsh-1.2.6-4.el6.x86_64.rpm - name: install pssh and crmsh yum: name={ { item }} state=present with_items: - /tmp/pssh-2 .3.1-2.el6.x86_64.rpm - /tmp/crmsh-1 .2.6-4.el6.x86_64.rpm # 软件列表信息 # ll roles/crmsh/files/ total 536 -rw-r--r-- 1 root root 495332 Sep 15 2013 crmsh-1.2.6-4.el6.x86_64.rpm -rw-r--r-- 1 root root 49960 Sep 15 2013 pssh-2.3.1-2.el6.x86_64.rpm # 安装和配置 corosync ll roles/ha/tasks/main.yml -rw-r--r-- 1 root root 472 Sep 20 16:50 roles /ha/tasks/main .yml [root@node1 corosync] # cat roles/ha/tasks/main.yml - name: installed corosync and pacemaker yum: name={ { item }} state=present with_items: - corosync - pacemaker tags: inst - name: auth key file copy: src=authkey dest= /etc/corosync/authkey owner=root group=root mode=0400 tags: authkey - name: configration file copy: src=corosync.conf dest= /etc/corosync/corosync .conf tags: conf notify: - restart corosync - name: start corosync service: name=corosync state=started enabled=no tags: start [root@node1 corosync] # cat roles/ha/handlers/main.yml - name: restart corosync service: name=corosync state=restarted # corosync的配置文件 cat roles/ha/files/corosync.conf # Please read the corosync.conf.5 manual page compatibility: whitetank totem { version: 2 secauth: on threads: 0 interface { ringnumber: 0 bindnetaddr: 172.16.0.0 mcastaddr: 228.10.10.10 mcastport: 5405 ttl: 1 } } 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 } # corosync认证文件 ll roles/ha/files/authkey -r-------- 1 root root 128 Sep 20 16:04 roles /ha/files/authkey |
执行 ansible-playbokks ha.yml,就可完成以上的配置。
二、corosync + pacemaker + NFS 实现MariaDB的高可用
1、在node2和node3上安装MariaDB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # 这里使用通用二进制包安装 # 数据目录: /mydata/data #### 在Node2 Node3上: # 添加用户 groupadd -r mysql useradd -r -g mysql mysql # 二进制格式安装mariadb tar xf mariadb-10.0.13-linux-x86_64. tar .gz -C /usr/local/ cd /usr/local/ ln -sv mariadb-10.0.13-linux-x86_64 mysql `mysql ' -> `mariadb-10.0.13-linux-x86_64' cd mysql cp support-files /my-large .cnf /etc/my .cnf # 修改 vim /etc/my.cnf # 在mysqld下添加 datadir=/mydata/data # 提供服务脚本 cp support-files /mysql .server /etc/init .d /mysqld chmod +x /etc/init .d /mysqld |
2、提供nfs共享服务
nfs服务端:172.16.10.11
1 2 3 4 | yum install nfs-utils /mysqldata 172.16.0.0 /16 (rw,no_root_squash) mkdir /mysqldata setfacl -m u:493:rwx /mysqldata/ # 493是mysql用户的UID |
在Node2:
完成以上任务就可以初始化数据库了,如下图:
初始化完成,就可以登录数据库了,并授权:
说明:此处授权的的用户不正确,应该是
配置完成,在Node2上,验证:
完成后,继续:
1 2 3 4 5 | 在Node2和Node3上确保: mysqld 是关闭状态; mysqld 开机不能启动; 卸载 nfs 文件系统。 |
3、配置corosync的资源,实现高可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # 以下是定义的资源和资源约束 crm(live) # configure show node node2.example.com node node3.example.com primitive mysql-storge ocf:heartbeat:Filesystem \ params device= "172.16.10.11:/mysqldata" directory= "/mydata/" fstype= "nfs" \ op start timeout= "60s" interval= "0" \ op stop timeout= "60s" interval= "0" primitive mysqlservice lsb:mysqld primitive vip ocf:heartbeat:IPaddr \ params ip= "172.16.10.200" group mysql vip mysql-storge mysqlservice order fist_vip_storge_service_end Mandatory: vip mysql-storge mysqlservice property $ id = "cib-bootstrap-options" \ dc -version= "1.1.10-14.el6-368c726" \ cluster-infrastructure= "classic openais (with plugin)" \ expected-quorum-votes= "2" \ stonith-enabled= "0" \ no-quorum-policy= "ignore" |
验证:
远程连接:
由于授权的问题,在这里需要重新授权,远程端才可以登录。
远程登录访问:
此时,如果有一个节点,出现故障?
远端是否可以正常访问?
三、corosync + pacemaker + DRBD 实现MariaDB的高可用
1、corosync的配置如上
2、配置DRBD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 具体详细配置可参考:http: //guoting .blog.51cto.com /8886857/1553462 1、准备好共享磁盘 这里使用的是 /dev/sda5 。Node2 和 Node3上都是。 2、安装软件 rpm -ivh drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm 3、配置文件 Node2: 1) 配置 /etc/drbd .d /global-common .conf global { usage-count no; # 是否为drbd官方收集数据 # minor-count dialog-refresh disable-ip-verification } # common是各个资源共用的选项 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"; # 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 120; #degr-wfc-timeout 120; } disk { on-io-error detach; # 发生i/o错误的处理方法,detach将镜像磁盘直接拔除 #fencing resource-only; } net { cram-hmac-alg "sha1" ; shared-secret "mydrbdlab" ; } syncer { rate 1000M; } } 2) 定义一个资源 /etc/drbd .d /mysql .res,内容如下: resource mysql { on node2.example.com { device /dev/drbd0 ; disk /dev/sda5 ; address 172.16.10.22:7789; meta-disk internal; } on node3.example.com { device /dev/drbd0 ; disk /dev/sda5 ; address 172.16.10.33:7789; meta-disk internal; } } 3) scp /etc/drbd .d/* node3.example.com: /etc/drbd .d ############################################################################## 在两个节点上初始化已定义的资源并启动服务 1)初始化资源,在 Node2 和 Node3 上分别执行: drbdadm create-md test 2)启动服务,在 Node3 和 Node2 上分别执行: /etc/init .d /drbd start 3)drbdadm primary --force mysql ############################################################################# 完成上面的步骤后,就可以使用 /dev/drbd0 设备了,可以正常的分区挂载使用。 |
3、初始化mysql,并授权
此时,Node2应降级为从节点(drbdadm secondary mysql)。Node3升级为主节点(drbdadm primary mysql),测试:
完成后,继续:
1 2 3 4 5 6 7 8 | 在Node2和Node3上确保: mysqld 是关闭状态; mysqld 开机不能启动; 卸载 drbd 文件系统; 确保 drbd 服务关闭; 确保 drbd 开机不能启动; 保证drbd都为从节点。 |
4、配置corosync的资源,实现高可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | crm(live)configure # show node node2.example.com node node3.example.com primitive myip ocf:heartbeat:IPaddr \ params ip= "172.16.10.200" primitive mysql_drbd ocf:linbit:drbd \ params drbd_resource= "mysql" \ op monitor role= "Master" interval= "10s" timeout= "60s" \ op monitor role= "Slave" interval= "20s" timeout= "60s" \ op start timeout= "240s" interval= "0" \ op stop timeout= "100s" interval= "0" primitive mysql_service lsb:mysqld \ op monitor interval= "20s" timeout= "30s" primitive mysql_storage ocf:heartbeat:Filesystem \ params device= "/dev/drbd0" directory= "/mydata" fstype= "ext4" \ op start timeout= "60s" interval= "0" \ op stop timeout= "60s" interval= "0" group mysqld myip mysql_storage mysql_service ms ms_mysql_drbd mysql_drbd \ meta master-max= "1" master-node-max= "1" clone-max= "2" clone-node-max= "1" \ notify= "True" colocation mysql_storage-with-ms_mysql_drbd_master \ inf: mysql_storage ms_mysql_drbd:Master colocation mysql_storage-with-mysql_service inf: mysql_service mysql_storage order fist_ip_storage_service_end Mandatory: myip mysql_storage mysql_service order mysql_storage-after-ms_mysql_drbd_master \ Mandatory: ms_mysql_drbd:promote mysql_storage:start property $ id = "cib-bootstrap-options" \ dc -version= "1.1.10-14.el6_5.3-368c726" \ cluster-infrastructure= "classic openais (with plugin)" \ expected-quorum-votes= "2" \ stonith-enabled= "0" \ no-quorum-policy= "ignore" \ last-lrm-refresh= "1411213570" |
资源和资源之间的约束如下图:
验证:
此时,如果有一个节点,出现故障?
四、corosync + pacemaker + iSCSI 实现MariaDB的高可用
1、corosync的配置如上
2、配置iSCSI 服务端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 具体详细配置可参考:http: //guoting .blog.51cto.com /8886857/1556487 # 192.168.1.11作为 iSCSI服务器端: yum -y install scsi-target-utils # 修改配置文件/etc/tgt/targets.conf <target iqn.2014-09.com.example:mysql.ha> backing-store /dev/sda5 initiator-address 172.16.0.0 /16 < /target > # 启动服务 service tgtd start chkconfig tgtd on |
完成以上之后,iscsi服务端配置完成。
3、配置iSCSI 客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 在Node2 Node3上: yum install iscsi-initiator-utils # 客户端管理工具 # 以下配置是非必需的,默认情况下,会有一个默认的iqn名称 echo "InitiatorName=`iscsi-iname -p iqn.2014-09.com.example`" \ > /etc/iscsi/initiatorname .iscsi echo "InitiatorAlias=ha" >> /etc/iscsi/initiatorname .iscsi service iscsi start chkconfig iscsi on iscsiadm -m discovery -t sendtargets -p 172.16.10.11 iscsiadm -m node -T iqn.2014-09.com.example:mysql.ha -p 172.16.10.11 -l |
完成以上任务后,本地会多出一块向本地磁盘的设备。
可以对这块磁盘分区,格式化,挂载使用。
在Node3测试:
完成后,继续:
1 2 3 4 5 6 | 在Node2和Node3上确保: mysqld 是关闭状态; mysqld 开机不能启动; 卸载文件系统; 保证drbd都为从节点。 |
4、配置corosync的资源,实现高可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | crm(live)configure # show node node2.example.com node node3.example.com primitive mysql-storge ocf:heartbeat:Filesystem \ params device= "/dev/sdb1" directory= "/mydata/" fstype= "ext4" \ op start timeout= "60s" interval= "0" \ op stop timeout= "60s" interval= "0" primitive mysqlservice lsb:mysqld primitive vip ocf:heartbeat:IPaddr \ params ip= "172.16.10.200" group mysql vip mysql-storge mysqlservice order first_vip_storge_service_end Mandatory: vip mysql-storge mysqlservice property $ id = "cib-bootstrap-options" \ dc -version= "1.1.10-14.el6_5.3-368c726" \ cluster-infrastructure= "classic openais (with plugin)" \ expected-quorum-votes= "2" \ stonith-enabled= "0" \ no-quorum-policy= "ignore" \ last-lrm-refresh= "1411224183" |
验证:
此时,如果有一个节点,出现故障?
到此,配置全部完成。