主机名 | IP | 所需软件 |
master | 192.168.30.130 | heartbeat、httpd |
node-1 | 192.168.30.131 | nfs |
node-2 | 192.168.30.132 | heartbeat、httpd |
配置hosts文件
[root@master ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.30.130 master 192.168.30.131 node-1 192.168.30.132 node-2 192.168.30.133 node-3 [root@master ~]# scp /etc/hosts node-1:/etc/ [root@master ~]# scp /etc/hosts node-1:/etc/ [root@master ~]# scp /etc/hosts node-1:/etc/
配置共享存储
[root@node-1 ~]# yum install -y nfs-utils [root@node-1 ~]# mkdir /nfsdata [root@node-1 ~]# echo "<h1>This is the HeartBeat HTTP Server Test</h1>" > /var/www/html/index.html [root@node-1 ~]# vim /etc/exports /nfsdata 192.168.30.0/24(rw) [root@node-1 ~]# chown 777 -R /nfsdata/ [root@node-1 ~]# chkconfig nfs on [root@node-1 ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@node-1 ~]# showmount -e Export list for node-1: /nfsdata 192.168.30.0/24
配置WEB服务器
主web服务器
[root@master ~]# yum install -y httpd [root@master ~]# showmount -e node-1 Export list for node-1: /nfsdata 192.168.30.0/24 [root@master ~]# mount -t nfs node-1:/nfsdata /var/www/html/ [root@master ~]# df -h /var/www/html/ Filesystem Size Used Avail Use% Mounted on node-1:/nfsdata 18G 4.1G 13G 25% /var/www/html [root@master ~]# service httpd start Starting httpd: [ OK ] [root@master ~]# curl 127.1 <h1>This is the HeartBeat HTTP Server Test</h1> [root@master ~]# umount /var/www/html/ [root@master ~]# chkconfig httpd on
从web服务器
[root@node-2 ~]# yum install -y httpd [root@node-2 ~]# showmount -e node-1 Export list for node-1: /nfsdata 192.168.30.0/24 [root@node-2 ~]# mount -t nfs node-1:/nfsdata /var/www/html/ [root@node-2 ~]# df -h /var/www/html/ Filesystem Size Used Avail Use% Mounted on node-1:/nfsdata 18G 4.1G 13G 25% /var/www/html [root@node-2 ~]# service httpd start Stopping httpd: [ OK ] Starting httpd: [ OK ] 测试访问页面 [root@node-2 ~]# curl 127.1 <h1>This is the HeartBeat HTTP Server Test</h1> [root@node-2 ~]# umount /var/www/html/ #卸载测试资源,heartbeat安装完后,这些通过heartbeat加载 [root@node-2 ~]# chkconfig httpd on
安装heartbeat软件包
配置YUM源 [root@master ~]# ls /media/cdrom/ EFI Packages RELEASE-NOTES-pa-IN.html EULA README RELEASE-NOTES-pt-BR.html EULA_de RELEASE-NOTES-as-IN.html RELEASE-NOTES-ru-RU.html EULA_en RELEASE-NOTES-bn-IN.html RELEASE-NOTES-si-LK.html EULA_es RELEASE-NOTES-de-DE.html RELEASE-NOTES-ta-IN.html EULA_fr RELEASE-NOTES-en-US.html RELEASE-NOTES-te-IN.html EULA_it RELEASE-NOTES-es-ES.html RELEASE-NOTES-zh-CN.html EULA_ja RELEASE-NOTES-fr-FR.html RELEASE-NOTES-zh-TW.html EULA_ko RELEASE-NOTES-gu-IN.html repodata EULA_pt RELEASE-NOTES-hi-IN.html ResilientStorage EULA_zh RELEASE-NOTES-it-IT.html RPM-GPG-KEY-redhat-beta GPL RELEASE-NOTES-ja-JP.html RPM-GPG-KEY-redhat-release HighAvailability RELEASE-NOTES-kn-IN.html ScalableFileSystem images RELEASE-NOTES-ko-KR.html Server isolinux RELEASE-NOTES-ml-IN.html TRANS.TBL LoadBalancer RELEASE-NOTES-mr-IN.html media.repo RELEASE-NOTES-or-IN.html [root@master ~]# cd /media/cdrom/HighAvailability/ [root@master HighAvailability]# pwd /media/cdrom/HighAvailability [root@master ~]# vim /etc/yum.repos.d/rhel-source.repo [rhel-source] name=Red Hat Enterprise Linux $releasever - $basearch - Source baseurl=file:///media/cdrom enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [rhel-HA-source] name=Red Hat Enterprise Linux $releasever - $basearch -HA-Source baseurl=file:///media/cdrom/HighAvailability enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
开始创建heartbeat软件包
[root@master ~]# tar -xf Heartbeat-3-0-958e11be8686.tar.bz2 -C /usr/local/src/ [root@master ~]# cd /usr/local/src/Heartbeat-3-0-958e11be8686/ [root@master Heartbeat-3-0-958e11be8686]# rpmbuild -ba heartbeat-fedora.spec [root@master ~]# tar -xf Heartbeat-3-0-958e11be8686.tar.bz2 -C /usr/local/src/ [root@master ~]# cd /usr/local/src/Heartbeat-3-0-958e11be8686/ [root@master Heartbeat-3-0-958e11be8686]# rpmbuild -ba heartbeat-fedora.spec error: File /root/rpmbuild/SOURCES/heartbeat.tar.bz2: No such file or directory [root@master Heartbeat-3-0-958e11be8686]# cd [root@master ~]# ls /root/rpmbuild/SOURCES/ [root@master ~]# ll -d /root/rpmbuild/SOURCES/ drwxr-xr-x 2 root root 4096 Jun 5 17:11 /root/rpmbuild/SOURCES/ [root@master ~]# tar -xf Heartbeat-3-0-958e11be8686.tar.bz2 -C /root/rpmbuild/SOURCES/ [root@master ~]# cd /root/rpmbuild/SOURCES/ [root@master SOURCES]# mv Heartbeat-3-0-958e11be8686/ heartbeat [root@master SOURCES]# ls heartbeat [root@master SOURCES]# tar -cjvf heartbeat.tar.bz2 heartbeat 开始创建软件包 [root@master heartbeat]# rpmbuild -ba heartbeat-fedora.spec error: Failed build dependencies: libtool-ltdl-devel is needed by heartbeat-3.0.6-1.el6.x86_64 cluster-glue-libs-devel is needed by heartbeat-3.0.6-1.el6.x86_64 解决依赖 [root@master heartbeat]# yum install -y ncurses-devel openssl-devel gettext bison flex mailx cluster-glue-libs-devel docbool-dtds docbook-style-xsl libtook-ltdl-devel libuuid-devel [root@master heartbeat]# rpmbuild -ba heartbeat-fedora.spec ........... Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.VvZNu1 + umask 022 + cd /root/rpmbuild/BUILD + cd heartbeat + rm -rf /root/rpmbuild/BUILDROOT/heartbeat-3.0.6-1.el6.x86_64 + exit 0
在WEB主服务器上
[root@master heartbeat]# cd /root/rpmbuild/RPMS/x86_64/ [root@master x86_64]# ls heartbeat-3.0.6-1.el6.x86_64.rpm heartbeat-devel-3.0.6-1.el6.x86_64.rpm heartbeat-debuginfo-3.0.6-1.el6.x86_64.rpm heartbeat-libs-3.0.6-1.el6.x86_64.rpm [root@master x86_64]# yum install -y cluster-glue resource-agents [root@master x86_64]# rpm -ivh heartbeat-libs-3.0.6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat-libs ########################################### [100%] [root@master x86_64]# rpm -ivh heartbeat-3.0.6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat ########################################### [100%] [root@master x86_64]# scp heartbeat-libs-3.0.6-1.el6.x86_64.rpm heartbeat-3.0.6-1.el6.x86_64.rpm node-2:/root/ root@node-2's password: heartbeat-libs-3.0.6-1.el6.x86_64.rpm 100% 76KB 75.6KB/s 00:00 heartbeat-3.0.6-1.el6.x86_64.rpm 100% 234KB 233.5KB/s 00:00
从WEB服务器
[root@node-2 ~]# yum install -y ncurses-devel openssl-devel gettext bison mailx cluster-glue-libs-devel docbook-dtds docbook-style-xsl cluster-glue resource-agents #前提是/etc/yum.repo文件需要修改与master一样 [root@node-2 ~]# rpm -ivh heartbeat-libs-3.0.6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat-libs ########################################### [100%] [root@node-2 ~]# rpm -ivh heartbeat-3.0.6-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat ########################################### [100%] [root@node-2 ~]# grep haclient /etc/group haclient:x:492: [root@node-2 ~]# id hacluster uid=496(hacluster) gid=492(haclient) groups=492(haclient)
配置heartbea,在master上
定义验证文件
[root@master ~]# cp /usr/share/doc/heartbeat-3.0.6/ha.cf /etc/ha.d/ [root@master ~]# cp /usr/share/doc/heartbeat-3.0.6/authkeys /etc/ha.d/ [root@master ~]# cp /usr/share/doc/heartbeat-3.0.6/haresources /etc/ha.d/ [root@master ~]# vim /etc/ha.d/authkeys #设置主备节点之间通信使用的验证文件 auth 3 #去掉原有的注释,auth后面的数字对应下面的序号,使用crc认证的话,auth后面就写1,使用md5认证的话,auth后面就写3 #1 crc #2 sha1 HI! 3 md5 sishenkey #去掉原有注释并修改 [root@master ~]# chmod 600 /etc/ha.d/authkeys #不改权限的话不能启动
定义浮动资源
[root@master ~]# vim /etc/ha.d/haresources #node-name resource1 resource2 ... resourceN #此行下面添加 master IPaddr::192.168.30.130/24/eth0 Filesystem::192.168.30.131:/nfsdata::/var/www /html/::nfs httpd #node-name 主服务器主机名,从服务器上不要修改这里 #IPaddr::192.168.30.130/24/eth0 #定义VIP及绑定到哪张网卡上 #Filesystem::192.168.30.131:/nfsdata::/var/www/html/ 定义要挂载的存储 #httpd 指定要启动的服务,这个服务必须是/etc/init.d/下面的,可以通过service启动或关闭的
手动测试浮动IP是否可用
[root@master ha.d]# /etc/ha.d/resource.d/IPaddr 192.168.30.100/24/eth0 start INFO: Adding inet address 192.168.30.100/24 with broadcast address 192.168.30.255 to device eth0 INFO: Bringing device eth0 up INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.30.100 eth0 192.168.30.100 auto not_used not_used INFO: Success INFO: Success ARPING 192.168.30.100 from 192.168.30.100 eth0 [root@master ha.d]# Sent 5 probes (5 broadcast(s)) Received 0 response(s) 使用ip addr查看,ifconfig看不到 [root@master ha.d]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4d:47:50 brd ff:ff:ff:ff:ff:ff inet 192.168.30.130/24 brd 192.168.30.255 scope global eth0 inet 192.168.30.100/24 brd 192.168.30.255 scope global secondary eth0 inet6 fe80::20c:29ff:fe4d:4750/64 scope link valid_lft forever preferred_lft forever
测试文件系统是否正常
[root@master ha.d]# /etc/ha.d/resource.d/Filesystem 192.168.30.131:/nfsdata /var/www/html/ nfs start INFO: Running start for 192.168.30.131:/nfsdata on /var/www/html INFO: Filesystem /var/www/html is already mounted. INFO: Success INFO: Success [root@master ha.d]# ll /var/www/html/ total 4 -rw-r--r-- 1 root root 48 Jun 5 16:48 index.html #结果表明是成功的
查看主配置文件
[root@master ~]# grep "^[^#]" /etc/ha.d/ha.cf #查看配置文件的有效行有哪些 logfacility local0 auto_failback on #auto_failback 为on时,主节点恢复正常,资源自动转给主节点,建议设为off,等主节点恢复正常后且业务不繁忙后再切回来,防止主节点恢复正常时,因为回切导致的异常。
修改主配置文件
[root@master ~]# vim /etc/ha.d/ha.cf #以下行去掉原有注释,启用 24 debugfile /var/log/ha-debug 29 logfile /var/log/ha-log 48 keepalive 2 #指定heartbeat之间的时间间隔2秒 56 deadtime 30 #在30秒后判断节点死亡 61 warntime 10 #在日志中发出“late heartbeat”警告之前等待的时间,单位:秒 71 initdead 120 #在一些系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔,取值至少为deadtime的两倍 76 udpport 694 #使用端口694惊醒bcast和ucast通信(默认值:694,IANA官方注册的端口号) 121 ucast eth0 192.168.30.132 #表示从本机的eth0接口发出的心跳消息给对方节点,写对方IP,这是单播地址。node-2上要写192.168.30.130,心跳网卡:加入有两张网卡,可以写成eth1。注意,配置文件中91行#bcast eth0 表示在eth0接口上使用广播heartbeat(将eth1替换为eth0,eth2,或者用户使用的任何接口) 157 auto_failback on #默认启用 211 node master #必须配置,集群中机器的主机名 212 node node-2 220 ping 192.168.30.2 #通过ping命令实现仲裁,这里需要注意每个人的网关不同,依据自己的实际网关而定 253 respawn hacluster /usr/libexec/heartbeat/ipfail #注意这里需要手动去看ipfail是在lib下面,还是在libexec下 259 apiauth ipfail gid=haclient uid=hacluster
从WEB服务器上(node-2)
[root@master ~]# cd /etc/ha.d/ [root@master ha.d]# scp authkeys ha.cf haresources node-2:/etc/ha.d/ root@node-2's password: authkeys 100% 643 0.6KB/s 00:00 ha.cf 100% 10KB 10.3KB/s 00:00 haresources 100% 6006 5.9KB/s 00:00 [root@node-2 ~]# chmod 600 /etc/ha.d/authkeys [root@node-2 ~]# vim /etc/ha.d/ha.cf ucast eth0 192.168.30.130 #只需修改这里
启动heartbeat(在WEB主和从上)
[root@master ~]# /etc/init.d/heartbeat restart Stopping High-Availability services: [ OK ] Waiting to allow resource takeover to complete: [ OK ] Starting High-Availability services: INFO: Running OK CRITICAL: Resource IPaddr::192.168.30.100/24/eth0 is active, and should not be! CRITICAL: Non-idle resources can affect data integrity! info: If you don't know what this means, then get help! info: Read the docs and/or source to /usr/share/heartbeat/ResourceManager for more details. CRITICAL: Resource IPaddr::192.168.30.100/24/eth0 is active, and should not be! CRITICAL: Non-idle resources can affect data integrity! info: If you don't know what this means, then get help! info: Read the docs and/or the source to /usr/share/heartbeat/ResourceManager for more details. CRITICAL: Non-idle resources will affect resource takeback! CRITICAL: Non-idle resources may affect data integrity! [ OK ] [root@node-2 ~]# /etc/init.d/heartbeat restart Stopping High-Availability services: [ OK ] Waiting to allow resource takeover to complete: [ OK ] Starting High-Availability services: INFO: Resource is stopped [ OK ] #注意这里在启动的时候等待大概2分钟左右master提示在接管所有浮动资源.等下启动成功后,会释放浮动资源,最后再次加载
验证端口是否监听成功
[root@master ~]# netstat -antup | grep 694 udp 0 0 0.0.0.0:694 0.0.0.0:* 3055/heartbeat: wri [root@node-2 ~]# netstat -antup | grep 694 udp 0 0 0.0.0.0:694 0.0.0.0:* 3514/heartbeat: wri
Ok,端口正常!
查看浮动IP是否存在
[root@master ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4d:47:50 brd ff:ff:ff:ff:ff:ff inet 192.168.30.130/24 brd 192.168.30.255 scope global eth0 inet 192.168.30.100/24 brd 192.168.30.255 scope global secondary eth0 inet6 fe80::20c:29ff:fe4d:4750/64 scope link valid_lft forever preferred_lft forever [root@node-2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:2f:84:4d brd ff:ff:ff:ff:ff:ff inet 192.168.30.132/24 brd 192.168.30.255 scope global eth0 inet6 fe80::250:56ff:fe2f:844d/64 scope link valid_lft forever preferred_lft forever
查看浮动资源是否挂载
[root@master ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_master-LogVol00 18G 4.1G 13G 25% / tmpfs 2.0G 72K 2.0G 1% /dev/shm /dev/sda1 194M 35M 150M 19% /boot /dev/sr0 3.6G 3.6G 0 100% /media/cdrom 192.168.30.131:/nfsdata 18G 4.1G 13G 25% /var/www/html
浏览器测试浮动IP
手动停掉master上的eth0,再次测试浮动IP
[root@master ~]# ifdown eth0 然后去node-2上查看 [root@node-2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:2f:84:4d brd ff:ff:ff:ff:ff:ff inet 192.168.30.132/24 brd 192.168.30.255 scope global eth0 inet 192.168.30.100/24 brd 192.168.30.255 scope global secondary eth0 inet6 fe80::250:56ff:fe2f:844d/64 scope link valid_lft forever preferred_lft forever #node-2已经接管浮动IP
还是可以的!OK~到此结束