drbd(distributedreplicatedblockdevice)是一个基于块设备级别在远程服务器直接同步和镜像数据的开源软件,类似于raid1数据镜像,通常配合keepalived、heartbeat等ha软件来实现高可用性。
drbd是一种块设备,可以被用于高可用(ha)之中.它类似于一个网络raid-1功能,当你将数据写入本地文件系统时,数据还将会被发送到网络中另一台主机上.以相同的形式记录在一个文件系统中。
本地(master)与远程主机(backup)的保证实时同步,如果本地系统出现故障时,远程主机上还会保留有一份相同的数据,可以继续使用.在高可用(ha)中使用drbd功能,可以代替使用一个共享盘阵.因为数据同时存在于本地主机和远程主机上,切换时,远程主机只要使用它上面的那份备份数据。
一、实施环境
系统版本:centos 6.5
drbd版本: drbd-8.3.15
keepalived:keepalived-1.1.15
master:192.168.10.128
backup:192.168.10.130
二、初始化配置
1) 在128、130两台服务器/etc/hosts里面都添加如下配置:
192.168.149.128 node1
192.168.149.130 node2
2) 优化系统kernel参数,直接上sysctl.conf配置如下:
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
|
net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 net.ipv4.tcp_max_tw_buckets = 10000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65530 net.ipv4.icmp_echo_ignore_all = 1 |
3)两台服务器分别添加一块设备,用于drbd主设备存储,我这里为/dev/sdb 20g硬盘;
执行如下命令:
mkfs.ext3 /dev/sdb ;dd if=/dev/zero of=/dev/sdb bs=1m count=1;sync
三、drbd安装配置
yum方式安装:
rpm -uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
yum -y install drbd83* kmod-drbd83 ; modprobe drbd
源码安装方式:
http://oss.linbit.com/drbd/8.4/drbd-8.4.4.tar.gz
./configure --prefix=/usr/local/drbd --with-km
make kdir=/usr/src/kernels/2.6.32-504.el6.x86_64/
make install
cp drbd/drbd.ko /lib/modules/`uname -r`/kernel/lib/
yum方式和源码方式都需要执行:modprobe drbd 加载drbd模块。
安装完成并加载drbd模块后,vi修改/etc/drbd.conf配置文件,内容如下:
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
|
global { usage-count yes; } common { syncer { rate 100m; } } resource r0 { protocol c; startup { } disk { on-io-error detach; #size 1g; } net { } on node1 { device /dev/drbd0; disk /dev/sdb; address 192.168.10.128:7898; meta-disk internal; } on node2 { device /dev/drbd0; disk /dev/sdb; address 192.168.10.130:7898; meta-disk internal; } } |
配置修改完毕后执行如下命令初始化:
drbdadm create-md r0 ;/etc/init.d/drbd restart ;/etc/init.d/drbd status
如下图:
以上步骤,需要在两台服务器都执行,两台都配置完毕后,在node2从上面执行如下命令:/etc/init.d/drbd status 看到如下信息,表示目前两台都为从,我们需要设置node1为master,命令如下:
drbdadm -- --overwrite-data-of-peer primary all
mkfs.ext4 /dev/drbd0
mkdir /app ;mount /dev/drbd0 /app
自此,drbd配置完毕,我们可以往/app目录写入任何东西,当master出现宕机或者其他故障,手动切换到backup,数据没有任何丢失,相当于两台服务器做网络raid1。
四、keepalived配置
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz ; tar -xzvf keepalived-1.1.15.tar.gz ;cd keepalived-1.1.15 ; ./configure ; make ;make install
dir=/usr/local/ ;cp $dir/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ ; cp $dir/etc/sysconfig/keepalived /etc/sysconfig/ ;
mkdir -p /etc/keepalived ; cp $dir/sbin/keepalived /usr/sbin/
两台服务器均安装keepalived,并进行配置,首先在node1(master)上配置,keepalived.conf内容如下:
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
|
! configuration file for keepalived global_defs { router_id lvs_devel } vrrp_script check_mysql { script "/data/sh/check_mysql.sh" interval 5 } vrrp_instance vi_1 { state master interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 192.168.10.100 } track_script { check_mysql } } |
然后创建check_mysql.sh检测脚本,内容如下:
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
|
#!/bin/sh a=` ps -c mysqld --no-header | wc -l` if [ $a - eq 0 ]; then /bin/umount /app/ drbdadm secondary r0 killall keepalived fi 添加node2(backup)上配置,keepalived.conf内容如下: ! configuration file for keepalived global_defs { router_id lvs_devel } vrrp_sync_group vi { group { vi_1 } notify_master /data/sh/master .sh notify_backup /data/sh/backup .sh } vrrp_instance vi_1 { state backup interface eth0 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type pass auth_pass 1111 } virtual_ipaddress { 192.168.10.100 } } |
创建master.sh检测脚本,内容如下:
1
2
3
4
|
#!/bin/bash drbdadm primary r0 /bin/mount /dev/drbd0 /app/ /etc/init .d /mysqld start |
创建backup.sh检测脚本,内容如下:
1
2
3
4
|
#!/bin/bash /etc/init .d /mysqld stop /bin/umount /dev/drbd0 drbdadm secondary r0 |
发生脑裂恢复步骤如下:
master执行命令:
drbdadm secondary r0
drbdadm -- --discard-my-data connect r0
drbdadm -- --overwrite-data-of-peer primary all
backup上执行命令:
drbdadm secondary r0
drbdadm connect r0
原文链接:http://www.cnblogs.com/linxizhifeng/p/6993878.html