heartbeat+DRBD 高可用 双机热备

时间:2022-02-25 03:12:36

heartbeat+DRBD 高可用 双机热备

原创博文http://www.cnblogs.com/elvi/p/7658109.html

## heartbeat+DRBD 高可用 双机热备
# 环境centos6 
# 测试演练

# 虚拟机添加10G硬盘
# 添加网卡,作为数据传输
#主机
VIP 192.168.18.80
web1
192.168.18.11
192.168.99.11(心跳线)
192.168.100.11(数据传输)
web2
192.168.18.12
192.168.99.12
192.168.100.12
vim /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE="eth2"
BOOTPROTO="static"
IPADDR=192.168.100.12
NETMASK=255.255.255.0
ONBOOT="yes"
#
ifup eth2
uname -r

#关闭防火墙

#分区
#fdisk快速分区
#echo -e "n\np\n1\n+10G\nn\np\n2\n\n+20G\nw" | fdisk /dev/sdb
fdisk -l
fdisk /dev/sdb
    p
    n
    1
    +8G
    2
    w
#备节点、状态分区 不需要格式化
mkfs.ext4 /dev/sdb1
e2label /dev/sdb1 /data #给分区标签
 
#
#安装 DRBD
rpm -Uvh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
yum install drbd kmod-drbd84 -y
#内核加载
lsmod |grep drbd
modprobe drbd #加载DRBD模块到内核
modprobe -l | grep -i drbd 
echo "modprobe drbd" >>/etc/rc.local ##开机自动加载
#yum install kernel-devel -y

#模板
ll /etc/drbd.d/
#配置
vim  /etc/drbd.conf
global {
        usage-count no;
}
common {
       syncer {
             rate 200M;
             verify-alg crc32c;  
       }
}
resource webdata {
   protocol C;

   disk {
     on-io-error detach;
   }
   on web1 {
     device    /dev/drbd0; 
     disk      /dev/sdb1;
     address    192.168.100.11:7788;
     meta-disk /dev/sdb2[0];   
   }
   on web2 {
     device    /dev/drbd0;
     disk      /dev/sdb1;
     address    192.168.100.12:7788;
     meta-disk /dev/sdb2[0];  
  }
}

#drbd初始化与启动
drbdadm create-md webdata
drbdadm up webdata
/etc/init.d/drbd start
#将第一个节点置为primary
drbdadm -- --overwrite-data-of-peer primary webdata
#drbdsetup /dev/drbd0 primary -o

#查看同步完成状态
cat /proc/drbd 
#磁盘挂载(仅primary节点),Secondary挂载需要先drbdadm down data -->mount /dev/sdb1 /data
mkdir /data
mount /dev/drbd0 /data
df -l

#验证同步
#主节点创建文件
echo $(date +%Y-%m-%d-%H%M%S)>>/data/test.txt
cat /data/test.txt
#备节点停用服务,挂载分区查看
drbdadm down webdata
mount /dev/sdb1 /data
ll /data
cat /data/test.txt
#备节点启用
umount /dev/sdb1
drbdadm up webdata
cat /proc/drbd

#DRBD主备节点切换
#主:卸载分区,设置为备节点
umount /dev/drbd0
drbdadm secondary webdata     #(all)
#备:设置为主节点,挂载分区
drbdadm primary webdata     #(all)
mount /dev/drbd0 /data

# DRBD脑裂后的处理
#从节点上切换成secondary,并放弃该资源的数据:
drbdadm secondary alldrbdadm -- --discard-my-data connect all
drbdadm -- --overwrite-data-of-peer primary data #设置主节点同步数据到备节点
#重新连接资源
drbdadm down webdata
drbdadm connect webdata
drbdadm up webdata
drbdadm primary all #主
drbdadm secondary all #备

#+ heartbeat高可用
vim /etc/ha.d/haresources
#web1 IPaddr::192.168.18.80/24/eth0 nginx
web1 IPaddr::192.168.18.80/24/eth0 nginx drbddisk::webdata Filesystem::/dev/drbd0::/data::ext4 
#主节点 ,解除磁盘挂载,重启heartbeat服务
umount /dev/drbd0
service heartbeat stop
service heartbeat start
#备节点启动heartbeat
service heartbeat start
#查看服务
ip add | grep eth0
mount|grep data
cat /proc/drbd

#主备切换验证
service heartbeat stop #主,停止服务
#查看备节点服务
ip add | grep eth0
mount|grep data
cat /proc/drbd
service heartbeat restart #主,启动服务

#备节点实际容量变小调整
/etc/init.d/drbd stop
e2fsck -f /dev/sdb1
resize2fs /dev/sdb1

#增加主机测试(主备/usr/share/nginx/html主页文件不同)
ln -s /usr/share/nginx/html /data/html
vim /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name test.com;
location / {
    root /data/html;
    index index.html index.htm index.php;
        }
    }
#重启服务
service heartbeat stop
service heartbeat start
#本机访问,修改本机hosts文件C:\Windows\System32\drivers\etc
192.168.18.80 test.com
#浏览器访问域名 test.com

##NFS
#服务端配置
mkdir /data/share
#vim /etc/exports
echo "/data/share 192.168.18.0/24(rw,no_root_squash)">>/etc/exports
/etc/init.d/rpcbind restart
/etc/init.d/nfs restart
#客户端
showmount -e 192.168.18.80 #查看共享
service rpcbind start
mkdir -p /data/share
umount /data/share
mount -t nfs 192.168.18.80:/data/share /data/share
#查看测试
mount|grep /data/share
echo $(date +%Y-%m-%d-%H%M%S)>>/data/share/nfs.txt
cat /data/share/nfs.txt
##开机挂载
echo "192.168.18.80:/data/share /data/share nfs defaults 0 0">>/etc/fstab

#heartbeat+drbd+nfs配置
vim /etc/ha.d/haresources
web1 IPaddr::192.168.18.80/24/eth0 nginx drbddisk::webdata Filesystem::/dev/drbd0::/data::ext4 nfs
showmount -e localhost
#主备切换验证 ……  

mount|grep /data/share
echo $(date +%Y-%m-%d-%H%M%S)>>/data/share/nfs.txt
cat /data/share/nfs.txt
showmount -e localhost

mount|grep data
ip add | grep eth0
cat /proc/drbd

service heartbeat stop
service heartbeat start
service heartbeat restart


# 参考
#http:
//blog.csdn.net/lt53130640688/article/details/50152615 #http://www.cnblogs.com/wsl222000/p/5777382.html