Vip | Real_ip | Drbd_mount | Hardware | Os |
192.168.88.9 | 192.168.88.7 | /drbd 100G | Mem:8G Cpu:8C Disk 400G 7200 | Centos6.3-32bit |
192.168.88.8 | /drbd 100G | Mem:8G Cpu:8C Disk::400G 7200 | Centos6.3-32bit |
1. Ucarp部署
官方文档:http://download.pureftpd.org/pub/ucarp/README
ucarp下载地址:http://download.pureftpd.org/pub/ucarp/
libpcap下载地址:http://www.tcpdump.org/#documentation(libpcap为依赖包,可通过yum下载安装)
# yum -y install libpcap libpcap-devel
# tar -xvf ucarp-1.5.2.tar.gz
# ./configure && make && make install
192.168.88.7
mkdir –p /etc/ucarp/
# vi ucarp-up.sh
#! /bin/bash
/sbin/ip addr add 192.168.88.9/24 dev eth0
#vi ucarp-down.sh
#! /bin/bash
/sbin/ip addr del 192.168.88.9/24 dev eth0
192.168.88.8(同上配置)
192.168.88.7
/usr/local/sbin/ucarp -i eth0 -v 42 -p hellowin -a 192.168.88.9 -u /etc/ucarp/ucarp-up.sh -d /etc/ucarp/ucarp-down.sh -s 192.168.88.7 -P –B
192.168.88.8
/usr/local/sbin/ucarp -i eth0 -v 42 -p hellowin -a 192.168.88.9 -u /etc/ucarp/ucarp-up.sh -d /etc/ucarp/ucarp-down.sh -s 192.168.88.8 -P -k 30 –B
192.168.88.7
# vi /etc/ucarp/ucarpstart.sh
/usr/local/sbin/ucarp –I eth0 –v 42 –p hellowin –a 192.168.88.9 –u /etc/ucarp/ucarp-up.sh –d /etc/ucarp/ucarp-down.sh –s 192.168.88.7 –P –B |
# vi /etc/ucarp/ucarpstop.sh
#! /bin/bash echo "[INFO] Stopping ucarp ..." pid=$(ps aux | grep "usr/local/sbin/ucarp" | grep -v grep | awk '{print $2}') if [ "$pid" = "" ];then echo "[INFO] ucarp already stopped !" else echo "[INFO] Kill ucarp, pid="$pid kill -9 $pid /etc/ucarp/ucarp-down.sh fi
添加ucarp(脚本如下)至/etc/init.d
#!/bin/bash # ucarp ucarp backend servers # # chkconfig: 345 71 29 # description: ucarp backend servers # Source function library. . /etc/rc.d/init.d/functions SERVERHOME=/etc/ucarp start() { if [ ! -f $SERVERHOME/ucarpstart.sh ]; then echo "[ERROR] Can't find ucarpstart.sh" exit fi if [ -f /var/lock/subsys/ucarp ]; then echo "[ERROR] ucarp already started ..." exit fi echo "[INFO] Starting ucarp ..." cd $SERVERHOME ./ucarpstart.sh ulimit -c unlimited ulimit -n 10240 touch /var/lock/subsys/ucarp } stop() { if [ ! -f $SERVERHOME/ucarpstop.sh ]; then echo "[ERROR] Can't find ucarpstop.sh" exit fi echo "[INFO] Stopping ucarp ..." cd $SERVERHOME ./ucarpstop.sh rm -f /var/lock/subsys/ucarp } # See how we were called case "$1" in start) start ;; stop) stop ;; restart|reload) stop start ;; status) ;; *) echo $"Usage: $0 {start|stop|restart|reload}" esac exit 0
chkconfig --add ucarp
chmod +x ucarp
service ucarp start
192.168.88.8
同上配置,只需修改对应ip(将/etc/ucarp/ucarpstart.sh 脚本内192.168.88.7ip更换为192.168.88.8即可)
2. Drbd部署
软件下载列表地址:http://oss.linbit.com/drbd/
官方文档介绍:http://www.drbd.org/users-guide-8.4/
#yum -y install kernel-devel kernel-headers flex
wget http://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz
#tar zxf drbd-8.4.3.tar.gz
#cd drbd-8.4.3
#./configure --prefix=/usr/local/drbd --with-km
make KDIR=/usr/src/kernels/2.6.32-358.el6.i686
注意KDIR的路径 (这个内核源码路径需要根据自己的系统修改)
# make install
# mkdir -p /usr/local/drbd/var/run/drbd
# cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d/
# chkconfig --add drbd
# chkconfig drbd on
安装drbd模块(回到解压目录)
# cd drbd
# make clean
# make KDIR=/usr/src/kernels/2.6.32-358.el6.i686
# cp drbd.ko /lib/modules/`uname -r`/kernel/lib/
[root@node1 drbd]# modprobe drbd
若出现 FATAL: Module drbd not found
yum -y install kernel*
reboot
[root@node1 drbd]# lsmod | grep drbd
drbd 292307 0
libcrc32c 841 1 drb
(
查看模块是否加载成功)
查看drbd的主配置文件
/usr/local/drbd/etc
# cat drbd.conf
# You can find an example in /usr/share/doc/drbd.../drbd.conf.example
include "drbd.d/global_common.conf";
include "drbd.d/*.res";
由此可见:主配置文件里面已经包含了全局配置文件和drbd目录下以.res结尾的文件
修改全局配置文件:
# pwd
/usr/local/drbd/etc/drbd.d
#vi global_common.conf
|
# vi drbd.res
|
192.168.88.7/192.168.88.8
初始化资源
#dd if=/dev/zero bs=1M count=1 of=/dev/sda4
# drbdadm create-md r1
启动服务
# service drbd start
192.168.88.7
drbdadm --overwrite-data-of-peer primary all
注意:第一次启动drbd时,两个drbd节点默认都处于Secondary状态,
# drbdadm role r1
Secondary/Secondary
由于默认没有主次节点之分,因而需要设置两个主机的主次节点,选择需要设置为主节点的主机,然后执行如下命令:# drbdadm -- --overwrite-data-of-peer primary all第一次执行完此命令后,在后面如果需要设置哪个是主节点时,就可以使用另外一个命令:drbdadm primary r1 (这个r1是定义资源的角色)或者drbdadm primary all
查看资源连接状态:
# drbdadm cstate r1
Connected
各种状态信息汇总:
StandAlone 独立的:网络配置不可用;资源还没有被连接或是被管理断开(使用 drbdadm disconnect 命令),或是由于出现认证失败或是脑裂的情况
Disconnecting 断开:断开只是临时状态,下一个状态是StandAlone独立的
Unconnected 悬空:是尝试连接前的临时状态,可能下一个状态为WFconnection和WFReportParams
Timeout 超时:与对等节点连接超时,也是临时状态,下一个状态为Unconected悬空
BrokerPipe:与对等节点连接丢失,也是临时状态,下一个状态为Unconected悬空
NetworkFailure:与对等节点推动连接后的临时状态,下一个状态为Unconected悬空
ProtocolError:与对等节点推动连接后的临时状态,下一个状态为Unconected悬空
TearDown 拆解:临时状态,对等节点关闭,下一个状态为Unconected悬空
WFConnection:等待和对等节点建立网络连接
WFReportParams:已经建立TCP连接,本节点等待从对等节点传来的第一个网络包
Connected 连接:DRBD已经建立连接,数据镜像现在可用,节点处于正常状态
StartingSyncS:完全同步,有管理员发起的刚刚开始同步,未来可能的状态为SyncSource或PausedSyncS
StartingSyncT:完全同步,有管理员发起的刚刚开始同步,下一状态为WFSyncUUID
WFBitMapS:部分同步刚刚开始,下一步可能的状态为SyncSource或PausedSyncS
WFBitMapT:部分同步刚刚开始,下一步可能的状态为WFSyncUUID
WFSyncUUID:同步即将开始,下一步可能的状态为SyncTarget或PausedSyncT
SyncSource:以本节点为同步源的同步正在进行
SyncTarget:以本节点为同步目标的同步正在进行
PausedSyncS:以本地节点是一个持续同步的源,但是目前同步已经暂停,可能是因为另外一个同步正在进行或是使用命令(drbdadm pause-sync)暂停了同步
PausedSyncT:以本地节点为持续同步的目标,但是目前同步已经暂停,这可以是因为另外一个同步正在进行或是使用命令(drbdadm pause-sync)暂停了同步
VerifyS:以本地节点为验证源的线上设备验证正在执行
VerifyT:以本地节点为验证目标的线上设备验证正在执行
查看资源角色的命令
[root@node1 ~]# drbdadm role r1
Primary/Secondary (在前面为当前节点)
Parimary 主:资源目前为主,并且可能正在被读取或写入,如果不是双主只会出现在两个节点中的其中一个节点上
Secondary 次:资源目前为次,正常接收对等节点的更新
Unknown 未知:资源角色目前未知,本地的资源不会出现这种状态
查看硬盘状态
[root@node1 ~]# drbdadm dstate r1
UpToDate/UpToDate
本地和对等节点的硬盘有可能为下列状态之一:
Diskless 无盘:本地没有块设备分配给DRBD使用,这表示没有可用的设备,或者使用drbdadm命令手工分离或是底层的I/O错误导致自动分离
Attaching:读取无数据时候的瞬间状态
Failed 失败:本地块设备报告I/O错误的下一个状态,其下一个状态为Diskless无盘
Negotiating:在已经连接的DRBD设置进行Attach读取无数据前的瞬间状态
Inconsistent:数据是不一致的,在两个节点上(初始的完全同步前)这种状态出现后立即创建一个新的资源。此外,在同步期间(同步目标)在一个节点上出现这种状态
Outdated:数据资源是一致的,但是已经过时
DUnknown:当对等节点网络连接不可用时出现这种状态
Consistent:一个没有连接的节点数据一致,当建立连接时,它决定数据是UpToDate或是Outdated
UpToDate:一致的最新的数据状态,这个状态为正常状态
查看同步进度
# cat /proc/drbd
version: 8.4.3 (api:1/proto:86-101)
GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@localhost.localdomain, 2016-04-24 20:16:24
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:2767088 nr:0 dw:0 dr:2774680 al:0 bm:168 lo:0 pe:1 ua:7 ap:0 ep:1 wo:f oos:18202972
[=>..................] sync'ed: 13.3% (17776/20476)M
finish: 0:12:59 speed: 23,344 (22,492) K/sec
由此可见:进度已经完成了13.3%,传输速度大概22M/S
注:
ds是磁盘状态信息
dw是磁盘写信息
dr是磁盘读信息
挂载drbd目录
192.168.88.7
格式化文件系统(文件格式根据自己的系统环境选择)
# mkfs.ext4 /dev/drbd0
挂载此文件系统
# mkdir /drbd
# mount /dev/drbd0 /drbd
在挂载data目录中创建一个测试文件,然后卸载挂载目录,然后切换主备节点,在备用节点上查看刚刚建立的测试文件还是否存在
192.168.88.7
# mkdir -p /drbd/mysql
# umount /drbd/
将node1变为备用节点
# drbdadm secondary r1
# drbdadm role r1
Secondary/Secondary
192.168.88.8:
将变为主节点
[root@node2 ~]# drbdadm primary r1
[root@node2 ~]# drbdadm role r1
Primary/Secondary
挂载设备,然后看文件是否存在
# mkdir /drbd
# mount /dev/drbd0 /drbd
#cd /drbd
验证mysql文件是否存在
# ll /drbd/mysql/
3.
Mysql
部署
#
yum -y install mysql mysql-server mysql-devel
(此处采用yum安装)
# vi /etc/my.cnf
#
修改mysql数据文件存储路径
|
# vi /etc/init.d/mysqld
|
# chown -R mysql:mysql /drbd/mysql/
# /etc/init.d/mysqld
192.168.88.7
创建数据库
mysql> create database herb
停止mysql
# servic mysqld stop
卸载drbd目录
#umount /drbd
降级为从状态
drbdadm secondary all
192.168.88.8
升级为主状态
drbdadm primary all
挂载drbd目录
mount /dev/drbd0 /drbd
启动mysql
service mysqld start
查看数据库切换
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| herb |
验证成功!
如上则可通过升级和降级脚本的方式实现自动切换:
4.1.1
升级主状态脚本
#!/bin/sh DATE=`date +'%Y-%m-%d %H:%M:%S'` echo -e "[$DATE]" |tee -a > /tmp/Drbd_m.txt check_status() { if [ $? -eq 0 ] then echo -e " $1 $2 ... [ OK ] " |tee -a /tmp/Drbd_m.txt else echo -e " $1 $2 ... [FAILED]" |tee -a /tmp/Drbd_m.txt fi } #start mysql Start_mysql() { service mysqld start check_status start mysql } #umount /drbd Mount_drbd() { Mount=`df -h |grep drbd | grep -v grep|wc -l` if [ $Mount -ne 0 ] then echo -e "The /Drbd has mounted ..." else mount /dev/drbd0 /drbd check_status Mount /Drbd fi } Upgrade_primary() { level=`drbdadm role r1|awk -F / '{print $1}'` if [ "$level" == Primary ] then echo -e "The level has Primary ..." else drbdadm primary all check_status Update primary fi } ##&& Start_mysql && Upgrade_primary && Mount_drbd && Start_mysql
4.1.2降级从状态脚本
#!/bin/sh DATE=`date +'%Y-%m-%d %H:%M:%S'` echo -e "[$DATE]" |tee -a > /tmp/Drbd_s.txt check_status() { if [ $? -eq 0 ] then echo -e " $1 $2 ... [ OK ] " |tee -a /tmp/Drbd_s.txt else echo -e " $1 $2 ... [FAILED]" |tee -a /tmp/Drbd_s.txt fi } #shudown mysql Shutdown_mysql() { service mysqld stop check_status shutdown mysql } #umount /drbd Umount_drbd() { drbdpid=`lsof -w /drbd | grep -v COMMAND|wc -l` if [ $drbdpid -gt 0 ] then lsof -w /drbd | grep -v COMMAND | awk '{print $2}' |xargs kill -9 check_status Killall Drbd umount /drbd check_status Umount /Drbd else Mount=`df -h |grep drbd | grep -v grep|wc -l` if [ $Mount -eq 0 ] then echo -e "The /Drbd has umounted ..." else umount /drbd check_status Umount /Drbd fi fi } Demotion_secondary() { level=`drbdadm role r1|awk -F / '{print $1}'` if [ "$level" == Secondary ] then echo -e "The level has secondary ..." else drbdadm secondary all check_status Downgrade secondary fi } Shutdown_mysql && Umount_drbd && Demotion_secondary
4.2 结合ucarp设置自动切换
在ucarp 切换脚本中添加drbd主从切换脚本
# cat ucarp-up.sh
#! /bin/bash /sbin/ip addr add 192.168.88.9/24 dev eth0 sh /etc/ucarp/drbd-m.sh
|
# cat ucarp-down.sh
#! /bin/bash /sbin/ip addr del 192.168.88.9/24 dev eth0 sh /etc/ucarp/drbd-s.sh
|
拔掉网线 、重启服务器、设置IP故障等方式测试 服务器切换情况
(已验证,此处不做详述!)\
1. Ucarp问题及解决
./configure 失败
解决办法
yum -y install libpcap libpcap-devel
2. Drbd问题及解决
问题一:
configure: error: Cannot build utils without flex, either install flex or pass the --without-utils option.
解决办法:
yum -y intalls flex
问题二:
make: [check-kdir] error 1
解决办法:yum install -y kernel-devel
问题三:
SORRY, kernel makefile not found.
You need to tell me a correct KDIR,
Or install the neccessary kernel source packages.
'drbd' not defined in your config (for this host).
这个因为没有加载到drbd模块
问题四:
启动报错:
Command 'drbdmeta 0 v08 /dev/sdb1 internal create-md' terminated with exit code 40
这是因为sdb1已经有文件系统了,已经有数据存在了
解决方法:
[root@node1 ~]# dd if=/dev/zero bs=1M count=1 of=/dev/sdb1
问题五:
# modprobe drbd
FATAL: Module drbd not found.
解决方法:
升级内核并重启
yum -y install kernel*
reboot
问题六:
[root@995120_master /]# mount /dev/drbd0 /drbd
mount: you must specify the filesystem type
解决方法:
mkfs.ext4 /dev/drbd0
问题七:
[root@995120_master /]# umount /drbd
umount: /drbd: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
解决方法: 卸载目录文件被打开,杀掉打开文件的进程即可
lsof -w /drbd | grep -v COMMAND | awk '{print $2}' |xargs kill -9
3.
Mysql
问题及解决
/etc/init.d/mysqld start
Initializing MySQL database: WARNING: The host '995120-master' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
140226 16:25:32 [Warning] Can't create test file /drbd/mysql/995120-master.lower-test
140226 16:25:32 [Warning] Can't create test file /drbd/mysql/995120-master.lower-test
/usr/libexec/mysqld: Can't change dir to '/drbd/mysql/' (Errcode: 13)
140226 16:25:32 [ERROR] Aborting
140226 16:25:32 [Note] /usr/libexec/mysqld: Shutdown complete
Installation of system tables failed! Examine the logs in
/drbd/mysql for more information.
You can try to start the mysqld daemon with:
shell> /usr/libexec/mysqld --skip-grant &
and use the command line tool /usr/bin/mysql
to connect to the mysql database and look at the grant tables:
shell> /usr/bin/mysql -u root mysql
mysql> show tables
Try 'mysqld --help' if you have problems with paths. Using --log
gives you a log in /drbd/mysql that may be helpful.
Please consult the MySQL manual section
'Problems running mysql_install_db', and the manual section that
describes problems on your OS. Another information source are the
MySQL email archives available at http://lists.mysql.com/.
Please check all of the above before mailing us! And remember, if
you do mail us, you MUST use the /usr/bin/mysqlbug script!
[FAILED]
确认数据目录权限
chown –R mysql:mysql /drbd/mysql/
设置selinux允许权限或者关闭selinux
方法一:
设置允许:# chcon -R -t mysqld_db_t /mysql
方法二:
关闭selinux:# setenforce 0 (临时关闭)
vi /etc/sysconfig/selinux
SELINUX=disabled
(下次重启则永久关闭selinux安装策略设置)
问题
[root@localhost drbd.d]# drbdadm create-md r1
'r1' not defined in your config (for this host).
解决办法:
确定配置文件主机名和
uname –n保持一致
修改主机名
#hostname 995120-master
#vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=995120-master
相关问题后续补充!
转载请注明:梦绘设计 » 高可用/自动化 » MySQL高可用-UCARP+DRBD+MYSQL架构部署
版权声明
本站《作品展示》类文章均为原创,转载必须注明出处,技术分享类文章部分来源于网络,版权归原作者所有,若侵权请留言。