红联Linux门户
Linux帮助

Ubuntu使用corosync+pacemaker+drbd实现MySQL的HA(1)

发布时间:2015-10-14 10:17:42来源:linux网站作者:chinagissoft

环境

Ubuntu 14.04.2
MySQL1:192.168.12.101
MySQL2:192.168.12.102
VIP:192.168.12.200

每台MySQL机器都新添加/dev/sdb,当然也可以使用已有的sda做分区。


实施步骤

(1代表mysql1,2代表mysql2,以下类同)

软件安装(1+2)

sudo apt-get install pacemaker corosync heartbeat crmsh cluster-glue resource-agents mysql-server  sysv-rc-conf
drbd8-utils

说明:sysv-rc-conf是一个可视化管理服务器自启动的工具,属于可选。


1、保证两台机器时间同步(1+2)


2、SSH可以使用root无密码访问(1+2)

修改/etc/ssh/sshd_config让ssh支持以root用户登录, 并重启 service ssh restart

#PermitRootLogin without-password
PermitRootLogin yes

机器1:

ssh-keygen -t rsa -P ''

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.103

机器2:

ssh-keygen -t rsa -P ''

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.101

测试双方是否可以无密码访问

ssh root@192.168.12.103


3、在一个节点上生成corosync key, 然后拷贝到其他所有节点. 生成key时会默认会调用/dev/random随机数设备,一旦系统中断的IRQS的随机数不够用,将会产生大量的等待时间,所以将random替换成urandom(1)

mv /dev/{random,random.bak}
ln -s /dev/urandom /dev/random


4、生成corosync的key文件,然后同步到另外一台机器(1)

corosync-keygen
scp /etc/corosync/authkey root@192.168.12.103:/etc/corosync/
chown root:root /etc/corosync/authkey
chmod 400 /etc/corosync/authkey


5、取消corosync、pacemaker、drbd的开机启动(1+2)

sysv-rc-conf

使用空格把drbd,pacemaker,corosync有X的地方取消掉,q退出

Ubuntu使用corosync+pacemaker+drbd实现MySQL的HA(1)


6、修改corosync.conf配置文件(1+2)

root@mysql1:~# cat /etc/corosync/corosync.conf
# Please read the openais.conf.5 manual page

totem {
version: 2

# How long before declaring a token lost (ms)
token: 10000

# How many token retransmits before forming a new configuration
token_retransmits_before_loss_const: 10

# How long to wait for join messages in the membership protocol (ms)
join: 60

# How long to wait for consensus to be achieved before starting a new round of membership configuration (ms)
consensus: 3600

# Turn off the virtual synchrony filter
vsftype: none

# Number of messages that may be sent by one processor on receipt of the token
max_messages: 20

# Limit generated nodeids to 31-bits (positive signed integers)
clear_node_high_bit: yes

# Disable encryption
secauth: on

# How many threads to use for encryption/decryption
threads: 0

# Optionally assign a fixed node id (integer)
# nodeid: 1234

# This specifies the mode of redundant ring, which may be none, active, or passive.
rrp_mode: active

interface {
# The following values need to be set based on your environment
ringnumber: 0
bindnetaddr: 192.168.12.0
mcastaddr: 226.94.1.1
mcastport: 5405
}
}

amf {
mode: disabled
}

service{
ver:1
name:   pacemaker
}
quorum {
# Quorum for the Pacemaker Cluster Resource Manager
provider: corosync_votequorum
expected_votes: 1
}

aisexec {
user:   root
group:  root
}

logging {
fileline: off
to_stderr: yes
to_logfile: no
to_syslog: yes
syslog_facility: daemon
debug: off
timestamp: on
logger_subsys {
subsys: AMF
debug: off
tags: enter|leave|trace1|trace2|trace3|trace4|trace6
}
}


7、配置/etc/default/corosync(1+2)

root@mysql2:~# cat /etc/default/corosync
# start corosync at boot [yes|no]
START=yes

重启corosync服务

查看corosync状态

corosync-cfgtool -s
corosync-quorumtool -l

重启pacemaker服务
查看状态

root@mysql2:~# crm status
Last updated: Fri Aug 14 10:01:57 2015
Last change: Fri Aug 14 09:17:34 2015 via crm_attribute on mysql1
Stack: corosync
Current DC: mysql1 (1084755045) - partition with quorum
Version: 1.1.10-42f2063
2 Nodes configured
0 Resources configured

Online: [ mysql1 mysql2 ]


8、为drbd创建存储磁盘(1+2)

fdisk /dev/sdb


9、创建drbd的mysql资源(1+2)

root@mysql2:~# cat /etc/drbd.d/mysql.res
resource mysql {
device /dev/drbd0;
disk /dev/sdb1;
meta-disk internal;
on mysql1 {
address 192.168.12.101:7788;
}
on mysql2 {
address 192.168.12.103:7788;
}
syncer {
rate 10M;
}
}

添加drbd到/etc/modules

echo -e 'drbd # -> added' >> /etc/modules

dump出mysql信息

root@mysql2:~# drbdadm dump mysql
# resource mysql on mysql2: not ignored, not stacked
# defined at /etc/drbd.d/mysql.res:1
resource mysql {
on mysql1 {
volume 0 {
device   /dev/drbd0 minor 0;
disk /dev/sdb1;
meta-diskinternal;
}
address  ipv4 192.168.12.101:7788;
}
on mysql2 {
volume 0 {
device   /dev/drbd0 minor 0;
disk /dev/sdb1;
meta-diskinternal;
}
address  ipv4 192.168.12.103:7788;
}
disk {
resync-rate  10M;
}
}

初始化DRBD元数据

drbdadm create-md mysql

创建/dev/drbd0设备文件,将指定的后端存储设备附加到该DRBD资源,同时建立所有节点服务器的通信连接。

drbdadm up mysql

验证

drbd-overview


10、创建drbd0文件系统(1)

drbdadm -- --overwrite-data-of-peer primary mysql

执行如下操作,该操作是在同步信息,你可以不停刷新,直至同步100%成功为止

cat /proc/drbd

对/dev/drbd0格式化文件系统
本例使用了ext4文件系统,也可以用更为高效的xfs文件系统

mkfs.ext4 /dev/drbd0

创建一个存储数据库文件的路径

mkdir -p /data/mysql

将drbd0对象mount到该路径下

mount /dev/drbd0 /data/mysql

注意:如果出现mount错误,例如;

mount: block device /dev/drbd0 is write-protected, mounting read-only mount: Wrong medium type

因为mount只能在primary一端使用,所以需要执行如下,再mount

drbdadm primary mysql

可以在mysql1机器创建一个文件夹testfile,然后umount,再将mysql1设置为secondary对mysql2进行测试

mkdir -p /data/mysql/testfile
umount /dev/drbd0
drbdadm secondary mysql


11、另外一台机器进行测试(2)

drbdadm primary mysql
mkfs.ext4 /dev/drbd0
mkdir /data/mysql
mount /dev/drbd0 /data/mysql
ll /data/mysql

如果执行ll命令可以看到新创建的testfile说明配置成功

umount /dev/drbd0
drbdadm secondary mysql


12、将mysql1设置为primary(1)

drbdadm primary mysql


CentOS 6.4下DRBD安装配置:http://www.linuxdiyf.com/linux/12186.html

Linux下DRBD配置:http://www.linuxdiyf.com/linux/11438.html