一.配置环境
HA1:192.168.85.144 eth1
HA2:192.168.85.145 eth1
NFS:192.168.85.143
VIP:192.168.85.128
1.配置节点主机IP并保证双发能通信
HA1 ping HA2:
[root@localhost ~]# ping 192.168.85.145 -c 1
PING 192.168.85.145 (192.168.85.145) 56(84) bytes of data.
64 bytes from 192.168.85.145: icmp_seq=1 ttl=64 time=3.09 ms
HA2 ping HA1:
[root@localhost ~]# ping 192.168.85.144 -c 1
PING 192.168.85.144 (192.168.85.144) 56(84) bytes of data.
64 bytes from 192.168.85.144: icmp_seq=1 ttl=64 time=0.764 ms
2.配置主机名保证和uname -n相同
HA1:
[root@localhost ~]# hostname node1.a.com
[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.a.com
[root@localhost ~]# uname -n
node1.a.com
HA2:
[root@localhost ~]# hostname node2.a.com
[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node2.a.com
[root@localhost ~]# uname -n
node2.a.com
3.编辑/etc/hosts文件使两台主机能够互相解析
HA1和HA2的/etc/hosts文件中都添加两行:
192.168.85.144 node1.a.com node1
192.168.85.145 node2.a.com node2
[root@node2 ~]# ping node1.a.com -c 1
PING node1.a.com (192.168.85.144) 56(84) bytes of data.
64 bytes from node1.a.com (192.168.85.144): icmp_seq=1 ttl=64 time=0.848 ms
[root@node1 ~]# ping node1.a.com -c 1
PING node1.a.com (192.168.85.144) 56(84) bytes of data.
64 bytes from node1.a.com (192.168.85.144): icmp_seq=1 ttl=64 time=1.49 ms
4.双机互信
HA1:
[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2.a.com
[root@node1 ~]# ssh root@node2.a.com 'hostname'
node2.a.com
HA2:
[root@node2 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
[root@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1.a.com
[root@node2 ~]# ssh root@node1.a.com 'hostname'
node1.a.com
5.时间同步
[root@node1 ~]# vim /etc/sysconfig/clock
ZONE="Asia/Shanghai"
[root@node1 ~]# rm /etc/localtime
[root@node1 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
二.安装相关软件
HA1和HA2:
1.安装perl-MailTools
[root@node1 ~]# yum --nogpgcheck localinstall perl-MailTools-2.04-4.el6.noarch.rpm
2.安装heartbeat
[root@node1 ~]# ls
heartbeat-3.0.4-2.el6.i686.rpm libnet-1.1.6-7.el6.i686.rpm
heartbeat-libs-3.0.4-2.el6.i686.rpm perl-MailTools-2.04-4.el6.noarch.rpm
[root@node1 ~]# yum --nogpgcheck localinstall heartbeat-3.0.4-2.el6.i686.rpm libnet-1.1.6-7.el6.i686.rpm heartbeat-libs-3.0.4-2.el6.i686.rpm
三.启动heartbeat
1.为heartbeat提供配置文件
配置密钥文件,heartbeat服务的配置文件和资源管理配置文件
[root@node1 ha.d]# cp /usr/share/doc/heartbeat-3.0.4/{ha.cf,authkeys,haresources} ./
[root@node1 ha.d]# ls
authkeys ha.cf harc haresources rc.d README.config resource.d shellfuncs
2.编辑authkeys文件(写法可以打开该文件查看,有具体解释,并且权限要设置为600启动heartbeat会出错)
[root@node1 ha.d]# vim authkeys
auth 1
1 md5 redhat
3.编辑主配置文件ha.cf
[root@node1 ha.d]# sed '/^#/d' ha.cf
logfile /var/log/ha-log
keepalive 2
deadtime 8
warntime 3
udpport 694
bcast eth1
auto_failback on
node node1.a.com
node node2.a.com
ping 192.168.85.128
4.haresources文件介绍
其中,每一行都是一个集群服务类型,写法有多种,具体可以看该文件内容;
如格式:node1 10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
node1为启动时处于活动状态的HA,和uname -n保持一致;
10.0.0.170为VIP,为多个时用空格隔开;
Filesystem::/dev/sda1::/data1::ext2中:(各字段用::隔开)
Filesystem表示资源类型,可在/etc/ha.d/resource.d中查看;
/dev/sda1表示挂载的分区;
data1表示挂载的目录;
ext2:表示文件系统类型;
5.编辑资源配置文件haresources添加
node1.a.com 192.168.85.128 httpd
6.HA1和HA2配置web服务
两个节点都安装httpd并提供测试页面,启动服务保证其web服务能正常访问,然后关闭web服务且禁止开机启动;
[root@node1 ha.d]# yum install httpd -y
[root@node1 ha.d]# cat /var/www/html/index.html
node1.a.com
[root@node1 ha.d]# service httpd start
[root@node2 ha.d]# curl
node1.a.com
[root@node1 ha.d]# service httpd stop
[root@node1 ha.d]# chkconfig httpd off
node2同node1
7.将HA1的配置拷贝到HA2上
[root@node1 ha.d]# scp -p authkeys ha.cf haresources root@node2.a.com:/etc/ha.d/
authkeys 100% 665 0.7KB/s 00:00
ha.cf 100% 10KB 10.3KB/s 00:00
haresources 100% 5957 5.8KB/s
8.启动heartbeat服务
[root@node1 ha.d]# service heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
[root@node1 ha.d]# ssh node2.a.com 'service heartbeat start'
Starting High-Availability services: 2015/10/26_20:22:39 INFO: Resource is stopped
Done.
四.测试
1.查看端口
HA1:
[root@node1 ha.d]# netstat -untlp | grep :694
udp 0 0 0.0.0.0:694 0.0.0.0:* 9525/heartbeat: wri
[root@node1 ha.d]# netstat -untlp | grep :80
tcp 0 0 :::80 :::* LISTEN 9915/httpd
HA2:
[root@node2 ha.d]# netstat -untlp | grep :694
udp 0 0 0.0.0.0:694 0.0.0.0:* 9535/heartbeat: wri
[root@node2 ha.d]# netstat -untlp | grep :80
2.查看VIP浮动情况
[root@node1 ha.d]# ip addr show
1: lo: 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: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:97:1c:35 brd ff:ff:ff:ff:ff:ff
inet 192.168.85.144/24 brd 192.168.85.255 scope global eth1
inet 192.168.85.128/24 brd 192.168.85.255 scope global secondary eth1
inet6 fe80::20c:29ff:fe97:1c35/64 scope link
valid_lft forever preferred_lft forever
[root@node2 ha.d]# ip -o -f inet addr show
1: lo inet 127.0.0.1/8 scope host lo
2: eth1 inet 192.168.85.145/24 brd 192.168.85.255 scope global eth1
3.关闭HA1的heartbeat后:
[root@node1 ha.d]# service heartbeat stop
Stopping High-Availability services: Done.
[root@node1 ha.d]# ip -o -f inet addr show
1: lo inet 127.0.0.1/8 scope host lo
2: eth1 inet 192.168.85.144/24 brd 192.168.85.255 scope global eth1
4.此时的HA2:
[root@node2 ha.d]# service heartbeat status
heartbeat OK [pid 8816 et al] is running on node2.a.com [node2.a.com]...
[root@node2 ha.d]# ip -o -f inet addr show
1: lo inet 127.0.0.1/8 scope host lo
2: eth1 inet 192.168.85.145/24 brd 192.168.85.255 scope global eth1
2: eth1 inet 192.168.85.128/24 brd 192.168.85.255 scope global secondary eth1
[root@node2 ha.d]# netstat -untlp | grep :80
tcp 0 0 :::80 :::* LISTEN 9872/httpd
也可以用浏览器测试,具体为:两个节点都开始heartbeat,浏览器测试此时的web服务再节点node1上,然后关闭该节点上的heartbeat,在浏览器测试,web服务在node2上;
补充:
昨天写博客的时候因为128主机有些问题无法用网页打开,今天在实验室又做了一次,补回来:
配置过程一样,过程也没有问题,只不过这次IP地址变了:
HA1:192.168.85.144--->192.168.140.131 eth1
HA2:192.168.85.145--->192.168.142.132 eth1
VIP:192.168.85.158 --->192.168.142.130
两个节点都开启heartbeat,此时浏览器访问192.168.142.130
关闭节点1上的heartbeat,浏览器访问192.168.142.130
花了点时间重装了一次centos IP地址也配好了,终于和第一次保持一致了;
五.配置NFS服务使访问不同节点时的web页面保持一致(新加一台192.168.85.143的主机用作NFS服务器)
1.安装NFS-utils并在NFS服务主机上创建web页面存放目录
[root@nfs ~]# yum install nfs-utils
[root@nfs ~]# mkdir -pv /web/htmls
mkdir: created directory `/web'
mkdir: created directory `/web/htmls'
2.定义共享目录
[root@nfs~]# vim /etc/exports
/web/htmls 192.168.85.0/255.255.255.0(ro)
3.保证NFS服务主机和两节点主机互通
[root@nfs ~]# ping 192.168.85.144 -c 1
PING 192.168.85.144 (192.168.85.144) 56(84) bytes of data.
64 bytes from 192.168.85.144: icmp_seq=1 ttl=64 time=8.59 ms
[root@nfs ~]# ping 192.168.85.145 -c 1
PING 192.168.85.145 (192.168.85.145) 56(84) bytes of data.
64 bytes from 192.168.85.145: icmp_seq=1 ttl=64 time=9.75 ms
4.提供一个测试文件并启动NFS服务
[root@nfs ~]# vim /web/htmls/index.html
NFS Server!!!
[root@nfs ~]# service rpcbind start
Starting rpcbind: [ OK ]
[root@nfs ~]# service rpcidmapd start
Starting RPC idmapd: [ OK ]
[root@nfs ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
5.查看是否已共享
[root@
nfs ~]# showmount -e 192.168.85.143
Export list for 192.168.85.143:
/web/htmls 192.168.85.0/255.255.255.0
[root@node1 ha.d]# showmount -e 192.168.85.143
Export list for 192.168.85.143:
/web/htmls 192.168.85.0/255.255.255.0
[root@node2 ha.d]# showmount -e 192.168.85.143
Export list for 192.168.85.143:
/web/htmls 192.168.85.0/255.255.255.0
6.更改资源配置(此时先停止两个节点的heartbeat)
6.1可以先在node1上挂载测试,如果没问题就继续,有问题说明NFS服务配置错误:
[root@node1 ~]# mount 192.168.85.143:/web/htmls /media/
[root@node1 ~]# ls /media/
index.html
6.2编辑node1的heart资源文件:
[root@node1 ha.d]# vim haresources
node1.a.com 192.168.85.128 Filesystem::192.168.85.143:/web/htmls::/var/www/html::nfs httpd
6.3将node1的haresources文件拷贝到node2上:
[root@node1 ha.d]# scp -p haresources root@node2:/etc/ha.d/
haresources 100% 5993 5.9KB/s 00:00
7.启动两个节点上的heartbeat服务
[root@node1 ~]# service heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
[root@node1 ~]# ssh node2 'service heartbeat start'
Starting High-Availability services: 2015/10/30_19:00:11 INFO: Resource is stopped
Done.
此时的端口状况:
node1节点:
[root@node1 ~]# netstat -untlp | grep :694
udp 0 0 0.0.0.0:694 0.0.0.0:* 29348/heartbeat: wr
[root@node1 ~]# netstat -untlp | grep :80
tcp 0 0 :::80 :::* LISTEN 29890/httpd
node2节点:
[root@node2 ~]# netstat -untlp | grep :694
udp 0 0 0.0.0.0:694 0.0.0.0:* 26449/heartbeat: wr
[root@node2 ~]# netstat -untlp | grep :80
8.测试
打开浏览器测试:
关闭node1的heartbeat服务:
[root@node1 ~]# service heartbeat stop
Stopping High-Availability services: Done.
[root@node1 ~]# netstat -untlp | grep :694
[root@node1 ~]# netstat -untlp | grep :80
node2节点:
[root@node2 ~]# netstat -untlp | grep :694
udp 0 0 0.0.0.0:694 0.0.0.0:* 27377/heartbeat: wr
[root@node2 ~]# netstat -untlp | grep :80
tcp 0 0 :::80 :::* LISTEN 27877/httpd
刷新浏览器显示:
就因为之前虚拟机出了点问题,让我把这个实验做了三次,还好做出来了,要不非郁闷死.....