设为首页 收藏本站
查看: 635|回复: 0

[经验分享] DRBD+heartbeat 解决nfs单点故障

[复制链接]
发表于 2019-1-7 07:32:56 | 显示全部楼层 |阅读模式
1.1   环境准备
1.1.1           添加新磁盘,分区并格式化
fdisk -uc /dev/sdb
mkfs.ext4 /dev/sdb1
dd if=/dev/zero of=/dev/sdb1bs=1M count=1

1.1.2           在hosts文件中添加以下记录
cat /etc/host
172.16.1.111 test01
172.16.1.112 test02
172.16.1.113 web

1.1.3           关闭selinux和iptables
setenforce 0
/etc/init.d/iptables stop

1.1.4           测试约定
  
  主机名,系统版本及内核版本:
[root@test01 ~]# uname -nr
test01 2.6.32-573.el6.x86_64
[root@test01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)  

[root@test02 ~]# uname -nr
test02 2.6.32-573.el6.x86_64
[root@test02 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)

[root@web ~]# uname -nr
web 2.6.32-573.el6.x86_64
[root@web ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)测试IP:
test01 172.16.1.111
test02 172.16.1.112
web   172.16.1.113
虚拟IP 172.16.1.100

  


1.2   DRBD的安装
1.2.1           配置DRBD
  默认官方源中没有drbd软件,所以使用elrepo
[root@test01 ~]# rpm --importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@test01 ~]# rpm -Uvhhttp://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
Retrieving http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
Preparing...               ########################################### [100%]
   1:elrepo-release        ########################################### [100%]
[root@test01 ~]# yum installdrbd84-utils kmod-drbd84 -y

[root@test01 ~]# modprobe drbd
[root@test01 ~]# lsmod|grepdrbd
drbd                  365931  0
libcrc32c               1246  1 drbd

  编辑配置文件
[root@test01 ~]# vim/etc/drbd.conf
# You can find an examplein  /usr/share/doc/drbd.../drbd.conf.example
#include"drbd.d/*.res";
global{
    usage-count no;
}
common{
    syncer{
        rate 200M;
    }
}
resource r1 {
      protocol C;
      startup{
          wfc-timeout 120;
          degr-wfc-timeout 120;
      }
      disk {
          on-io-error detach;
      }
      net {
          timeout 60;
          connect-int 10;
          ping-int 10;
          max-buffers 2048;
          max-epoch-size 2048;
          cram-hmac-alg"sha1";
          shared-secret"Mysql-abcD";
      }
      on test01 {
          device /dev/drbd0;
          disk /dev/sdb1;
          address 172.16.1.31:6666;
          meta-disk internal;
      }
      on test02 {
          device /dev/drbd0;
          disk /dev/sdb1;
          address 172.16.1.41:6666;
          meta-disk internal;
      }
}
1.2.3           初始化设备
[root@test01 ~]# drbdadmcreate-md r1
initializing activity log
NOT initializing bitmap
Writing meta data...
New drbd meta data blocksuccessfully created.
[root@test01 ~]# drbdadm up r1
[root@test01 ~]# cat /proc/drbd
version: 8.4.6(api:1/proto:86-101)
GIT-hash:833d830e0152d1e457fa7856e71e11248ccf3f70 build by phil@Build64R6, 2015-04-0914:35:00
0: cs:Connected ro:Secondary/Secondaryds:Inconsistent/Inconsistent C r-----
    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0ua:0 ap:0 ep:1 wo:f oos:5241660

  现在两个节点都处于Secondary状态      
  test01手动提升至primary。并将/dev/drbd0进行格式化以供挂在使用
[root@test01 ~]# drbdadmprimary --force r1
[root@test01 ~]# cat/proc/drbd
version: 8.4.6(api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70build by root@test01, 2015-12-07 10:40:31
0: cs:Connected ro:Primary/Secondaryds:UpToDate/UpToDate C r-----
    ns:1047484 nr:0 dw:0 dr:1048148 al:0 bm:0lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

1.2.4           格式化虚拟分区
[root@test01 ~]# mkfs.ext4/dev/drbd0
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
..
...
Writing superblocks andfilesystem accounting information: 完成
This filesystem will beautomatically checked every 33 mounts or
180 days, whichever comesfirst.  Use tune2fs -c or -i to override.
[root@test01 ~]# tune2fs -c -1/dev/drbd0
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to-1

1.2.5           挂载测试
[root@test01 ~]# mkdir /data
[root@test01 ~]# mount -t ext4/dev/drbd0 /data
[root@test01 ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.5G  7.4G  17% /
tmpfs           495M    0  495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data
[root@test01 ~]# touch/data/drbd.test
[root@test01 ~]# ls /data/
drbd.test lost+found

  手动切换连个节点状态,并进行挂载测试
[root@test01 ~]# umount /data
[root@test01 ~]# drbdadmsecondary r1
[root@test01 ~]# cat/proc/drbd
version: 8.4.6(api:1/proto:86-101)
GIT-hash:833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@test01, 2015-12-0710:40:31
0: cs:Connected ro:Secondary/Secondaryds:UpToDate/UpToDate C r-----
    ns:1080828 nr:0 dw:33344 dr:1048921 al:10bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

  test01手动提升至primary。并将/dev/drbd0进行格式化以供挂在使用
[root@test02drbd-utils-8.9.2]# drbdadm primary r1
[root@test02drbd-utils-8.9.2]# cat /proc/drbd
version: 8.4.6(api:1/proto:86-101)
GIT-hash:833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@test02, 2015-12-0710:40:31
0: cs:Connected ro:Primary/Secondaryds:UpToDate/UpToDate C r-----
    ns:0 nr:1080828 dw:1080828 dr:664 al:0 bm:0lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@test02 ~]# mkdir /data
[root@test02 ~]# mount -t ext4/dev/drbd0 /data
[root@test02 ~]# ls /data/
drbd.test lost+found
[root@test01 ~]# umount /data
[root@test01 ~]# drbdadmsecondary r1
  现在drbd两个节点已经配置成功,并手动测试成功。drbd的两个节点,同时时间只有primary状态的节点提供服务。
  

1.3   配置nfs服务
  test01 test02 web配置nfs的共享目录为/data test01 test02同时/dev/drbd0的挂载目录也是/data
1.3.1           将test01 drbd状态提升为primary并将/dev/drbd0挂载至/data,编辑/etc/exports文件,并本地测试。
[root@test01 ~]# cat/proc/drbd   
version: 8.4.6(api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70build by root@test01, 2015-12-07 10:40:31
0: cs:Connected ro:Secondary/Secondaryds:UpToDate/UpToDate C r-----
    ns:1080828 nr:8 dw:33352 dr:1048921 al:10bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@test01 ~]# drbdadmprimary r1
[root@test01 ~]# mount -t ext4/dev/drbd0 /data
[root@test01 ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.5G  7.4G  17% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data

[root@test01 ~]# cat/etc/exports   
/data172.16.1.0/24(rw,sync,all_squash)

[root@test01 ~]#/etc/init.d/rpcbind start
[root@test01 ~]#/etc/init.d/nfs start
[root@test01 ~]# chown -R nfsnobody.nfsnobody/data
[root@test01 ~]# showmount -e172.16.1.111
Export list for 172.16.1.111:
/data 172.16.1.0/24

[root@test01 ~]# mount -t nfs172.16.1.111:/data /mnt
[root@test01 ~]# ls /mnt
drbd.test lost+found
[root@test01 ~]# touch/mnt/test01.test
[root@test01 ~]# ls /mnt
drbd.test lost+found  test01.test

1.3.2           将test01状态降为secondary,将test02状态提升为primary,并将/dev/drbd0挂载至/data,编辑/etc/exports文件,并本地测试
[root@test01 ~]# umount /mnt
[root@test01 ~]#/etc/init.d/nfs stop
[root@test01 ~]# umount /data
[root@test01 ~]# drbdadmsecondary r1
[root@test01 ~]# cat/proc/drbd
version: 8.4.6(api:1/proto:86-101)
GIT-hash:833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@test01, 2015-12-0710:40:31
0: cs:Connected ro:Secondary/Secondaryds:UpToDate/UpToDate C r-----
    ns:1080952 nr:8 dw:33476 dr:1049979 al:10bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@test02 ~]# drbdadm primary r1
[root@test02 ~]# mount -t ext4/dev/drbd0 /data
[root@test02 ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.5G  7.4G  17% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data

[root@test02 ~]# cat/etc/exports   
/data172.1.6.0/24(rw,sync,all_squash)

[root@test02 ~]#/etc/init.d/rpcbind start
[root@test02 ~]#/etc/init.d/nfs start
[root@test02 ~]# chown -Rnfsnobody.nfsnobody /data
[root@test02 ~]# showmount -e172.16.1.112
Export list for 172.16.1.112:
/data 172.16.1.0/24

[root@test02 ~]# mount -t nfs172.16.1.112:/data /mnt
[root@test02 ~]# ls /mnt
drbd.test lost+found  test01.test
[root@test02 ~]# touch/mnt/test02.test
[root@test02 ~]# ls /mnt
drbd.test lost+found  test01.test  test02.test

[root@test02 ~]# umount /mnt
[root@test02 ~]#/etc/init.d/nfs stop
[root@test02 ~]# umount /data
[root@test02 ~]# drbdadmsecondary r1
version: 8.4.6(api:1/proto:86-101)
GIT-hash:833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@test02, 2015-12-0710:40:31
0: cs:Connected ro:Secondary/Secondaryds:UpToDate/UpToDate C r-----
    ns:124 nr:1080952 dw:1081076 dr:1726 al:2bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
  至此:drbd nfs手动挂载测试成功
1.4   配置heartbeat
  配置heartbeat以达到nfs主节点失效自动将所有资源切换到备用节点
1.4.1           两台nfs服务器上安装heartbeat
添加epel扩展源。官方yum源没有heartbeat
yum -y install epel-release
yum install heartbeat -y

1.4.2           将两台服务器默认配置文件拷贝到/etc/ha.d/并做更改
[root@test01 ~]# rpm -qdheartbeat|grep doc
/usr/share/doc/heartbeat-3.0.4/AUTHORS
/usr/share/doc/heartbeat-3.0.4/COPYING
/usr/share/doc/heartbeat-3.0.4/COPYING.LGPL
/usr/share/doc/heartbeat-3.0.4/ChangeLog
/usr/share/doc/heartbeat-3.0.4/README
/usr/share/doc/heartbeat-3.0.4/apphbd.cf
/usr/share/doc/heartbeat-3.0.4/authkeys
/usr/share/doc/heartbeat-3.0.4/ha.cf
/usr/share/doc/heartbeat-3.0.4/haresources

[root@test01 ~]# cp/usr/share/doc/heartbeat-3.0.4/authkeys /usr/share/doc/heartbeat-3.0.4/ha.cf/usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/

[root@test01 ~]# egrep -v"#|^$" /etc/ha.d/ha.cf
logfile /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 30
warntime 10
initdead 60
mcast eth0 225.0.0.1 694 1 0
auto_failback on
node    test01
node    test02

[root@test01 ~]# egrep -v"#|^$" /etc/ha.d/authkeys
auth 1
1 sha1 liyanan
[root@test01 ~]# chmod600  /etc/ha.d/authkeys
[root@test01 ~]# ll  /etc/ha.d/authkeys         
-rw------- 1 root root 647 12月  7 15:13 /etc/ha.d/authkeys
[root@test01 ~]#echo " test01drbddisk::r1 Filesystem::/dev/drbd0::/data::ext4 nfsIPaddr::172.16.1.100/24/eth1
" >>/etc/ha.d/haresources

将附件的脚本加入到/etc/ha.d/resource.d/ 下并赋予可执行权限。
1.4.3           启动两台服务器heartbeat
[root@test01 ~]#/etc/init.d/heartbeat start
[root@test01 ~]#/etc/init.d/heartbeat status
heartbeat OK [pid 5362 et al]is running on test01 [test01]...
1.4.4           测试heartbeat
  全部启动heartbeat服务
[root@test01 ha.d]#/etc/init.d/heartbeat status
heartbeat OK [pid 22646 et al]is running on test01 [test01]...
[root@test02 ~]#/etc/init.d/heartbeat status
heartbeat OK [pid 13217 et al]is running on test02 [test02]...
  检查test01挂载
[root@test01 ha.d]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data

  检查test02挂载
[root@test02 ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot

  关闭test01的heartbeat服务后再检查挂载
[root@test01 ha.d]#/etc/init.d/heartbeat stop
Stopping High-Availabilityservices: Done.

[root@test01 ha.d]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M    0  495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot

  再次检查test02挂载
[root@test02 ~]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data
  再次启动test01的heartbeat服务,并检查挂载
[root@test01 ha.d]#/etc/init.d/heartbeat start
Starting High-Availabilityservices: Done.

[root@test01 ha.d]# df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
/dev/drbd0      991M 1.3M  939M   1% /data

  再次检查test02挂载
[root@test02 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.4G 1.6G  7.3G  18% /
tmpfs           495M     0 495M   0% /dev/shm
/dev/sda1       190M  36M  145M  20% /boot
  
  经过循环测试,证明heartbeat服务已经成功接管drbd nfs VIP服务
  可通过VIP向外部提供nfs挂载解决nfs单点故障。
  

  终极测试,循环写入数据测试丢包率
  测试方式:由test01为主,提供nfs服务、test02为备,接管nfs服务。web挂载test01提供的nfs共享目录。通过间隔 1S 连续在web的挂载点上写入数据,期间手动关闭test01的heartbeat服务,造成意外宕机的实验效果,等脚本执行完毕后,在web端查看写入的数据,以便检测丢包率。

  

  检查挂载情况及虚拟IP
  
[root@test01 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.4G  1.6G  7.4G  18% /
tmpfs           242M     0  242M   0% /dev/shm
/dev/sda1       190M   36M  145M  20% /boot
/dev/drbd0      4.8G   11M  4.6G   1% /data
[root@test01 ~]# ip add |grep 172.16
    inet 172.16.1.111/24 brd 172.16.1.255 scope global eth1
    inet 172.16.1.100/24 brd 172.16.1.255 scope global secondary eth1  
  
[root@test02 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.4G  1.6G  7.4G  18% /
tmpfs           242M     0  242M   0% /dev/shm
/dev/sda1       190M   36M  145M  20% /boot
[root@test02 ~]# ip add |grep 172.16
    inet 172.16.1.112/24 brd 172.16.1.255 scope global eth1  
[root@web ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             9.4G  2.1G  6.9G  24% /
tmpfs                 242M     0  242M   0% /dev/shm
/dev/sda1             190M   36M  145M  20% /boot
172.16.1.100:/data  4.8G   11M  4.6G   1% /data/www/
  测试脚本:
for ((n=1;n

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-660121-1-1.html 上篇帖子: Heartbeat和syslog配置 下篇帖子: Linux 高可用(HA)集群之Heartbeat安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表