(一) 网络架构
两台CentOS6.5
nfsmaster:heartbeatmaster:C1 eth1: 192.168.1.111 (和外网通信提供文件服务) eth3(192.168.4.10 heartbeat心跳监控)
nfsslave:heartbeatslave:C2 eth2: 192.168.1.113 (和外网通信提供文件服务) eth3(192.168.4.20 heartbeat心跳监控)
(二) 配置时间同步 (两台服务器同时执行)
1)配置时区
1
2
[iyunv@localhost yum.repos.d]# rm -f /etc/localtime
[iyunv@localhost yum.repos.d]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2)配置时间同步
1
2
[iyunv@localhost network-scripts]# ntpdate pool.ntp.org
29 Dec 15:40:14 ntpdate[3232]: adjust time server 202.112.29.82 offset 0.033253 sec
(三) host文件配置(两台服务器配置一样)
1
2
3
4
5
[iyunv@localhost network-scripts]# vi /etc/hosts (添加下面两行)
192.168.4.10 heartbeatmaster
192.168.4.20 heartbeatslave
192.168.1.111 C1.centos.sx
192.168.1.113 C2.centos.sx
修改hostname (C1上修改为HOSTNAME=C1.centos.sx,C2上修改为HOSTNAME=C2.centos.sx)
1
2
3
4
5
6
7
[iyunv@localhost ha.d]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=C1.centos.sx
GATEWAY=192.168.1.1
NETWORKING_IPV6=off
临时修改
hostname C1.centos.sx
(四) 添加两条路由规则,使心跳检测和数据同步不受干扰
1
2
3
4
5
6
7
C1端:
[iyunv@localhost network-scripts]# route add -host 192.168.4.20 dev eth3
开机启动时添加这条规则
[iyunv@localhost network-scripts]# echo '/sbin/route add -host 192.168.4.20 dev eth3' >> /etc/rc.local
C2端:
[iyunv@localhost network-scripts]# route add -host 192.168.4.10 dev eth3
[iyunv@localhost network-scripts]# echo '/sbin/route add -host 192.168.4.10 dev eth3' >> /etc/rc.local
(五) 部署heartbeat(只演示C1端,C2端同样)
C1端:
1) 增加epel源 官方yum源上没有heartbeat
2) yum安装heartbeat
1
[iyunv@localhost yum.repos.d]# yum -y install heartbeat
3) 修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@localhost yum.repos.d]# cd /usr/share/doc/heartbeat-3.0.4/
[iyunv@localhost heartbeat-3.0.4]# cp ha.cf authkeys haresources /etc/ha.d/
[iyunv@localhost ha.d]# vi ha.cf #(C1,C2配置相同)
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 10
warntime 6
#initdead 120
udpport 694
#bcast eth3
mcast eth3 239.0.0.192 694 1 0 # 心跳监控时使用的网卡、组播地址、和端口
auto_failback on
respawn hacluster /usr/lib64/heartbeat/ipfail #(C1,C2配置相同)
node C1.centos.sx # 主节点
node C2.centos.sx # 从节点
ping 192.168.1.1 # (对外网关的ip地址,主要用来测试当前节点是否可以对外网络是否正常。作为是否要主从切换的一个条件)
1
2
3
4
[iyunv@localhost ha.d]# vi authkeys # C1,C2配置相同
auth 1
1 crc
[iyunv@localhost ha.d]# chmod 600 authkeys #该文件必须设置为600权限,不然heartbeat启动会报错
C1:
1
2
[iyunv@localhost ha.d]# vi haresources
C1.centos.sx IPaddr::192.168.1.119/24/eth1 # 这里的C1.centos.sx代表主节点C1,C2配置相同,主节点都为C1。这里的ip是对外提供的浮动ip的地址 (这里C1,C2浮动ip必须写成一样 网卡则根据具体来定 在C2中我写成了eth2)
C2:
1
2
[iyunv@localhost ha.d]# vi haresources
C1.centos.sx IPaddr::192.168.1.119/24/eth2
## 如果配置成上面这样则heartbeat是根据heartbeat这项服务来做主从切换 如果C1宕机或者C1的heartbeat关闭则对外的浮动ip则被C2占用
#(该文件列出集群所提供的服务以及服务的默认所有者。 注意:两个集群节点上的该文件必须相同。集群的IP地址是该选项是必须配置的,不能在haresources文件以外配置该地址, haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址以及启动的服务等。其配置语句格式如下:
node-name network-config
#其中node-name指定双机系统的主节点,取值必须匹配ha.cf文件中node选项设置的主机名中的一个,node选项设置的另一个主机名成为从节点。network-config用于网络设置,包括指定集群IP、子网掩码、广播地址等。resource-group用于设置heartbeat启动的服务,该服务最终由双机系统通过集群IP对外提供。在本文末中我们假设要配置的HA服务为nginx和apache。 )
(六) 启动heartbeat (C1,C2执行相同)
1
2
[iyunv@localhost ha.d]# service heartbeat start # 启动
[iyunv@localhost ha.d]# chkconfig heartbeat on # 设置开机时启动
(七) 测试heartbeat启动停止时浮动ip资源的切换
1) 正常状态下
C1:
1
2
3
4
[iyunv@localhost ha.d]# ip a | grep eth1
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.111/24 brd 192.168.1.255 scope global eth1
inet 192.168.1.119/24 brd 192.168.1.255 scope global secondary eth1
C2:
1
2
3
4
[iyunv@C2 ha.d]# ip a | grep eth2
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.113/24 brd 192.168.1.255 scope global eth2
# 此时浮动ip(192.168.1.119)属于C1
2) 关闭C1上的heartbeat
C1:
1
2
3
[iyunv@localhost ha.d]# ip a | grep eth1
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.111/24 brd 192.168.1.255 scope global eth1
C2:
1
2
3
4
[iyunv@C2 ha.d]# ip a | grep eth2
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.113/24 brd 192.168.1.255 scope global eth2
inet 192.168.1.119/24 brd 192.168.1.255 scope global secondary eth2
# 此时浮动ip属于C2
(八) 测试heartbeat启动停止时控制其他服务(这里是nginx apache)的启动或停止
C1:
1
2
3
4
[iyunv@C1 ha.d]# vi haresources (更改heartbeat的资源定义文件)
C1.centos.sx 192.168.1.119 nginx httpd
[iyunv@C1 ha.d]# ln -s /etc/rc.d/init.d/nginx /etc/ha.d/resource.d/
[iyunv@C1 ha.d]# ln -s /etc/rc.d/init.d/httpd /etc/ha.d/resource.d/
C2:
1
2
3
4
[iyunv@C2 ha.d]# vi haresources (更改heartbeat的资源定义文件)
C1.centos.sx 192.168.1.119 nginx httpd
[iyunv@C2 ha.d]# ln -s /etc/rc.d/init.d/nginx /etc/ha.d/resource.d/
[iyunv@C2 ha.d]# ln -s /etc/rc.d/init.d/httpd /etc/ha.d/resource.d/
C1:
1
[iyunv@C1 ha.d]# service heartbeat restart
C2:
1
[iyunv@C2 ha.d]# service heartbeat restart
默认的状态下:
C1:
1
2
3
4
[iyunv@C1 ha.d]# service httpd status
httpd (pid 1219) is running...
[iyunv@C1 ha.d]# service nginx status
nginx (pid 19215) is running...
C2:
1
2
3
4
[iyunv@C2 ha.d]# service nginx status
nginx is stopped
[iyunv@C2 ha.d]# service httpd status
httpd is stopped
停止C1上的heartbeat服务
C1:
1
[iyunv@C1 ha.d]# service heartbeat stop
再次查看状态
C1:
1
2
3
4
[iyunv@C1 ha.d]# service nginx status
nginx is stopped
[iyunv@C1 ha.d]# service httpd status
httpd is stopped
C2:
1
2
3
4
[iyunv@C2 ha.d]# service nginx status
nginx (pid 14433) is running...
[iyunv@C2 ha.d]# service httpd status
httpd (pid 14469) is running...
说明实验已经成功
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com