1. 环境介绍
我这里弄了2个虚拟机,信息如下:
node1:192.168.168.201
node2:192.168.168.202
2.配置主机名
[root@node1 ~]# vim /etc/sysconfig/network #编辑节点1主机名配置文件
#编辑
HOSTNAME=node1.linuxpanda.com
[root@node1 ~]# hostname node1.linuxpanda.com #主机名立即生效 [root@node2 ~]# vim /etc/sysconfig/network #编辑节点2主机名配置文件
#编辑
HOSTNAME=node2.linuxpanda.com
[root@node2 ~]# hostname node2.linuxpanda.com #主机名立即生效
2.主机互信
具体可以参见我的上一篇博客:http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_023_sshgenkey.html
[root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #生成rsa
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.168.202 #复制公钥 [root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #生成rsa
[root@node2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.168.201 #复制公钥
[root@node1 ~]# ssh 192.168.168.202 'ip addr show dev eth0 ' #测试node1 ssh node2
[root@node2 ~]# ssh 192.168.168.201 'ip addr show dev eth0 ' #测试node2 ssh node1
3.host文件配置
host配置文件配置主要用于名称解析, 我们不建议使用dns解析,否则dns无法解析就会导致集群服务的崩溃。
[root@node1 ~]# vim /etc/hosts
#添加如下2行
192.168.168.201 node1.linuxpanda.com node1
192.168.168.202 node2.linuxpanda.com node2
[root@node1 ~]# scp /etc/hosts root@node2:/etc/hosts #host文件复制一份到node2中去
[root@node1 ~]# ping node2 -c 1 #node1 ping node2
[root@node2 ~]# ping node1 -c 1 #node2 ping node1
4.防火墙和selinux设置
[root@node1 ~]# service iptables stop #关闭node防火墙
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
[root@node1 ~]# ssh node2 'service iptables stop' #关闭node2防火墙
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
[root@node1 ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config #修改node1selinux设置
[root@node1 ~]# setenforce 0 #立马生效
[root@node1 ~]# ssh node2 "sed -i '/SELINUX/s/enforcing/disabled/' #修改node1selinux设置
[root@node1 ~]# ssh node2 " setenforce 0' #立马生效
5.时间同步
具体参见我的另一篇博客:http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_022_ntp.html
6.安装heartbeat
[root@node1 ~]# yum install heartbeat heartbeat-devel heartbeat-lib
[root@node2 ~]# ssh node2 'yum install heartbeat heartbeat-devel heartbeat-lib'
7.编辑配置文件
[root@node1 ha.d]# cd /etc/ha.d/
[root@node1 ha.d]# cp /usr/share/doc/heartbeat-3.0./{authkeys,ha.cf,haresources} -p .
[root@node1 ha.d]# vim authkeys
#添加以下两行
auth
md5 oracle
[root@node1 ha.d]# mv ha.cf ha.cf.bak
[root@node1 ha.d]# vim ha.cf
#添加如下行
logfile /var/log/ha-log
keepalive 2
deadtime 30
warntime 10
initdead 120
bcast eth0 # Linux
auto_failback on
node node1.linuxpanda.com
node node2.linuxpanda.com
ping 192.168.168.1
[root@node1 ha.d]# vim haresources
#添加如下行
node1.linuxpanda.com IPaddr::192.168.168.200//eth0 httpd
[root@node1 ha.d]# scp -p authkeys haresources ha.cf node2:/etc/ha.d/
8.安装httpd服务
[root@node1 ha.d]# yum install httpd
[root@node1 ha.d]# service httpd start
Starting httpd: [ OK ]
[root@node1 ha.d]# echo "node1" > /var/www/html/index.html
[root@node1 ha.d]# elinks node1 -source [root@node2 ha.d]# yum install httpd
[root@node2 ha.d]# service httpd start
Starting httpd: [ OK ]
[root@node2 ha.d]# echo "node2" > /var/www/html/index.html
[root@node2 ha.d]# elinks node2 -source
[root@node1 ha.d]# for i in {..};do ssh node$i 'service httpd stop';ssh node$i 'chkconfig httpd off' ; done;
9.启动heartbeat
[root@node1 ha.d]# for i in {..} ; do ssh node$i 'service heartbeat start' ; done;
10.测试
我们使用宿主机测试下:
[root@node1 ha.d]# ip addr show dev eth0 #确认node1的ip信息,发现200ip资源在node1上分配的。
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::c9:: brd ff:ff:ff:ff:ff:ff
inet 192.168.168.201/ brd 192.168.168.255 scope global eth0
inet 192.168.168.200/ brd 192.168.168.255 scope global secondary eth0
inet6 fe80::20c:29ff:fec9:/ scope link
valid_lft forever preferred_lft forever
[root@node2 ha.d]# ip addr show dev eth0 #查看node2的ip信息
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:3f:42:13 brd ff:ff:ff:ff:ff:ff
inet 192.168.168.202/24 brd 192.168.168.255 scope global eth0
inet6 fe80::250:56ff:fe3f:4213/64 scope link
valid_lft forever preferred_lft forever
我们使用hb_standby脚本将node1转为standby,当然也是可以断点处理。
[root@node1 heartbeat]# /usr/share/heartbeat/hb_standby
Going standby [all].
查看日志信息:
[root@node1 heartbeat]# tail - /var/log/ha-log May :: node1.linuxpanda.com heartbeat: []: info: node1.linuxpanda.com wants to go standby [all]
May :: node1.linuxpanda.com heartbeat: []: info: standby: node2.linuxpanda.com can take our all resources
May :: node1.linuxpanda.com heartbeat: []: info: give up all HA resources (standby).
ResourceManager(default)[]: //11_12:: info: Releasing resource group: node1.linuxpanda.com IPaddr::192.168.168.200//eth0 httpd
ResourceManager(default)[]: //11_12:: info: Running /etc/init.d/httpd stop
ResourceManager(default)[]: //11_12:: info: Running /etc/ha.d/resource.d/IPaddr 192.168.168.200//eth0 stop
IPaddr(IPaddr_192.168.168.)[]: //11_12:: INFO: IP status = ok, IP_CIP=
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.168.200)[9196]: 2017/05/11_12:39:46 INFO: Success
May :: node1.linuxpanda.com heartbeat: []: info: all HA resource release completed (standby).
May :: node1.linuxpanda.com heartbeat: []: info: Local standby process completed [all].
May :: node1.linuxpanda.com heartbeat: []: WARN: lost packet(s) for [node2.linuxpanda.com] [:]
May :: node1.linuxpanda.com heartbeat: []: info: remote resource transition completed.
May :: node1.linuxpanda.com heartbeat: []: info: No pkts missing from node2.linuxpanda.com!
May :: node1.linuxpanda.com heartbeat: []: info: Other node completed standby takeover of all resources.
测试成功。