drbd+mysql+keepalived实现高可用测试


一、Drbd安装配置及配置

drbd-8.3.10.tar.gz
 
1、安装
./configure --prefix=/usr/local/drbd/ —with-km
Make && make install
2、编辑配置文件
Vim /usr/local/drbd/etc/drbd.d/global_common.conf
global {
        usage-count yes;
        # minor-count dialog-refresh disable-ip-verification
}

common {syncer {rate 10M;} }
resource r0 {
        protocol C;

        handlers {
                pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /u
sr/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; /u
sr/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 degr-wfc-timeout outdated-wfc-timeout wait-after-s
b
        }

        disk {
                # on-io-error fencing use-bmbv no-disk-barrier no-disk-flushes
                # no-disk-drain no-md-flushes max-bio-bvecs
        }

        net {
             //主备之间通信使用的算法
             cram-hmac-alg sha1;
             shared-secret "hello";
                # sndbuf-size rcvbuf-size timeout connect-int ping-int ping-time
out max-buffers
                # max-epoch-size ko-count allow-two-primaries cram-hmac-alg shar
ed-secret
                # after-sb-0pri after-sb-1pri after-sb-2pri data-integrity-alg n
o-tcp-cork
        }

        syncer {
                # rate after al-extents use-rle cpu-mask verify-alg csums-alg
        }
        on server01.com {
                device /dev/drbd0;
                disk /dev/hdb1;
                address 192.168.100.5:7788;
                meta-disk internal;
              }
        on server02.com {
                device /dev/drbd0;
                disk /dev/hdb1;
                address 192.168.100.6:7788;
                meta-disk internal;
              }
}

3、创建启动脚步
Cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/init.d/drbd
Chconfig —add drbd
Chconfig drbd on
4、修改主机名
server01上
Shell>Hostname  server01.com
Shell>Vim /etc/sysconfig/network
HOSTNAME=server01.com
server02上:
Shell>Hostname  server02.com
Shell>Vim /etc/sysconfig/network
HOSTNAME=server02.com
5、修改  hosts文件,server01.com与server02.com主机一致
Shell> vim /etc/hosts
192.168.100.5 server01.com
192.168.100.6 server02.com

6、创建供drbd记录信息的数据块,r0是我们配置文件中定义的
[root@server01 ~]# drbdadm create-md r0
[root@server02 ~]# drbdadm create-md r0
7、启动drbd
[root@server01 ~]# /etc/init.d/drbd start
[root@server02 ~]# /etc/init.d/drbd start
出看drbd的状态
[root@server01 ~]# cat /proc/drbd
version: 8.3.10 (api:88/proto:86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603378f9017b build by root@server01, 2011-06-08 17:18:04
 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:2097012

8、初始化一方为主机
[root@server01 ~]# drbdsetup /dev/drbd0 primary -o

[root@server01 ~]# cat /proc/drbd
version: 8.3.10 (api:88/proto:86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603378f9017b build by root@server01, 2011-06-08 17:18:04
 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
    ns:95232 nr:0 dw:0 dr:95232 al:0 bm:5 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:2001780
        [>....................] sync'ed:  4.7% (2001780/2097012)K
        finish: 0:03:08 speed: 10,580 (10,580) K/sec
查看状态显示 以主机为准同步磁盘数据

[root@server01 ~]# cat /proc/drbd
version: 8.3.10 (api:88/proto:86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603378f9017b build by root@server01, 2011-06-08 17:18:04
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
    ns:2097012 nr:0 dw:0 dr:2097012 al:0 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
状态显示数据已经同步完毕

9、格式化文件系统
[root@server01 ~]# mkfs -t ext3 /dev/drbd0
挂载文件系统
[root@server01 ~]# mkdir /data
[root@server01 ~]# mount /dev/drbd0 /data/
8、数据测试
创建一个10M的文件,查看备机是否存在
[root@server01 data]# dd if=/dev/zero of=test count=20480

9、主备切换
在主机上执行
[root@server01 ~]# umount /data/
[root@server01 ~]# drbdadm secondary all
这时查看drbd状态
[root@server01 ~]# cat /proc/drbd
version: 8.3.10 (api:88/proto:86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603378f9017b build by root@server01, 2011-06-08 17:18:04
 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
    ns:3004452 nr:0 dw:907440 dr:2097569 al:241 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
在备机上执行
[root@server02 drbd-8.3.10]# drbdadm primary all
查看状态
[root@server02 drbd-8.3.10]# cat /proc/drbd
version: 8.3.10 (api:88/proto:86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603378f9017b build by root@server02, 2011-06-08 17:19:40
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:0 nr:3004452 dw:3004452 dr:0 al:0 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0

挂载drbd分区
[root@server02 ~]# mkdir /data
[root@server02 ~]# mount /dev/drbd0 /data/

[root@server02 data]# ll  -h
total 11M
drwx------ 2 root root 16K Jun  8 17:50 lost+found
-rw-r--r-- 1 root root 10M Jun  8 17:55 test
可以看到刚才在server01.com上创建的test文件,至此安装已经完毕


二、安装mysql数据库
.mysql安装省略
数据库安装完成,将安装好的mysql拷贝到另一台机器

三、使用keepalived 做ha
1、安装keeplaived
Yum  install -y openssl  openssl-devel
./configure --prefix=/usr/local/keepalived
Make && make install
2、配置文件如下
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
virtual_router_id 51
#主备之间配置唯一的区别
    priority 250
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.7
    }
}



3、启动keepalived
/usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
查看vip状态,VIP是根据在keepalived中priority值决定帮定在master还是backup上
[root@server01 keepalived-1.2.2]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:95:bf:9e brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.5/24 brd 192.168.100.255 scope global eth0
inet 192.168.100.7/32 scope global eth0
4、测试结果
down掉server01.com上网卡,vip会切换到server02.com上。
Server01.com恢复正常,vip自动切换到server01.com上(因为server01.com优先级要高;如果不想这样,可以在server01.com加上nopreempt)


四、drbd与keepalived 结合
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 250
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.7
    }
notify_master "/usr/local/sbin/to-master"
notify_backup "/usr/local/sbin/to-backup"
notify_fault "/usr/local/sbin/to-backup"
}


To-master脚本如下
#!/bin/bash
while :
do
drbd_status=`service drbd status|grep Inconsistent | gawk '{print $4}'`
if [ -n "$drbd_status" ]; then
echo "drbd's status is inconsisten,waiting..."
sleep 1
else
echo "drbd's status is ok,namenode will be changing"
break
fi
done

while :
do
if [ -d /data/mysql ]; then
#service mysqld stop
service mysqld start
break
else
drbdadm primary r0
mount /dev/drbd0 /data
sleep 1
service mysqld start
fi
done


drbd_status=`service drbd status|grep StandAlone | gawk '{print $2}'`
if [ -n "$drbd_status" ]; then
vip_status=`ip addr |grep eth0|grep "192.168.100.7"`
   if [[ ! -z ${vip_status} ]];then
     for i in $(seq 3)
       do
        drbdadm connect all
        sleep 2
     done
   fi
fi


To-backup脚步如下
#!/bin/bash
if [ -d /data/mysql ];then
service mysqld stop
pkill -9 mysql
sleep 1
fuser -ck /data/
umount -l /dev/drbd0
drbdadm secondary all
else
drbd_status=`service drbd status|grep StandAlone | gawk '{print $2}'`
if [ -n "$drbd_status" ]; then
drbdadm secondary all

fi
fi


 

你可能感兴趣的:(数据库,mysql,keepalived,Mysql主从,drbd)