DRBD(Distributed Replicated Block Device)是一个基于块设备级别在远程服务器直接同步和镜像数据的开源软件,类似于RAID1数据镜像,通常配合keepalived、heartbeat等HA软件来实现高可用性。
DRBD是一种块设备,可以被用于高可用(HA)之中.它类似于一个网络RAID-1功能,当你将数据写入本地文件系统时,数据还将会被发送到网络中另一台主机上.以相同的形式记录在一个文件系统中。
本地(master)与远程主机(backup)的保证实时同步,如果本地系统出现故障时,远程主机上还会保留有一份相同的数据,可以继续使用.在高可用(HA)中使用DRBD功能,可以代替使用一个共享盘阵.因为数据同时存在于本地主机和远程主机上,切换时,远程主机只要使用它上面的那份备份数据。
通过本次课程的学习,大家可以熟练构建企业级MySQL+DRBD+Keepalived高性能高可用架构,满足企业网站高效的访问,有突发故障及时切换。
MySQL+DRBD+Keepalived企业实战
MASTER:192.168.77.133
SLAVE:192.168.77.166
VIP:192.168.77.189
初始化配置
1) 在128、129两台服务器/etc/hosts里面都添加如下配置:
192.168.77.133 node1
192.168.77.166 node2
每台单独划一个分区,我这里直接各增加一个大小相同的磁盘
两台服务器分别添加一块设备,用于DRBD主设备存储,我这里为/dev/sdb 20G硬盘;
执行如下命令初始化:
mkfs.ext4 /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
lsmod | grep drbd 查看drbd模块 [[email protected]_168_77_133 ~]# modprobe drbd FATAL: Module drbd not found. 需要重启系统
修改配置文件/etc/drbd.conf两台配置文件一样
global { usage-count yes; } common { syncer { rate 100M; } #同步每s的速率 } resource r0 { #节点r0 protocol C; startup { } disk { on-io-error detach; #错误的信息 #size 1G; #同步的磁盘空间 } net { #网络节点node1,node2 } on node1 { # device /dev/drbd0; #块设备 disk /dev/sdb; address 192.168.77.133:7898; meta-disk internal; } on node2 { device /dev/drbd0; disk /dev/sdb; address 192.168.77.166:7898; meta-disk internal; } }
配置修改完毕后执行如下命令初始化:
drbdadm create-md r0 ;/etc/init.d/drbd restart ;/etc/init.d/drbd status
这里主要如果报错主机名问题需要修改主机名为node1、node2
两台都启动后查看状态会发现两台都是secondary备用状态表示目前两台都为从,我们需要设置node1为master,命令如下:
[[email protected] ~]# drbdadm -- --overwrite-data-of-peer primary all [[email protected] ~]# /etc/init.d/drbd status drbd driver loaded OK; device status: version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by [email protected], 2014-11-24 14:49:06 m:res cs ro ds p mounted fstype ... sync‘ed: 44.4% (586368/1048576)K 0:r0 SyncSource Primary/Secondary UpToDate/Inconsistent C
查看为主后初始化块然后挂载:
[[email protected] ~]# mkfs.ext4 /dev/drbd0 [[email protected] ~]# mkdir /app ;mount /dev/drbd0 /app [[email protected] ~]# df -h #查看app已经挂载上 Filesystem Size Used Avail Use% Mounted on /dev/sda5 3.1G 2.8G 138M 96% / tmpfs 442M 0 442M 0% /dev/shm /dev/sda1 190M 40M 141M 23% /boot /dev/sda2 15G 55M 14G 1% /data /dev/drbd0 20G 44M 19G 1% /app
注意只需要在主上格式化drbd0然后挂载app即可
LAMP:我这里便于实验采用yum安装方式
yum install http mysql mysql-devel mysql-server php php-mysql php-devel -y
停止mysql
nohup cp /var/lib/mysql/* . -a & 后台将mysql数据copy到app目录下面
然后将my.cnf中的数据文件路径改为datadir=/app即可,然后重启mysql
登录mysql正常
模拟drbd主down机:
[[email protected] app]# poweroff
在从上查看drbd状态还是从:
[[email protected] ~]# /etc/init.d/drbd status drbd driver loaded OK; device status: version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by [email protected], 2014-11-24 14:49:06 m:res cs ro ds p mounted fstype 0:r0 WFConnection Secondary/Unknown UpToDate/DUnknown C
把从设置为主需要执行命令:
[email protected] ~]# drbdadm -- --overwrite-data-of-peer primary all [[email protected] ~]# /etc/init.d/drbd status drbd driver loaded OK; device status: version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by [email protected], 2014-11-24 14:49:06 m:res cs ro ds p mounted fstype 0:r0 WFConnection Primary/Unknown UpToDate/DUnknown C
然后创建一个挂载点app
[[email protected] ~]# mkdir /app [[email protected] ~]# mount /dev/drbd0 /app [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 29G 2.8G 25G 10% / tmpfs 442M 0 442M 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot /dev/sda5 48G 73M 46G 1% /data /dev/drbd0 20G 106M 19G 1% /app [[email protected] ~]# cd /app [[email protected] app]# ls bbs mysql mysql-bin.000011 mysql-bin.000022 mysql-bin.000033 mysql-bin.000044 cacti mysql-bin.000001 mysql-bin.000012 mysql-bin.000023 mysql-bin.000034 mysql-bin.000045 discuz mysql-bin.000002 mysql-bin.000013 mysql-bin.000024 mysql-bin.000035 mysql-bin.index ibdata1 mysql-bin.000003 mysql-bin.000014 mysql-bin.000025 mysql-bin.000036 nagios ib_logfile0 mysql-bin.000004 mysql-bin.000015 mysql-bin.000026 mysql-bin.000037 nohup.out ib_logfile1 mysql-bin.000005 mysql-bin.000016 mysql-bin.000027 mysql-bin.000038 yuning jfdeu mysql-bin.000006 mysql-bin.000017 mysql-bin.000028 mysql-bin.000039 li mysql-bin.000007 mysql-bin.000018 mysql-bin.000029 mysql-bin.000040 lijq mysql-bin.000008 mysql-bin.000019 mysql-bin.000030 mysql-bin.000041 linux mysql-bin.000009 mysql-bin.000020 mysql-bin.000031 mysql-bin.000042 lixi mysql-bin.000010 mysql-bin.000021 mysql-bin.000032 mysql-bin.000043
查看会发现down机的node1下的文件都挂载到了node2下面的app下面了,,node2下切记不要格式化,否则数据会丢失
然后修改my.cnf路经重启mysql进入数据库,会看到数据都存在
以下为DRBD脑裂手动恢复过程:主down机后主恢复后发生脑裂,,无法通信
将Node1设置为主节点并挂载测试
[[email protected] ~]#/etc/init.d/mysqld stop [[email protected] ~]# umount /app [[email protected] ~]# drbdadm primary r0 [[email protected] ~]# mount /dev/drbd0 /app
将Node2设置为从节点并丢弃资源数据
[[email protected] ~]# drbdadm secondary r0 [email protected] ~]# drbdadm -- --discard-my-data connect r0
在Node1主节点上手动连接资源
[[email protected] ~]# drbdadm connect r0
查看drbd服务启动没,,如果没启动启动后
[[email protected] ~]# cat /proc/drbd version: 8.3.16 (api:88/proto:86-97) GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by [email protected], 2014-11-24 14:51:37 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:304 nr:0 dw:548 dr:12866 al:12 bm:15 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
Keepalived配置实现自动切换:
安装:
wget ./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内容如下:
! 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.77.189 } track_script { check_mysql } }
然后创建check_mysql.sh检测脚本,内容如下:
#!/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.77.189 } }
创建master.sh检测脚本,内容如下:
#!/bin/bash drbdadm primary r0 /bin/mount /dev/drbd0 /app/ /etc/init.d/mysqld start
创建backup.sh检测脚本,内容如下:
#!/bin/bash /etc/init.d/mysqld stop /bin/umount /dev/drbd0 drbdadm secondary r0
在主上查看vip:
[[email protected] sh]# ip add list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:3c:a1:5a brd ff:ff:ff:ff:ff:ff inet 192.168.77.133/24 brd 192.168.77.255 scope global eth0 inet 192.168.77.189/32 scope global eth0 inet6 fe80::20c:29ff:fe3c:a15a/64 scope link valid_lft forever preferred_lft forever
模拟主down机:
[[email protected] sh]# poweroff
然后在从上查看,,vip自动切换到从上,,切mysql启动磁盘自动挂载等
discuz安装:
省略,前面已安装多次
主上mysql授权vip权限,,注册数据库用vip注册,,然后用vip访问测试,可以访问
copy一份论坛网站到从上的html目录下面,模拟主down机,,vip自动漂移到从上,然后用vip访问问得出结果,网站正常访问。。。