您好,登錄后才能下訂單哦!
實驗參考 http://blog.csdn.net/qq_19175749/article/details/51607210
官方站點:http://linux-ha.org/wiki/Main_Page
heartbeat可以資源(VIP地址及程序服務(wù))從一臺有故障的服務(wù)器快速的轉(zhuǎn)移到另一臺正常的服務(wù)器提供服務(wù),heartbeat和keepalived相似,heartbeat可以實現(xiàn)failover功能,但不能實現(xiàn)對后端的健康檢查
heartbeat和keepalived應用場景及區(qū)別
很多網(wǎng)友說為什么不使用keepalived而使用長期不更新的heartbeat,下面說一下它們之間的應用場景及區(qū)別
1、對于web,db,負載均衡(lvs,haproxy,nginx)等,heartbeat和keepalived都可以實現(xiàn)
2、lvs最好和keepalived結(jié)合,因為keepalived最初就是為lvs產(chǎn)生的,(heartbeat沒有對RS的健康檢查功能,heartbeat可以通過ldircetord來進行健康檢查的功能)
3、mysql雙主多從,NFS/MFS存儲,他們的特點是需要數(shù)據(jù)同步,這樣的業(yè)務(wù)最好使用heartbeat,因為heartbeat有自帶的drbd腳本
總結(jié):無數(shù)據(jù)同步的應用程序高可用可選擇keepalived,有數(shù)據(jù)同步的應用程序高可用可選擇heartbeat
官方站點: http://www.drbd.org/users-guide-8.4/
DRBD(DistributedReplicatedBlockDevice)是一個基于塊設(shè)備級別在遠程服務(wù)器直接同步和鏡像數(shù)據(jù)的軟件,用軟件實現(xiàn)的、無共享的、服務(wù)器之間鏡像塊設(shè)備內(nèi)容的存儲復制解決方案。它可以實現(xiàn)在網(wǎng)絡(luò)中兩臺服務(wù)器之間基于塊設(shè)備級別的實時鏡像或同步復制(兩臺服務(wù)器都寫入成功)/異步復制(本地服務(wù)器寫入成功),相當于網(wǎng)絡(luò)的RAID1,由于是基于塊設(shè)備(磁盤,LVM邏輯卷),在文件系統(tǒng)的底層,所以數(shù)據(jù)復制要比cp命令更快。DRBD已經(jīng)被MySQL官方寫入文檔手冊作為推薦的高可用的方案之一
官方站點: http://www.mysql.com/
MySQL是一個開放源碼的小型關(guān)聯(lián)式數(shù)據(jù)庫管理系統(tǒng)。目前MySQL被廣泛地應用在Internet上的中小型網(wǎng)站中。由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網(wǎng)站為了降低網(wǎng)站總體擁有成本而選擇了MySQL作為網(wǎng)站數(shù)據(jù)庫。
架構(gòu)說明:
一主多從最常用的架構(gòu),多個從庫可以使用lvs來提供讀的負載均衡
解決一主單點的問題,當主庫宕機后,可以實現(xiàn)主庫宕機后備節(jié)點自動接管,所有的從庫會自動和新的主庫進行同步,實現(xiàn)了mysql主庫的熱備方案
系統(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 (提供對外服務(wù)地址) |
master1 |
eth0: 192.168.12.55/24 (內(nèi)網(wǎng)) eth2: 10.1.12.55/24 (心跳線) 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 (心跳線) eth3: 10.2.12.56/24(DRBD千兆數(shù)據(jù)傳輸) |
slave1 |
eth0: 192.168.12.55/24(暫時放在master1上) |
說明:從庫通過主庫的VIP進行主從同步replication |
|
需求: 1、主庫master1宕機后master2自動接管VIP以及所有從庫 2、在master2接管時,不影響從庫的主從同步replication |
磁盤 |
容量 |
分區(qū) |
掛載點 |
說明 |
/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ū)不要進行掛載 3、生產(chǎn)環(huán)境DRBDmetadata分區(qū)一般可設(shè)置為1-2G,數(shù)據(jù)分區(qū)看需求給最大 4、在生產(chǎn)環(huán)境中兩塊硬盤一樣大 |
## 主機名、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 《到對端心跳路由》
[root@master1 ~]# route add -host 10.2.12.56 dev eth3 《到對端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
## 如果開啟防火墻,需要放行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
說明:
drbddisk::data <==啟動drbd data資源,相當于執(zhí)行/etc/ha.d/resource.d/drbddisk data stop/start操作
Filesystem::/dev/drbd1::/data::ext4 <==drbd分區(qū)掛載到/data目錄,相當于執(zhí)行/etc/ha.d/resource.d/Filesystem /dev/drbd1 /data ext4 stop/start <==相當于系統(tǒng)中執(zhí)行mount /dev/drbd1 /data
mysql <==啟動mysql服務(wù)腳本,相當于/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
說明:master1宕機后,vip地址漂移到master2節(jié)點上,master2成為主節(jié)點
## 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
說明:master1啟動后,vip地址漂移回master1節(jié)點上,master1成為主節(jié)點。原因是/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é)點兩端配置文件完全一致
# 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
## 配置開機自動加載
# 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é)點兩端配置文件完全一致
[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é)點兩端配置文件完全一致
[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é)點操作
[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
說明:master1為主節(jié)點,master2為備節(jié)點
## 模擬master1宕機
[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
說明:master1宕機后,master2可以升級為主節(jié)點,掛載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
## 測試切換
[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從庫也配置在master1上,這里暫時不配置slave從庫
# 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 指定用戶uid ----s 指定用戶登錄所使用shell ----M 不要創(chuàng)建用戶登錄目錄 ----g 指定用戶所屬組 ----G指定用戶附加組
# groupadd -g 1200 mysql
# useradd mysql -s /sbin/nologin -M -g mysql -u 1200
---hosts配置(這里不用配置,因為后續(xù)mysql是通過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存儲上
# 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ù)庫存放數(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/
## 啟動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
## 登錄測試
# mysql -uroot -p111111 -S /data/master/mysql.sock
## 注意master1、master2都需要添加,并且需要添加到ha啟動目錄下
# 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
## 測試啟停
# 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磁盤掛載到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
## 啟停測試
[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ù)移動到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
## 修改啟動參數(shù)
# vi /etc/ha.d/haresources
master1 IPaddr::192.168.12.1/24/eth0 drbddisk::data Filesystem::/dev/drbd1::/data::ext4 mysqld
## 啟動高可用
# /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
## 測試切換
[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)配置好,當前節(jié)點在master1上
## 簡單配置優(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)
mysql> flush privileges;
## 修改主庫參數(shù)
[root@master1 ~]# vi /data/master/my.cnf
[mysqld]
server-id = 1
log-bin=/data/master/mysql-bin
## 重啟mysql
[root@master1 ~]# /etc/ha.d/resource.d/mysqld restart
[root@master1 ~]# netstat -nltpd |grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 43242/mysqld
[root@master1 ~]# ll /data/master/|grep mysql-bin
-rw-rw---- 1 mysql mysql 126 Mar 19 16:03 mysql-bin.000001
-rw-rw---- 1 mysql mysql 107 Mar 19 16:03 mysql-bin.000002
-rw-rw---- 1 mysql mysql 60 Mar 19 16:03 mysql-bin.index
## 主庫創(chuàng)建同步帳號rep
# mysql -uroot -p111111 -S /data/master/mysql.sock
mysql> grant replication slave on *.* to 'rep'@'192.168.12.%' identified by '123456';
mysql> flush privileges;
mysql> select user,host from mysql.user;
## 測試連接,從庫通過vip進行同步
# mysql -urep -p123456 -h292.168.12.1 -P3306
## 從庫安裝到master1的本地磁盤
[root@master1 ~]# mkdir -p /mysql/slave
[root@master1 ~]# cd /app/mysql/scripts/
# ./mysql_install_db --basedir=/app/mysql --datadir=/mysql/slave/data --user=mysql
# vi /mysql/slave/my.cnf
[mysqld]
socket = /mysql/slave/mysql.sock
port = 3307
pid-file = /mysql/slave/mysql.pid
datadir = /mysql/slave/data
basedir = /app/mysql
user = mysql
server-id=2
[client]
port = 3307
socket = /mysql/slave/mysql.sock
[mysql]
no-auto-rehash
# chown -R mysql. /mysql/slave
## 啟動mysql
# mysqld_safe --defaults-file=/mysql/slave/my.cnf &
# netstat -nltpd |grep mysql
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 62750/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 62462/mysqld
## 從庫通過vip連接主庫
[root@master1 ~]# mysql -uroot -p -S /mysql/slave/mysql.sock
mysql> change master to
master_host='192.168.12.1',
master_port=3306,
master_user='rep',
master_password='123456';
mysql> start slave;
mysql> show slave status\G
## 測試主從同步
[root@master1 ~]# mysql -uroot -p111111 -S /data/master/mysql.sock
mysql> create database shaw_db;
mysql> create table shaw_db.t_user as select * from mysql.user;
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
## 從庫查看數(shù)據(jù)
mysql> show databases like 'shaw%';
+------------------+
| Database (shaw%) |
+------------------+
| shaw_db |
+------------------+
mysql> select count(*) from shaw_db.t_user;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
## 這里注意,為什么這里主庫插入3條數(shù)據(jù),而從庫插入7條數(shù)據(jù),因為從庫初始化后直接change master同步的主庫數(shù)據(jù),而mysql庫中user表里面的數(shù)據(jù)并沒有清理,因此查詢的數(shù)據(jù)會比主庫多
mysql> select user,host from shaw_db.t_user;
+------+--------------+
| user | host |
+------+--------------+
| root | localhost |
| root | master1 |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| | master1 |
| rep | 192.168.12.% |
+------+--------------+
7 rows in set (0.00 sec)
## 目前主庫在master1上,切換ha到master2上
[root@master1 ~]# /etc/init.d/heartbeat stop
## 此時主庫已經(jīng)切換到master2上,查看當前從庫狀態(tài)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.12.1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 537
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 683
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 537
Relay_Log_Space: 985
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error reconnecting to master 'rep@192.168.12.1:3306' - retry-time: 60 retries: 86400
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
## 等待60s后,從庫自動和master2同步了
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.12.1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 107
Relay_Log_File: mysql-relay-bin.000005
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 107
Relay_Log_Space: 555
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
## 此時在master2上創(chuàng)建一些表 看是否能同步到從庫上
[root@master2 ~]# mysql -uroot -p111111 -S /data/master/mysql.sock
mysql> use shaw_db;
mysql> create table t_zhong (id int,name varchar(20));
mysql> insert into t_zhong values(1,'test'),(2,'test2'),(3,'test3');
## 從庫查看數(shù)據(jù)
mysql> select * from shaw_db.t_zhong;
+------+-------+
| id | name |
+------+-------+
| 1 | test |
| 2 | test2 |
| 3 | test3 |
+------+-------+
1、高可用服務(wù)器之間心跳鏈路故障,導致無法相互檢查心跳
2、高可用服務(wù)器上開啟了防火墻,阻擋了心跳檢測
3、高可用服務(wù)器上網(wǎng)卡地址等信息配置不正常,導致發(fā)送心跳失敗
4、其他服務(wù)配置不當?shù)仍颍缧奶绞讲煌奶鴱V播沖突,軟件BUG等
1、加冗余線路
2、檢測到裂腦時,強行關(guān)閉心跳檢測(遠程關(guān)閉主節(jié)點,控制電源的電路fence)
3、做好腦裂的監(jiān)控報警
4、報警后,備節(jié)點在接管時設(shè)置比較長的時間去接管,給運維人員足夠的時間去處理(人為處理)
5、啟動磁盤鎖,正在服務(wù)的一方鎖住磁盤,裂腦發(fā)生時,讓對方完全搶不走"共享磁盤資源"
磁盤鎖存在的問題:
使用鎖磁盤會有死鎖的問題,如果占用共享磁盤的一方不主動"解鎖"另一方就永遠得不到共享磁盤,假如服務(wù)器節(jié)點突然死機或崩潰,就不可能執(zhí)行解鎖命令,備節(jié)點也就無法接管資源和服務(wù)了,有人在HA中設(shè)計了智能鎖,正在提供服務(wù)的一方只在發(fā)現(xiàn)心跳全部斷開時才會啟用磁盤鎖,平時就不上鎖
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。