heartbeat + nfs 实现高可用共享资源
主节点node1:192.168.200.101/24
备节点node2: 192.168.200.102/24
nfs: 192.168.200.103/24
安装前准备
heartbeat v2
ha web 实例
1、节点名称修改:
[root@node1 ~]#hostname node1
[root@node1 ~]# uname -n
node1
[root@node1 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1
2、ssh 互相通信:
[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' //自动生成秘钥
[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2 //传送至节点node2
[root@node1 ~]# ssh node2 'ifconfig' //测试是否能操作节点node2
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:7C:3F
inet addr:192.168.200.102 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe65:7c3f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:469456 errors:0 dropped:0 overruns:0 frame:0
TX packets:632160 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:224664819 (214.2 MiB) TX bytes:339254044 (323.5 MiB)
node2 节点配置相同,传送给node1.
3、主机名解析:
cat /etc/hosts
192.168.200.101 node1
192.168.200.102 node2
4、时间同步:
主节点配置:
[root@node1 ~]# yum -y install ntp
[root@node1 ~]# cp -p /etc/ntp.conf /etc/ntp.conf.origin
[root@node1 ~]# vim /etc/ntp.conf
22 server 127.127.1.0 //手动添加此两行内容
23 fudge 127.127.1.0 startum 8
[root@node1 ~]#service ntpd start
正在启动 ntpd: [确定]
[root@node1 ~]#chkconfig ntpd on
从节点配置:
[root@node2 ~]# yum -y instal ntpdate
[root@node2 ~]# ntpdate 192.168.200.101
4 Aug 16:41:16 ntpdate[40441]: adjust time server 192.168.200.101 offset -0.067223 sec
添加计划任务每5分钟执行一次。
[root@node2 ~]# crontab -l
*/5 * * * * /sbin/ntpdate 192.168.200.101 &>/dev/null
[root@nfs ~]# crontab -l
*/5 * * * * /sbin/ntpdate 192.168.200.101 &>/dev/null
nfs 和 node2 一样同步时间与node1时间服务器。
准备的安装包
[root@node1 heartbeat]# ls
cluster-glue-1.0.5-6.el6.x86_64.rpm perl-TimeDate-1.16-13.el6.noarch.rpm
cluster-glue-libs-1.0.5-6.el6.x86_64.rpm PyXML-0.8.4-19.el6.x86_64.rpm
heartbeat-3.0.4-2.el6.x86_64.rpm resource-agents-3.9.5-24.el6_7.1.x86_64.rpm
heartbeat-libs-3.0.4-2.el6.x86_64.rpm start.sh
lib64ltdl7-2.2.6-6.1mdv2009.1.x86_64.rpm
安装的顺序:
1、rpm -ivh PyXML-0.8.4-19.el6.x86_64.rpm
2、rpm -ivh resource-agents-3.9.5-24.el6_7.1.x86_64.rpm
3、rpm -ivh cluster-glue-libs-1.0.5-6.el6.x86_64.rpm
4、rpm -ivh perl-TimeDate-1.16-13.el6.noarch.rpm
5、rpm -ivh cluster-glue-1.0.5-6.el6.x86_64.rpm
6、rpm -ivh heartbeat-libs-3.0.4-2.el6.x86_64.rpm heartbeat-3.0.4-2.el6.x86_64.rpm //同时安装。
备节点node2 安装顺序相同。
安装后有三个配置文件:
1、秘钥文件,600,authkeys
2、heartbeat服务的配置文件ha.cf
3、资源管理配置文件:haresources(v2有2个资源管理器一个是crm,另一个默认v2版本使用haresources作为资源管理器)
[root@node1 heartbeat-3.0.4]# cd /etc/ha.d/
[root@node1 ha.d]# cp /usr/share/doc/heartbeat-3.0.4/{haresources,authkeys,ha.cf} ./
[root@node1 ha.d]# cat authkeys
#auth 1
#1 crc
#2 sha1 HI!
#3 md5 Hello!
auth 3
3 md5 3c94943451e56d84786bdf5072964350 // dd if=/dev/random cout=1 bs=512 |md5sum 添加一个随机码 放到md5 后面。
[root@node1 ha.d]# grep -v "^#" ha.cf
logfacility local0
keepalive 2
bcast eth0 # Linux
auto_failback on
node node1
node node2
[root@node1 ha.d]# cat haresources
node1 IPaddr::192.168.200.254/24/eth0:0 httpd
备节点和主节点配置一样。
======================================================================
各节点关闭httpd开机自启,让heartbeat启动httpd服务。
service httpd stop
chkconfg httpd off
[root@node1 ~]# echo "node1" > /var/www/html/index.html
[root@node2 ~]# echo "node2" > /var/www/html/index.html
测试:
主节点
[root@node1 html]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:4C:DD:5D
inet addr:192.168.200.101 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4c:dd5d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8825 errors:0 dropped:0 overruns:0 frame:0
TX packets:4271 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1030060 (1005.9 KiB) TX bytes:723112 (706.1 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:4C:DD:5D
inet addr:192.168.200.254 Bcast:192.168.200.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
备节点
[root@node2 ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:7C:3F
inet addr:192.168.200.102 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe65:7c3f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:465233 errors:0 dropped:0 overruns:0 frame:0
TX packets:629414 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:224046430 (213.6 MiB) TX bytes:338744439 (323.0 MiB)
当主节点挂了备节点是否能接过来。
使用heartbeat自带的测试脚本工具
[root@node1 ha.d]# /usr/share/heartbeat/hb_standby
Going standby [all].
主节点
[root@node1 html]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:4C:DD:5D
inet addr:192.168.200.101 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4c:dd5d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8825 errors:0 dropped:0 overruns:0 frame:0
TX packets:4271 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1030060 (1005.9 KiB) TX bytes:723112 (706.1 KiB)
备节点
[root@node2 ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:7C:3F
inet addr:192.168.200.102 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe65:7c3f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:465386 errors:0 dropped:0 overruns:0 frame:0
TX packets:629536 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:224070947 (213.6 MiB) TX bytes:338766983 (323.0 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:65:7C:3F
inet addr:192.168.200.254 Bcast:192.168.200.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
========================================================================================================
配置nfs
[root@nfs ~]# cat /etc/exports
/web/htdocs 192.168.200.0/24(insecure,ro,no_root_squash)
echo "nfs server" >/web/htdocs/index.html
service rpcbind start
service nfs start
先开启rpc在开启nfs 顺序不能搞错。关闭也是先关闭rpc在关闭nfs.
[root@node1 ~]# showmount -e 192.168.200.103 //查看挂载情况
Export list for 192.168.200.103:
/web/htdocs 192.168.200.0/24
[root@node1 ~]# mount 192.168.200.103:/web/htdocs /var/www/html/ //测试手动挂载
[root@node1 ~]#umount /var/www/html/ //然后在卸载
先停止heartbeat node1 和node2 ,因为资源必须先启动在服务的前面。
然后配置haresources文件,添加Filesystem段nfs服务.
[root@node1 ~]# vim /etc/ha.d/haresources
node1 IPaddr::192.168.200.254/24/eth0:0 Filesystem::192.168.200.103:/web/htdocs::/var/www/html::nfs httpd
========================================================================================================
测试服务
[root@node1 ~]# curl http://192.168.200.254
nfs server
[root@node2 ~]# curl http://192.168.200.254
nfs server