实验简介
本实验采用centos6为基础操作系统,通过heartbeat v1 版本来实现httpd服务的高可用。
实验架构
- active:192.168.99.41 node3.magedu.com
- passive:192.168.99.24 node4.magedu.com
- Floting IP:192.168.99.200
配置HA集群的前提
- 节点间时间必须同步
ntpdate
[root@node3 ~]# ntpdate 0.centos.pool.ntp.org
[root@node4 ~]# ntpdate 0.centos.pool.ntp.org
- 节点间需要通过主机名互信,必须解析IP地址
- 建议通过hosts文件来实现
- 通信中使用的名字与节点的名字必须保持一致:
uname -n
命令或hoatname
# hostname
node3.magedu.com
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.0.1 server.magelinux.com server
192.168.99.41 node3.magedu.com node3
192.168.99.42 node4.magedu.com node4
-
考虑仲裁设备是否会被用到
因为我们采用两节点必须使用仲裁设备,此处我们采用ping node
来实现仲裁 -
建立各节点之间的root用户能够基于密钥认证
[root@node3 ~]# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
9a:52:eb:68:6a:a1:80:fa:c6:ec:db:4a:48:e7:0e:f4 root@node3.magedu.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
|.o . . S|
|=.= . + |
|+=.E. + |
|o.B..+ |
| =**o . |
+-----------------+
[root@node3 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@node4.magedu.com
[root@node3 ~]# date ;ssh node4.magedu.com 'date'
Mon Feb 6 10:21:35 CST 2017
Mon Feb 6 10:21:35 CST 2017
至此准备工作完成我们应该开始正式的安装部署了
解决依赖关系与安装heartbeat程序包
#注意需要配置好yum仓库和epel仓库
[root@node3 ~]# yum install -y net-snmp-libs libnet PyXML
[root@node3 ~]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:heartbeat-pils ########################################### [ 33%]
2:heartbeat-stonith ########################################### [ 67%]
3:heartbeat ########################################### [100%]
#这里的rpm包使我们自己配置安装的
复制配置文件并修改相应的权限
[root@node3 ~]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
[root@node3 ~]# chmod 600 /etc/ha.d/authkeys #必须修改为600,否则服务有可能无法启动
[root@node3 ~]# ll /etc/ha.d/
total 48
-rw------- 1 root root 645 Feb 6 10:43 authkeys
-rw-r--r-- 1 root root 10539 Feb 6 10:43 ha.cf
-rwxr-xr-x 1 root root 745 Sep 10 2013 harc
-rw-r--r-- 1 root root 5905 Feb 6 10:43 haresources
drwxr-xr-x 2 root root 4096 Feb 6 10:36 rc.d
-rw-r--r-- 1 root root 692 Sep 10 2013 README.config
drwxr-xr-x 2 root root 4096 Feb 6 10:36 resource.d
-rw-r--r-- 1 root root 7864 Sep 10 2013 shellfuncs
- 主配置配置
vim /etc/ha.d/ha.cf
定义heartbeat守护进程的工作方式和属性
logfile /var/log/ha-log
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
mcast eth0 225.12.0.1 694 1 0
auto_failback on
node node3.magedu.com
node node4.magedu.com
ping 192.168.99.1
compression bz2
compression_threshold 2
- 集群资源配置resources
通过此配置文件来配置资源
[root@node3 ~]# vim /etc/ha.d/haresources
node3.magedu.com 192.168.99.200/24/eth0/192.168.99.255 httpd
- 密钥认证文件配置authkeys
通过此来设置集群内多个节点的认证
[root@node3 ~]# openssl rand -base64 16
duPgfWr4O5SgHlUbNgCNtA==
[root@node3 ~]# vim /etc/ha.d/authkeys
auth 2
#1 crc
2 sha1 duPgfWr4O5SgHlUbNgCNtA
以上配置完成后我们将其复制到另外一台节点上,快速数显heatbeat的安装配置
[root@node3 ~]# scp -r heartbeat2/ node4.magedu.com:/root
[root@node4 ~]# rpm -ivh heartbeat2/heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat2/heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat2/heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
[root@node3 ~]# scp -p /etc/ha.d/{ha.cf,haresources,authkeys} node4.magedu.com:/etc/ha.d/
安装httpd服务,并禁止开机自启
# yum install -y httpd
提供不同的页面
[root@node3 ~]# echo "<h1>node3.magedu.com</h1>" > /var/www/html/index.html
[root@node4 ~]# echo "<h1>node4.magedu.com</h1>" > /var/www/html/index.html
启动服务
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl 192.168.99.41
<h1>node3.magedu.com</h1>
[root@node3 ~]# service httpd stop
Stopping httpd: [ OK ]
[root@node3 ~]# chkconfig httpd off
[root@node3 ~]# service heartbeat start
logd is already running
Starting High-Availability services:
2017/02/06_13:46:17 INFO: Resource is stopped
Done.
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl 192.168.99.41
<h1>node3.magedu.com</h1>
[root@node3 ~]# service httpd stop
Stopping httpd: [ OK ]
[root@node3 ~]# chkconfig httpd off
[root@node4 ~]# service heartbeat start
logd is already running
Starting High-Availability services:
2017/02/06_13:46:17 INFO: Resource is stopped
Done.
注意:需要两台机器同时启动heartbeat
才能看到效果
效果检验
1.两台主机的heartbeat服务都启动,我们用curl请求会产生以下效果:
[root@client ~]# curl 192.168.99.200
<h1>node3.magedu.com</h1>
此时因为主节点的为node3.magedu.com
,所以我们访问到的是node3.magedu.com主机提供的页面
2.关闭主节点上的
heartbeat
服务,我们会发现haresources
配置的文件全都跑到备用节点上了
[root@node3 ~]# service heartbeat stop
Stopping High-Availability services:
Done.
[root@node3 ~]#
[root@client ~]# curl 192.168.99.200
<h1>node4.magedu.com</h1>
[root@client ~]#
通过上述示例我们达到了双机主/备用,资源快速切换的效果
3.我们重新让主节点上线,它会将资源抢回来,这是我们在
ha.cf
文件中配置的
[root@node3 ~]# service heartbeat start
[root@client ~]# curl 192.168.99.200
<h1>node3.magedu.com</h1>
- tip:温馨小提示
在
/usr/lib64/heartbeat/
目录下有一些小程序,我们可以用来对heartbeat高可用集群进行手动实现hb_standby ,hb_takeover,ha_propagate...
借用共享存储来实现资源转移
准备第三个节点用来充当NFS共享服务器,此处作者用centos7主机来充当nfs服务。
[root@nfs ~]# mkdir /web/htdocs -pv
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/htdocs’
[root@nfs ~]# echo "<h1>Page from NFS</h1>" > /web/htdocs/index.html
[root@nfs ~]# vim /etc/exports
[root@nfs ~]# systemctl start nfs
分别挂载测试是否成功
#节点1:node3.magedu.com
[root@node3 ~]# mount -t nfs 192.168.99.241:/web/htdocs /var/www/html/
[root@node3 ~]# service httpd start
Starting httpd: [ OK ]
[root@node3 ~]# curl node3.magedu.com
<h1>Page from NFS</h1>
[root@node3 ~]# service httpd stop
[root@node3 ~]# umount /var/www/html/
# 节点2:node4.magedu.com
[root@node4 ~]# mount -t nfs 192.168.99.241:/web/htdocs /var/www/html/
[root@node4 ~]# service httpd start
[root@node4 ~]# curl 192.168.99.42
<h1>Page from NFS</h1>
[root@node4 ~]# service httpd stop
[root@node4 ~]# umount /var/www/html/
至此,nfs服务已经准备ok。我们可以配置1heartbeat来进行挂载使用测试了
- 1.编辑harources添加nfs资源
[root@node3 ~]# vim /etc/ha.d/haresources
node3.magedu.com 192.168.99.200/24/eth0/192.168.99.255 Filesystem::192.168.99.241:/web/htdocs::/var/www/html::nfs httpd
[root@node4 ~]# vim /etc/ha.d/haresources
node4.magedu.com 192.168.99.200/24/eth0/192.168.99.255 Filesystem::192.168.99.241:/web/htdocs::/var/www/html::nfs httpd
- 2.启动服务
[root@node3 ~]# service heartbeat start;ssh node4.magedu.com 'service heartbeat start'
- 3.验证结果:
# 文件已经挂载到node3.magedu.com这个节点上去了
[root@node3 ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.99.241:/web/htdocs on /var/www/html type nfs (rw,vers=4,addr=192.168.99.241,clientaddr=192.168.99.41)
#请求weeb服务正常是nfs提供的页面
[root@client ~]# curl 192.168.99.200
<h1>Page from NFS</h1>
# 停止node3.magedu.com节点,资源将转移到node4.magedu.com上,且用户不会犹豫任何察觉
[root@node3 ~]# /usr/lib64/heartbeat/hb_standby
#nfs挂载到了node4节点上
[root@node4 ~]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.99.241:/web/htdocs on /var/www/html type nfs (rw,vers=4,addr=192.168.99.241,clientaddr=192.168.99.42)
#web服务仍然能访问
[root@client ~]# curl 192.168.99.200
<h1>Page from NFS</h1>
网友评论