a>:设置主机名和你的uname -n 是一样的
[root@i51 ~]# uname -n i511.tele.com [root@e3 ~]# uname -n e3.teleframe.cn
要和你的主机名一致
[root@i511 ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=i511.tele.com
2台一样
b>:同步时间 时间要一致
[root@i51 ~]# date ;ssh 192.168.1.249 'date' Fri Oct 24 18:29:29 CST 2014 Fri Oct 24 18:29:29 CST 2014
我这是做完双击互信后可以查看2边的时间
我同步window的时钟
[root@e3 ~]# ntpdate time.windows.com 24 Oct 18:28:06 ntpdate[11532]: step time server 65.55.56.206 offset -41.421659 sec
当然自己搭建时钟服务器也可以别的时钟服务器也行
,设置hosts文件能解析对方主机名和IP
192.168.1.243 i511.tele.com 192.168.1.249 e3.teleframe.cn
复制到对方主机
[root@i51 ~]# scp /etc/hosts [email protected]:/etc/ [email protected]'s password: hosts
c>:设置ssh互信2台都要如下操作,我这做过就覆盖了
[root@i51 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' Generating public/private rsa key pair. /root/.ssh/id_rsa already exists. Overwrite (y/n)? y 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: 75:0c:56:74:95:1a:93:80:a5:87:51:26:30:27:93:0e [email protected] The key's randomart image is: +--[ RSA 2048]----+ | =o+*B+ o.o| | E .=o*o = . | | o o..o + | | . ... . | | S | | | | | | | | | +-----------------+
复制公钥到对方节点
[root@i51 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected] The authenticity of host 'e3.teleframe.cn (192.168.1.249)' can't be established. RSA key fingerprint is 98:75:98:d5:df:f4:b4:4a:c3:75:35:ed:ee:63:1b:47. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'e3.teleframe.cn' (RSA) to the list of known hosts. [email protected]'s password: Now try logging into the machine, with "ssh '[email protected]'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
另一台也是如此这里省略
做完测试看互相连接不需要密码就可以测试如下
[root@e3 ~]# ssh 192.168.1.243 'ifconfig' eth0 Link encap:Ethernet HWaddr E0:3F:49:A3:58:FC inet addr:192.168.1.243 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::e23f:49ff:fea3:58fc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2271994 errors:0 dropped:0 overruns:0 frame:0 TX packets:2389685 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1180578751 (1.0 GiB) TX bytes:1578422149 (1.4 GiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:18077 errors:0 dropped:0 overruns:0 frame:0 TX packets:18077 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2294727 (2.1 MiB) TX bytes:2294727 (2.1 MiB)
安装相关的软件
阿里云的epel 默认的光盘有可能没带相关的软件包没带的话去官网下载源码包 2个节点都装
[root@i51 ~]# yum -y install heartbeat;ssh 192.168.1.249 'yum -y install heartbeat'
提供配置文件
[root@i51 ha.d]# cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} ./
配置authkeys
先生成随机码这个方法很多我这个简单
[root@i51 ha.d]# md5sum authkeys 5f5430be18342f15e9143a01b215c3b0 authkeys 打开authkys 修改如下 auth 1 #1 crc #2 sha1 HI! #3 md5 Hello! auth 1 1 md5 5f5430be18342f15e9143a01b215c3b0 这是生成的随机码贴着
配置ha.cf
修改如下
[root@i51 ha.d]# grep -v ^# ha.cf logfacility local0 keepalive 1 bcast eth0 # Linux auto_failback on node i511.tele.com 主要是2个节点要一定和你的uname -n 保持一致 node e3.teleframe.cn ping 192.168.1.1
2台服务器都装httpd用来测试
[root@i51 www]# yum install httpd -y ;ssh 192.168.1.249 'yum -y install httpd'
我的测试页面为Hello Word!
访问2台测试页面没问题关掉httpd
[root@e3 html]# /etc/init.d/httpd stop ;ssh 192.168.1.243 '/etc/init.d/httpd stop' Stopping httpd: [FAILED] Stopping httpd: [ OK ] [root@e3 html]# chkconfig httpd off ;ssh 192.168.1.243 'chkconfig httpd off'
配置haresource配置如下
[root@i51 ha.d]# grep -v ^# haresources i511.tele.com IPaddr::192.168.1.100/24/eth0 httpd
这是默认活动节点的uname -n IP是虚拟IP 掩码 绑定那个网卡上 httpd服务
拷贝配置文件到另一个节点
[root@i51 ha.d]# scp -p authkeys ha.cf haresources [email protected]:/etc/ha.d/ authkeys 100% 692 0.7KB/s 00:00 ha.cf 100% 10KB 10.3KB/s 00:00 haresources
启动heartbeat
[root@i51 ha.d]# /etc/init.d/heartbeat start Starting High-Availability services: INFO: Resource is stopped Done. [root@i51 ha.d]# /etc/init.d/heartbeat status heartbeat OK [pid 2518 et al] is running on i511.tele.com [i511.tele.com]... [root@i51 ha.d]# ssh 192.168.1.249 '/etc/init.d/heartbeat start' Starting High-Availability services: 2014/10/24_19:49:44 INFO: Resource is stopped Done. [root@i51 ha.d]#
OK
来测试
[root@e3 log]# ip addr show 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 e0:3f:49:13:0a:7e brd ff:ff:ff:ff:ff:ff inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 brd 192.168.1.255 scope global secondary eth0 这条可以看到虚拟IP192.168.1.100 已经绑定到eth0网卡上了 inet6 fe80::e23f:49ff:fe13:a7e/64 scope link valid_lft forever preferred_lft forever 3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:43:f5:8a brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:43:f5:8a brd ff:ff:ff:ff:ff:ff
因为虚拟IP是192.168.1.100 所以测试结果
有个heartbeat自带的脚本测试转移
执行
[root@i51 heartbeat]# ./hb_standby Going standby [all]. [root@i51 heartbeat]# pwd /usr/share/heartbeat
OK在来测试
[root@i51 heartbeat]# ip addr show 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 e0:3f:49:a3:58:fc brd ff:ff:ff:ff:ff:ff inet 192.168.1.243/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 brd 192.168.1.255 scope global secondary eth0 地址已经切过来了 inet6 fe80::e23f:49ff:fea3:58fc/64 scope link valid_lft forever preferred_lft forever
查看端口
[root@i51 heartbeat]# ss -an State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::50315 :::* LISTEN 0 128 *:60111 *:* LISTEN 0 128 :::111 :::* LISTEN 0 128 *:111 *:* LISTEN 0 128 :::80 :::* LISTEN 0 32 *:21 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 ::1:631 :::* LISTEN 0 10 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 :::443 :::* LISTEN 0 5 127.0.0.1:25151 *:* LISTEN 0 64 :::873 :::* LISTEN 0 128 *:3306 *:* ESTAB 0 52 192.168.1.243:22 192.168.1.252:64304
80端口
OK一个简单个高可用http服务器就搭建完成
实现拓扑
重新启动一台server充当一个nfs或者scsi来充当存储 每个http节点自动挂载,
mkdir /web/htdocs -pv vim /etc/exports [root@nfs ~]# cat /etc/exports /web/htdocs 192.168.1.0/24(ro) [root@nfs ~]# /etc/init.d/rpcbind start 正在启动 rpcbind: [确定] [root@nfs ~]# /etc/init.d/nfs restart 关闭 NFS 守护进程: [确定] 关闭 NFS mountd: [确定] 关闭 NFS 服务: [确定] Shutting down RPC idmapd: [确定] 启动 NFS 服务: [确定] 启动 NFS mountd: [确定] 启动 NFS 守护进程: [确定] 正在启动 RPC idmapd: [确定] [root@nfs ~]# showmount -e 192.168.1.115 Export list for 192.168.1.115: /web/htdocs 192.168.1.0/24
测试挂载关闭nfs服务器上的防火墙和selinux
[root@nfs ~]# /etc/init.d/iptables stop iptables:将链设置为政策 ACCEPT:filter [确定] iptables:清除防火墙规则: [确定] iptables:正在卸载模块: [确定] [root@nfs ~]# setenforce 0
测试远程挂载
[root@e3 heartbeat]# mount 192.168.1.115:/web/htdocs /mnt/ [root@e3 heartbeat]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_e3-root 103212320 3645924 94323516 4% / tmpfs 3981468 0 3981468 0% /dev/shm /dev/sda2 198337 69886 118211 38% /boot /dev/sda1 204580 260 204320 1% /boot/efi /dev/mapper/vg_e3-data 516061624 202532 489644692 1% /data /dev/mapper/vg_e3-home 103212320 340800 97628640 1% /home 192.168.1.115:/web/htdocs 18134400 1254912 15958272 8% /mnt
关闭2台节点的heartbeat
修改haresource文件
[root@i51 ~]# vim /etc/ha.d/haresources 修改如下 i511.tele.com IPaddr::192.168.1.100/24/eth0 Filesystem::192.168.1.115:/web/htdocs::/var/www/html::nfs httpd [root@i51 ha.d]# scp haresources 192.168.1.249:/etc/ha.d/ haresources 复制到对端节点 启动服务于看日志 [root@i51 ha.d]# /etc/init.d/heartbeat start Starting High-Availability services: INFO: Resource is stopped Done. [root@i51 ha.d]# ssh 192.168.1.249 '/etc/init.d/heartbeat start' Starting High-Availability services: 2014/10/27_14:36:54 INFO: Resource is stopped Done. [root@i51 ha.d]# tial /var/log/messages -bash: tial: command not found [root@i51 ha.d]# tail /var/log/messages Oct 27 14:37:11 i51 kernel: RPC: Registered named UNIX socket transport module. Oct 27 14:37:11 i51 kernel: RPC: Registered udp transport module. Oct 27 14:37:11 i51 kernel: RPC: Registered tcp transport module. Oct 27 14:37:11 i51 kernel: RPC: Registered tcp NFSv4.1 backchannel transport module. Oct 27 14:37:11 i51 kernel: FS-Cache: Loaded Oct 27 14:37:11 i51 kernel: NFS: Registering the id_resolver key type Oct 27 14:37:11 i51 kernel: FS-Cache: Netfs 'nfs' registered for caching Oct 27 14:37:11 i51 nfsidmap[32272]: nss_getpwnam: name 'root@localdomain' does not map into domain 'tele.com' Oct 27 14:37:11 i51 /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_192.168.1.115:/web/htdocs)[32200]: INFO: Success Oct 27 14:37:11 i51 ResourceManager(default)[31903]: info: Running /etc/init.d/httpd start
查看
[root@i51 ha.d]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_i52-root 103212320 83347172 14622268 86% / tmpfs 3964280 0 3964280 0% /dev/shm /dev/sda2 198337 92871 95226 50% /boot /dev/sda1 204580 264 204316 1% /boot/efi /dev/mapper/vg_i52-data 269763184 666416 255393600 1% /data /dev/mapper/vg_i52-home 103212320 433512 97535928 1% /home 192.168.1.115:/web/htdocs 18134400 1254912 15958272 8% /var/www/html 查看端口 [root@i51 html]# ss -an State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::50315 :::* LISTEN 0 128 *:60111 *:* LISTEN 0 128 :::111 :::* LISTEN 0 128 *:111 *:* LISTEN 0 128 :::80 :::* LISTEN 0 64 :::58672 :::* LISTEN 0 64 *:41040 *:* LISTEN 0 32 *:21 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 ::1:631 :::* LISTEN 0 10 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 :::443 :::* LISTEN 0 5 127.0.0.1:25151 *:* LISTEN 0 64 :::873 :::* LISTEN 0 128 *:3306 *:* ESTAB 0 84 192.168.1.243:22 23.234.61.65:44638 TIME-WAIT 0 0 192.168.1.243:22 23.234.61.65:43402 ESTAB 0 52 192.168.1.243:22 192.168.1.252:64304 ESTAB 0 0 192.168.1.243:41040 192.168.1.115:747 SYN-SENT 0 1 192.168.1.243:41578 192.168.0.249:3306 ESTAB 0 0 192.168.1.243:699 192.168.1.115:2049 TIME-WAIT 0 0 192.168.1.243:22 23.234.61.65:42193 [root@i51 html]# ip addr show 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 e0:3f:49:a3:58:fc brd ff:ff:ff:ff:ff:ff inet 192.168.1.243/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.100/24 brd 192.168.1.255 scope global secondary eth0 inet6 fe80::e23f:49ff:fea3:58fc/64 scope link valid_lft forever preferred_lft forever
切换或者关闭主节点
[root@e3 heartbeat]# /usr/share/heartbeat/hb_standby Going standby [all].
切换成备节点 一样可以放问nfs server
http://www.linuxfly.org/post/531/ 参考