實(shí)驗(yàn)參考 http://blog.csdn.net/qq_19175749/article/details/51607210
成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)提供從項(xiàng)目策劃、軟件開(kāi)發(fā),軟件安全維護(hù)、網(wǎng)站優(yōu)化(SEO)、網(wǎng)站分析、效果評(píng)估等整套的建站服務(wù),主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè),成都APP應(yīng)用開(kāi)發(fā)以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。成都創(chuàng)新互聯(lián)深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
官方站點(diǎn):http://linux-ha.org/wiki/Main_Page
heartbeat可以資源(VIP地址及程序服務(wù))從一臺(tái)有故障的服務(wù)器快速的轉(zhuǎn)移到另一臺(tái)正常的服務(wù)器提供服務(wù),heartbeat和keepalived相似,heartbeat可以實(shí)現(xiàn)failover功能,但不能實(shí)現(xiàn)對(duì)后端的健康檢查
heartbeat和keepalived應(yīng)用場(chǎng)景及區(qū)別
很多網(wǎng)友說(shuō)為什么不使用keepalived而使用長(zhǎng)期不更新的heartbeat,下面說(shuō)一下它們之間的應(yīng)用場(chǎng)景及區(qū)別
1、對(duì)于web,db,負(fù)載均衡(lvs,haproxy,nginx)等,heartbeat和keepalived都可以實(shí)現(xiàn)
2、lvs最好和keepalived結(jié)合,因?yàn)閗eepalived最初就是為lvs產(chǎn)生的,(heartbeat沒(méi)有對(duì)RS的健康檢查功能,heartbeat可以通過(guò)ldircetord來(lái)進(jìn)行健康檢查的功能)
3、MySQL雙主多從,NFS/MFS存儲(chǔ),他們的特點(diǎn)是需要數(shù)據(jù)同步,這樣的業(yè)務(wù)最好使用heartbeat,因?yàn)閔eartbeat有自帶的drbd腳本
總結(jié):無(wú)數(shù)據(jù)同步的應(yīng)用程序高可用可選擇keepalived,有數(shù)據(jù)同步的應(yīng)用程序高可用可選擇heartbeat
官方站點(diǎn): http://www.drbd.org/users-guide-8.4/
DRBD(DistributedReplicatedBlockDevice)是一個(gè)基于塊設(shè)備級(jí)別在遠(yuǎn)程服務(wù)器直接同步和鏡像數(shù)據(jù)的軟件,用軟件實(shí)現(xiàn)的、無(wú)共享的、服務(wù)器之間鏡像塊設(shè)備內(nèi)容的存儲(chǔ)復(fù)制解決方案。它可以實(shí)現(xiàn)在網(wǎng)絡(luò)中兩臺(tái)服務(wù)器之間基于塊設(shè)備級(jí)別的實(shí)時(shí)鏡像或同步復(fù)制(兩臺(tái)服務(wù)器都寫(xiě)入成功)/異步復(fù)制(本地服務(wù)器寫(xiě)入成功),相當(dāng)于網(wǎng)絡(luò)的RAID1,由于是基于塊設(shè)備(磁盤(pán),LVM邏輯卷),在文件系統(tǒng)的底層,所以數(shù)據(jù)復(fù)制要比cp命令更快。DRBD已經(jīng)被MySQL官方寫(xiě)入文檔手冊(cè)作為推薦的高可用的方案之一
官方站點(diǎn): http://www.mysql.com/
MySQL是一個(gè)開(kāi)放源碼的小型關(guān)聯(lián)式數(shù)據(jù)庫(kù)管理系統(tǒng)。目前MySQL被廣泛地應(yīng)用在Internet上的中小型網(wǎng)站中。由于其體積小、速度快、總體擁有成本低,尤其是開(kāi)放源碼這一特點(diǎn),許多中小型網(wǎng)站為了降低網(wǎng)站總體擁有成本而選擇了MySQL作為網(wǎng)站數(shù)據(jù)庫(kù)。
架構(gòu)說(shuō)明:
一主多從最常用的架構(gòu),多個(gè)從庫(kù)可以使用lvs來(lái)提供讀的負(fù)載均衡
解決一主單點(diǎn)的問(wèn)題,當(dāng)主庫(kù)宕機(jī)后,可以實(shí)現(xiàn)主庫(kù)宕機(jī)后備節(jié)點(diǎn)自動(dòng)接管,所有的從庫(kù)會(huì)自動(dòng)和新的主庫(kù)進(jìn)行同步,實(shí)現(xiàn)了mysql主庫(kù)的熱備方案
系統(tǒng)環(huán)境 |
|
系統(tǒng) | Red Hat Enterprise Linux Server release 6.5 |
系統(tǒng)位數(shù) | x86_64 |
內(nèi)核版本 | 2.6.32-431.el6.x86_64 |
軟件環(huán)境 |
|
heartbeat | heartbeat-3.0.4 |
drbd | drbd-8.4.4 |
mysql | mysql-5.5.32 |
角色 | IP |
VIP | eth0: 192.168.12.1/24 (提供對(duì)外服務(wù)地址) |
master1 | eth0: 192.168.12.55/24(內(nèi)網(wǎng)) eth2: 10.1.12.55/24(心跳線(xiàn)) eth3: 10.2.12.55/24(DRBD千兆數(shù)據(jù)傳輸) |
master2 | eth0: 192.168.12.56/24(內(nèi)網(wǎng)) eth2: 10.1.12.56/24(心跳線(xiàn)) eth3: 10.2.12.56/24(DRBD千兆數(shù)據(jù)傳輸) |
slave1 | eth0: 192.168.12.55/24(暫時(shí)放在master1上) |
說(shuō)明:從庫(kù)通過(guò)主庫(kù)的VIP進(jìn)行主從同步replication | |
需求: 1、主庫(kù)master1宕機(jī)后master2自動(dòng)接管VIP以及所有從庫(kù) 2、在master2接管時(shí),不影響從庫(kù)的主從同步replication |
磁盤(pán) | 容量 | 分區(qū) | 掛載點(diǎn) | 說(shuō)明 |
/dev/sdb | 18G | /dev/sdb2 | /data/ | 存放數(shù)據(jù) |
2G | /dev/sdb1 | metadata分區(qū) | 存放drbd同步的狀態(tài)信息 | |
注意 1、metadata分區(qū)一定不能格式化建立文件系統(tǒng)(sdb2存放drbd同步的狀態(tài)信息) 2、分好的分區(qū)不要進(jìn)行掛載 3、生產(chǎn)環(huán)境DRBDmetadata分區(qū)一般可設(shè)置為1-2G,數(shù)據(jù)分區(qū)看需求給最大 4、在生產(chǎn)環(huán)境中兩塊硬盤(pán)一樣大 |
##主機(jī)名、ip修改,關(guān)閉防火墻,重啟一次系統(tǒng):
# hostname master1
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=master1
####如下:
[root@master1 ~]# hostname
master1
[root@master1 ~]# ip add|egrep "eth0|eth2|eth3"|grep inet
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
inet 10.1.12.55/24 brd 10.1.12.255 scope global eth2
inet 10.2.12.55/24 brd 10.2.12.255 scope global eth3
[root@master1 ~]# fdisk -l|grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
[root@master1 ~]# chkconfig iptables off
[root@master1 ~]# reboot
[root@master2 ~]# hostname
master2
[root@master2 ~]# ip add|egrep "eth0|eth2|eth3"|grep inet
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
inet 10.1.12.56/24 brd 10.1.12.255 scope global eth2
inet 10.2.12.56/24 brd 10.2.12.255 scope global eth3
[root@master2 ~]# fdisk -l|grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
[root@master2 ~]# chkconfig iptables off
[root@master2 ~]# reboot
======master1
[root@master1 ~]# route add -host 10.1.12.56 dev eth2 《到對(duì)端心跳路由》
[root@master1 ~]# route add -host 10.2.12.56 dev eth3 《到對(duì)端DRBD數(shù)據(jù)路由》
[root@master1 ~]# route -n|grep "10\."|sort
10.1.12.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
10.1.12.56 0.0.0.0 255.255.255.255 UH 0 0 0 eth2
10.2.12.0 0.0.0.0 255.255.255.0 U 0 0 0 eth3
10.2.12.56 0.0.0.0 255.255.255.255 UH 0 0 0 eth3
## hosts配置選擇心跳ip以及drbd-ip
[root@master1 ~]# echo "10.1.12.55 master1
10.2.12.55 master1
10.1.12.56 master2
10.2.12.56 master2" >> /etc/hosts
[root@master1 ~]# tail -4 /etc/hosts
10.1.12.55 master1
10.2.12.55 master1
10.1.12.56 master2
10.2.12.56 master2
======master2
[root@master2 ~]# route add -host 10.1.12.55 dev eth2
[root@master2 ~]# route add -host 10.2.12.55 dev eth3
[root@master2 ~]# route -n|grep "10\."|sort
10.1.12.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
10.1.12.55 0.0.0.0 255.255.255.255 UH 0 0 0 eth2
10.2.12.0 0.0.0.0 255.255.255.0 U 0 0 0 eth3
10.2.12.55 0.0.0.0 255.255.255.255 UH 0 0 0 eth3
## hosts配置選擇心跳ip
[root@master2 ~]# echo "10.1.12.55 master1
10.2.12.55 master1
10.1.12.56 master2
10.2.12.56 master2" >> /etc/hosts
[root@master2 ~]# tail -2 /etc/hosts
10.1.12.55 master1
10.2.12.55 master1
10.1.12.56 master2
10.2.12.56 master2
##如果開(kāi)啟防火墻,需要放行udp 694端口 主備一致
[root@master2 heartbeat-3.0.4-RPM]# cat setup.sh
#!/bin/bash
cd `dirname $0`
echo `pwd`
echo install lib64ltdl7
rpm -ivf lib64ltdl7-2.2.6-6.1mdv2009.1.x86_64.rpm
echo install perl-TimeDate
rpm -ivf perl-TimeDate-1.16-13.el6.noarch.rpm
echo install PyXML
rpm -ivf PyXML-0.8.4-19.el6.x86_64.rpm
echo install cluster-glue-libs
rpm -ivf cluster-glue-libs-1.0.5-6.el6.x86_64.rpm
echo install cluster-glue
rpm -ivf cluster-glue-1.0.5-6.el6.x86_64.rpm
echo install resource-agents
rpm -ivf resource-agents-3.9.5-24.el6_7.1.x86_64.rpm
echo install heartbeat
rpm -ivf heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-libs-3.0.4-2.el6.x86_64.rpm
echo Done
exit 0
[root@master2 heartbeat-3.0.4-RPM]# sh setup.sh
[root@master2 heartbeat-3.0.4-RPM]# rpm -qa|grep heartbeat
heartbeat-libs-3.0.4-2.el6.x86_64
heartbeat-3.0.4-2.el6.x86_64
##主備兩端配置文件ha.cf/authkeys/sharesources完全一致
[root@master1 ~]# vim /etc/ha.d/ha.cf
#log configure
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local1
#options configure
keepalive 2
deadtime 30
warntime 10
initdead 120
#bcast eth2
mcast eth2 225.0.0.55 694 1 0
#node configure
auto_failback on
node master1
node master2
crm no
[root@master1 ~]# vi /etc/ha.d/authkeys
auth 1
1 sha1 47e9336850f1db6fa58bc470bc9b7810eb397f06
[root@master1 ~]# chmod 600 /etc/ha.d/authkeys
master1 IPaddr::192.168.12.1/24/eth0
#master1 IPaddr::192.168.12.1/24/eth0 drbddisk::data Filesystem::/dev/drbd1::/data::ext4 mysqld
說(shuō)明:
drbddisk::data <==啟動(dòng)drbd data資源,相當(dāng)于執(zhí)行/etc/ha.d/resource.d/drbddisk data stop/start操作
Filesystem::/dev/drbd1::/data::ext4 <==drbd分區(qū)掛載到/data目錄,相當(dāng)于執(zhí)行/etc/ha.d/resource.d/Filesystem /dev/drbd1 /data ext4 stop/start <==相當(dāng)于系統(tǒng)中執(zhí)行mount /dev/drbd1 /data
mysql <==啟動(dòng)mysql服務(wù)腳本,相當(dāng)于/etc/init.d/mysql stop/start
[root@master1 ~]# /etc/init.d/heartbeat start
##注意看日志
[root@master1 ~]# tail -200f /var/log/ha-log
[root@master1 ~]# tail -200f /var/log/ha-debug
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master1 ~]# chkconfig --list heartbeat
heartbeat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@master2 ~]# /etc/init.d/heartbeat start
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
##模擬故障
[root@master1 ~]# /etc/init.d/heartbeat stop
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
說(shuō)明:master1宕機(jī)后,vip地址漂移到master2節(jié)點(diǎn)上,master2成為主節(jié)點(diǎn)
## master1搶占vip
[root@master1 ~]# /etc/init.d/heartbeat start
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
說(shuō)明:master1啟動(dòng)后,vip地址漂移回master1節(jié)點(diǎn)上,master1成為主節(jié)點(diǎn)。原因是/etc/ha.d/ha.cf 參數(shù)中auto_failback on 這里建議改為off
##這里已經(jīng)分好區(qū),如下:(2G給drbd存狀態(tài)信息,18G用于存data數(shù)據(jù))
[root@master1 ~]# fdisk -l|grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb2 263 2610 18860310 83 Linux
[root@master2 ~]# fdisk -l|grep sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb2 263 2610 18860310 83 Linux
##格式化sdb2分區(qū),sdb1分區(qū)為meta data分區(qū),不要格式化
[root@master1 ~]# mkfs.ext4 /dev/sdb2
[root@master2 ~]# mkfs.ext4 /dev/sdb2
##主備節(jié)點(diǎn)兩端配置文件完全一致
# yum -y install kernel-devel kernel-headers flex
# tar zxvf drbd-8.4.4.tar.gz
# cd drbd-8.4.4
# ./configure --prefix=/usr/local/drbd --with-km
# make KDIR=/usr/src/kernels/2.6.32-431.el6.x86_64
# 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 --list drbd
drbd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
回到剛剛解壓drbd的目錄,然后
# cd drbd
# make clean
rm -rf .tmp_versions Module.markers Module.symvers modules.order
rm -f *.[oas] *.ko .*.cmd .*.d .*.tmp *.mod.c .*.flags .depend .kernel*
rm -f compat/*.[oas] compat/.*.cmd
# make KDIR=/usr/src/kernels/2.6.32-431.el6.x86_64
# cp drbd.ko /lib/modules/2.6.32-431.el6.x86_64/kernel/lib/
# depmod
# modprobe drbd
#查看模塊是否加載成功
# lsmod |grep drbd
drbd 340519 0
libcrc32c 1246 1 drbd
##配置開(kāi)機(jī)自動(dòng)加載
# echo "modprobe drbd >/dev/null 2>&1" > /etc/sysconfig/modules/drbd.modules
[root@master1 ~]# more /etc/sysconfig/modules/drbd.modules
modprobe drbd >/dev/null 2>&1
##主備節(jié)點(diǎn)兩端配置文件完全一致
[root@master1 ~]# vi /usr/local/drbd/etc/drbd.conf
global {
# minor-count 64;
# dialog-refresh 5; # 5 seconds
# disable-ip-verification;
usage-count no;
}
common {
protocol C;
disk {
on-io-error detach;
#size 454G;
no-disk-flushes;
no-md-flushes;
}
net {
sndbuf-size 512k;
# timeout 60; # 6 seconds (unit = 0.1 seconds)
# connect-int 10; # 10 seconds (unit = 1 second)
# ping-int 10; # 10 seconds (unit = 1 second)
# ping-timeout 5; # 500 ms (unit = 0.1 seconds)
max-buffers 8000;
unplug-watermark 1024;
max-epoch-size 8000;
# ko-count 4;
# allow-two-primaries;
cram-hmac-alg "sha1";
shared-secret "hdhwXes23sYEhart8t";
after-sb-0pri disconnect;
after-sb-1pri disconnect;
after-sb-2pri disconnect;
rr-conflict disconnect;
# data-integrity-alg "md5";
# no-tcp-cork;
}
syncer {
rate 120M;
al-extents 517;
}
}
resource data {
on master1 {
device /dev/drbd1;
disk /dev/sdb2;
address 10.2.12.55:7788;
meta-disk /dev/sdb1 [0];
}
on master2 {
device /dev/drbd1;
disk /dev/sdb2;
address 10.2.12.56:7788;
meta-disk /dev/sdb1 [0];
}
}
##主備節(jié)點(diǎn)兩端配置文件完全一致
[root@master1 ~]# drbdadm create-md data
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
##主備都需要
[root@master1 ~]# drbdadm up all
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:WFConnection ro:Secondary/Unknown ds:Inconsistent/DUnknown C r----s
ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:18860312
##主節(jié)點(diǎn)操作
[root@master1 ~]# drbdadm -- --overwrite-data-of-peer primary data
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:10354688 nr:0 dw:0 dr:10358428 al:0 bm:632 lo:1 pe:0 ua:4 ap:0 ep:1 wo:d oos:8505624
[==========>.........] sync'ed: 55.0% (8304/18416)M
finish: 0:01:25 speed: 99,360 (96,772) K/sec
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r-----
ns:0 nr:15407104 dw:15406080 dr:0 al:0 bm:940 lo:2 pe:7 ua:1 ap:0 ep:1 wo:d oos:3454232
[===============>....] sync'ed: 81.7% (3372/18416)M
finish: 0:00:35 speed: 98,360 (96,288) want: 102,400 K/sec
[root@master1 ~]# chkconfig --list drbd
drbd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@master1 ~]# drbdadm primary all
[root@master1 ~]# mkdir /data
[root@master1 ~]# mount /dev/drbd1 /data/
[root@master1 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 172M 17G 1% /data
##正常狀態(tài)
[root@master1 ~]# cp drbd-8.4.4.tar.gz /data/
[root@master1 ~]# echo "test drbd switch .....">/data/test.txt
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:18861122 nr:0 dw:812 dr:18861341 al:4 bm:1152 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:0 nr:18861146 dw:18861146 dr:0 al:0 bm:1152 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
說(shuō)明:master1為主節(jié)點(diǎn),master2為備節(jié)點(diǎn)
##模擬master1宕機(jī)
[root@master1 ~]# umount /dev/drbd1
[root@master1 ~]# drbdadm down all
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:WFConnection ro:Secondary/Unknown ds:UpToDate/DUnknown C r-----
ns:0 nr:18861178 dw:18861178 dr:0 al:0 bm:1152 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# drbdadm primary all
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:WFConnection ro:Primary/Unknown ds:UpToDate/DUnknown C r-----
ns:0 nr:18861178 dw:18861178 dr:668 al:0 bm:1152 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# mkdir /data
[root@master2 ~]# mount /dev/drbd1 /data/
[root@master2 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 173M 17G 2% /data
[root@master2 ~]# ll -lrht /data/
total 716K
drwx------ 2 root root 16K Mar 16 2018 lost+found
-rw-r--r-- 1 root root 693K Mar 16 2018 drbd-8.4.4.tar.gz
-rw-r--r-- 1 root root 23 Mar 16 2018 test.txt
說(shuō)明:master1宕機(jī)后,master2可以升級(jí)為主節(jié)點(diǎn),掛載drbd分區(qū)繼續(xù)使用
####還原drbd到master1
[root@master1 ~]# /etc/init.d/drbd start
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:0 nr:4 dw:4 dr:0 al:0 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:4 nr:18861178 dw:18861182 dr:1027 al:1 bm:1153 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# /etc/init.d/drbd stop
Stopping all DRBD resources:
.
[root@master2 ~]# cat /proc/drbd
cat: /proc/drbd: No such file or directory
[root@master2 ~]# df -hT|grep drbd
[root@master2 ~]# /etc/init.d/drbd start
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate 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:d oos:0
[root@master1 ~]# drbdadm primary all
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:0 nr:8 dw:8 dr:668 al:0 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master1 ~]# mount /dev/drbd1 /data/
[root@master1 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 173M 17G 2% /data
[root@master1 ~]# chkconfig --list drbd
drbd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@master1 ~]# chkconfig drbd off
[root@master1 ~]# vi /etc/ha.d/haresources
master1 IPaddr::192.168.12.1/24/eth0 drbddisk::data Filesystem::/dev/drbd1::/data::ext4
[root@master1 ~]# cp /root/drbd-8.4.4/scripts/drbddisk /etc/ha.d/resource.d/
[root@master1 ~]# /etc/init.d/heartbeat stop
[root@master2 ~]# chkconfig drbd off
[root@master2 ~]# vi /etc/ha.d/haresources
master1 IPaddr::192.168.12.1/24/eth0 drbddisk::data Filesystem::/dev/drbd1::/data::ext4
[root@master2 ~]# cp /root/drbd-8.4.4/scripts/drbddisk /etc/ha.d/resource.d/
[root@master2 ~]# /etc/init.d/heartbeat stop
[root@master1 ~]# /etc/init.d/heartbeat start
[root@master2 ~]# /etc/init.d/heartbeat start
##測(cè)試切換
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:4 nr:0 dw:4 dr:693 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master1 ~]# df -hT|grep /drbd
/dev/drbd1 ext4 18G 173M 17G 2% /data
[root@master1 ~]# /etc/init.d/heartbeat stop
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:8 nr:4 dw:12 dr:1027 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:4 nr:8 dw:12 dr:693 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 173M 17G 2% /data
## master1安裝mysql,master2只安裝到初始化之前,slave從庫(kù)也配置在master1上,這里暫時(shí)不配置slave從庫(kù)
# ls -lrht cmake-2.8.8.tar.gz
-rw-r--r-- 1 root root 5.5M Oct 20 04:50 cmake-2.8.8.tar.gz
# tar xf cmake-2.8.8.tar.gz
# cd cmake-2.8.8
# ./configure
# gmake
# gmake install
# which cmake
/usr/local/bin/cmake
# yum -y install ncurses-devel
# yum -y install bison
----u指定用戶(hù)uid ----s指定用戶(hù)登錄所使用shell ----M不要?jiǎng)?chuàng)建用戶(hù)登錄目錄 ----g指定用戶(hù)所屬組 ----G指定用戶(hù)附加組
# groupadd -g 1200 mysql
# useradd mysql -s /sbin/nologin -M -g mysql -u 1200
---hosts配置(這里不用配置,因?yàn)楹罄m(xù)mysql是通過(guò)vip提供服務(wù))
# tail -4 /etc/hosts
10.1.12.55 master1
10.2.12.55 master1
10.1.12.56 master2
10.2.12.56 master2
# tar zxf mysql-5.5.32.tar.gz
# cd mysql-5.5.32
# cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql-5.5.32 \
-DMYSQL_DATADIR=/app/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/app/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
# make && make install
# ln -s /app/mysql-5.5.32/ /app/mysql
# ll /app/
total 4
lrwxrwxrwx 1 root root 18 Oct 30 05:06 mysql -> /app/mysql-5.5.32/
drwxr-xr-x 13 root root 4096 Oct 30 05:05 mysql-5.5.32
# echo 'export PATH=/app/mysql/bin:$PATH' >> /etc/profile
tail -5 /etc/profile
source /etc/profile
echo $PATH
##放到drbd存儲(chǔ)上
# vi /data/master/my.cnf
[mysqld]
socket = /data/master/mysql.sock
port = 3306
pid-file = /data/master/mysql.pid
datadir = /data/master/data
basedir = /app/mysql
user = mysql
server-id=1
[client]
port = 3306
socket = /data/master/mysql.sock
[mysql]
no-auto-rehash
## 注意:數(shù)據(jù)庫(kù)存放數(shù)據(jù)的目錄一定要在drbd分區(qū)
## master1上操作
[root@master1 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 173M 17G 2% /data
[root@master1 ~]# cd /app/mysql/scripts/
# ./mysql_install_db --basedir=/app/mysql --datadir=/data/master/data --user=mysql
# chown -R mysql. /data/master/
##啟動(dòng)mysql
# mysqld_safe --defaults-file=/data/master/my.cnf &
# netstat -nltpd |grep mysql
##修改mysql登錄密碼
# /app/mysql/bin/mysqladmin -u root password '111111' -S /data/master/mysql.sock
##登錄測(cè)試
# mysql -uroot -p111111 -S /data/master/mysql.sock
##注意master1、master2都需要添加,并且需要添加到ha啟動(dòng)目錄下
# vi /etc/ha.d/resource.d/mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="111111"
cmdpath="/app/mysql/bin/"
mysql_sock="/data/master/mysql.sock"
#startup function
function_start_mysql()
{
if [ ! -e ${mysql_sock} ];then
printf "Starting MySQL...\n"
/bin/sh ${cmdpath}/mysqld_safe --defaults-file=/data/master/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
# echo " ${cmdpath}mysqladmin -u${mysql_user} -p${mysql_pwd} -S /mysqldata/${port}/mysql${port}.sock shutd
own "
${cmdpath}mysqladmin -u${mysql_user} -p${mysql_pwd} -S /data/master/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage:/mysqldata/${port}/mysql {start|stop|restart}\n"
esac
##測(cè)試啟停
# chmod a+x /etc/ha.d/resource.d/mysqld
# /etc/ha.d/resource.d/mysqld start
# netstat -nltpd |grep mysql
##先停master1上的mysql服務(wù)
# /etc/ha.d/resource.d/mysqld stop
##停master1上的高可用,使得drbd磁盤(pán)掛載到master2上
[root@master1 ~]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.
[root@master1 ~]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
## master2上檢查
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:20 nr:41760 dw:41780 dr:2096 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 202M 17G 2% /data
## master2拷貝mysql啟停服務(wù)并修改
# scp /etc/ha.d/resource.d/mysqld master2:/etc/ha.d/resource.d/mysqld
##啟停測(cè)試
[root@master2 ~]# /etc/ha.d/resource.d/mysqld start
Starting MySQL.. [ OK ]
[root@master2 ~]# netstat -nltpd|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 41571/mysqld
[root@master2 ~]# /etc/ha.d/resource.d/mysqld stop
Shutting down MySQL. [ OK ]
=======master1、master2都需要操作
##先停高可用
# /etc/init.d/heartbeat stop
##將mysqld服務(wù)移動(dòng)到ha服務(wù)中
# chkconfig --list mysqld
service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
# ll /etc/ha.d/resource.d/|grep mysql
-rwxr-xr-x 1 root root 1152 Oct 30 11:35 mysqld
##修改啟動(dòng)參數(shù)
# vi /etc/ha.d/haresources
master1 IPaddr::192.168.12.1/24/eth0 drbddisk::data Filesystem::/dev/drbd1::/data::ext4 mysqld
##啟動(dòng)高可用
# /etc/init.d/heartbeat start
# tail -200f /var/log/ha-log
# tail -200f /var/log/ha-debug
##檢查
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:41952 nr:884 dw:42836 dr:9467 al:16 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master1 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 202M 17G 2% /data
[root@master1 ~]# netstat -nltpd|grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 54535/mysqld
##測(cè)試切換
[root@master1 ~]# /etc/init.d/heartbeat stop
[root@master1 ~]# ip add|grep 192
inet 192.168.12.55/24 brd 192.168.12.255 scope global eth0
[root@master1 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master1, 2018-03-16 11:44:26
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:42100 nr:996 dw:43096 dr:9801 al:16 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master1 ~]# df -hT|grep drbd
[root@master1 ~]# netstat -nltpd|grep mysql
[root@master2 ~]# ip add|grep 192
inet 192.168.12.56/24 brd 192.168.12.255 scope global eth0
inet 192.168.12.1/24 brd 192.168.12.255 scope global secondary eth0
[root@master2 ~]# cat /proc/drbd
version: 8.4.4 (api:1/proto:86-101)
GIT-hash: 74402fecf24da8e5438171ee8c19e28627e1c98a build by root@master2, 2016-10-29 23:25:51
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:1044 nr:42100 dw:43144 dr:13859 al:6 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:d oos:0
[root@master2 ~]# df -hT|grep drbd
/dev/drbd1 ext4 18G 202M 17G 2% /data
[root@master2 ~]# netstat -nltpd|grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 43020/mysqld
##現(xiàn)在mysql的HA已經(jīng)配置好,當(dāng)前節(jié)點(diǎn)在master1上
##簡(jiǎn)單配置優(yōu)化
[root@master1 ~]# mysql -uroot -p111111 -S /data/master/mysql.sock
mysql> drop database test;
mysql> select user,host from mysql.user;
mysql> delete from mysql.user where user='';
mysql> delete from mysql.user where host='master1';
mysql> delete from mysql.user where host='::1';
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
網(wǎng)站題目:Heartbeat+DRBD+MySQL高可用架構(gòu)方案與實(shí)施
鏈接地址:http://www.rwnh.cn/article48/igicep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、搜索引擎優(yōu)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、面包屑導(dǎo)航、服務(wù)器托管、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)