简介
通过安装该Linux-HA软件,可以实现Linux双机系统的高可用性解决方案,实现双机系统的热备份,并能够做到双机之间的无缝切换,从而对外
提供稳定可靠的服务,最终实现系统高性能RAS(reliability, availability, and serviceability)。
实验环境
这里使用heartbeat来做HA集群,并且把nginx服务作为HA对应的服务。
Chumjtest01 | eth0 172.31.24.199 | eth0:1 192.168.1.109 | VIP: 172.31.24.202 |
Chumjtest02 | eth0 172.31.24.200 | eth0:1 192.168.1.110 | VIP: 172.31.24.202 |
实验准备工作
1.编辑修改hosts文件,将两台服务器的IP添加上,
2. 关闭防火墙 iptables -F;
关闭selinux: setenforce 0
3.安装好eprl,安装好heartbeat、libnet
主配置
[root@chumjtest01 ~]# cd /usr/share/doc/heartbeat-3.0.4/ [root@chumjtest01 heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/ [root@chumjtest01 heartbeat-3.0.4]# cd /etc/ha.d/ [root@chumjtest01 ha.d]# ls authkeys ha.cf harc haresources rc.d README.config resource.d shellfuncs [root@chumjtest01 ha.d]# vim authkeys //authkeys是验证主从的 auth 3 //选择3 使用md5 验证 #1 crc #2 sha1 HI! 3 md5 Hello! //去掉#号,表示打开md5来验证 [root@chumjtest01 ha.d]# chmod 600 authkeys //赋予root读写权限。
配置VIP
[root@chumjtest01 ha.d]# cd /etc/sysconfig/network-scripts/ [root@chumjtest01 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1 //新建一个IP配置文件,同一个网卡可以配置多个IP [root@chumjtest01 network-scripts]# vim ifcfg-eth0:1 //编辑VIP DEVICE=eth0:1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=172.31.24.202 //将IP地址改成VIP地址,去掉一些不要的内容。 PREFIX=24 #GATEWAY=172.31.24.1 DEFROUTE=yes [root@chumjtest01 network-scripts]# service network restart //重启下网络服务 Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining if ip address 172.31.24.199 is already in use for device eth0... Determining if ip address 172.31.24.198 is already in use for device eth0... [ OK ] [root@chumjtest01 network-scripts]# ifconfig //可以看到VIP网卡已经显示 eth0 Link encap:Ethernet HWaddr 00:50:56:B3:79:E3 inet addr:172.31.24.199 Bcast:172.31.24.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:feb3:79e3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:22 errors:0 dropped:0 overruns:0 frame:0 TX packets:24 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2130 (2.0 KiB) TX bytes:3428 (3.3 KiB) eth0:1 Link encap:Ethernet HWaddr 00:50:56:B3:79:E3 inet addr:172.31.24.198 Bcast:172.31.24.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 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:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
配置节点文件
[root@chumjtest01 /]# cd /etc/ha.d/ [root@chumjtest01 ha.d]# vim haresources #node1 10.0.0.170 Filesystem::/dev/sda1::/data1::ext2 chumjtest01 172.31.24.198:/24/eth0:0 nginx //添加主机名、VIP和需要集群的服务
配置ha配置文件
[root@chumjtest01 ha.d]# vim ha.cf debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 60 udpport 694 ucast eth0 172.31.24.200 //对方的IP地址 auto_failback on node chumjtest01 //node1地址 node chumjtest02 //node2地址 ping 172.31.24.1 //仲裁地址,一般用路由器或者交换机 respawn hacluster /usr/lib64/heartbeat/ipfail
从机配置
将主机上的配置文件拷贝到从机上
[root@chumjtest01 ha.d]scp authkeys ha.cf haresources chumjtest02:/etc/ha.d/ 只要修改 vi /etc/ha.d/ha.cf //只需要更改一个地方 ucast eth1 172.31.24.200 改为 ucast eth1 172.31.24.199
启动heartbeat
先启动主,在启动从
主机启动情况:
[root@chumjtest01 ha.d]# service heartbeat start Starting High-Availability services: INFO: Resource is stopped Done.
检查下nginx服务有没有启动
[root@chumjtest01 ha.d]# ps aux |grep nginx root 6989 0.0 0.0 109336 2036 ? Ss 17:40 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 6990 0.0 0.0 109760 2704 ? S 17:40 0:00 nginx: worker process nginx 6991 0.0 0.0 109760 2756 ? S 17:40 0:00 nginx: worker process root 6995 0.0 0.0 103252 824 pts/1 S+ 17:41 0:00 grep nginx
从机启动情况:
[root@chumjtest02 ha.d]# service heartbeat start Starting High-Availability services: INFO: Resource is stopped Done.
测试
在主机的nginx下写一个简单的html文件,并且访问下
[root@chumjtest01 /]# cd /usr/share/nginx/html [root@chumjtest01 html]# vim 1.html chumjtest01 [root@chumjtest01 ha.d]# curl http://172.31.24.199/1.html //测试下显示chumjtest01 chumjtest01
同样在从机上写下一个简单的html文件
[root@chumjtest02 html]# vim 1.html chumjtest02 [root@chumjtest02 html]# curl http://172.31.24.200/1.html //网页无法显示,说明Nginx没启动404 Not Found Not Found
The requested URL /1.html was not found on this server.
Apache/2.2.15 (CentOS) Server at 172.31.24.200 Port 80
在测试下VIP访问,说明现在在nginx在主机上运行
[root@chumjtest01 ha.d]# curl http://172.31.24.202/1.html chumjtest01
打开防火墙,将主机禁ping
[root@chumjtest01 html]# iptables -I INPUT -p icmp -j DROP
查看主机chumjtest01的HA日志,看到“We are dead”,表示主机认为自己已经死了,要切到从机上
Jan 05 13:55:29 chumjtest01 ipfail: [21826]: info: NS: We are dead. :<
查看从机chumjtest02的HA日志,从机启动ngix。
ResourceManager(default)[3176]: 2017/01/05_13:55:44 info: Running /etc/init.d/nginx start
在访问下VIP的地址,已经切换到从机上去了。
[root@chumjtest01 html]# curl http://172.31.24.202/1.html chumjtest02
注:
heartbeat日志在/var/log/ha-log,如果启动有问题请尽量查看日志。
1.ERROR: Client child command [/usr/lib/heartbeat/ipfail] is not executable
说明64位系统,要在ha.cf下的/usr/lib64/heartbeat/ipfail
2.ERROR: Illegal directive [node ] in /etc/ha.d//ha.cf
ucast eth0 172.31.24.200 要注意在ha.cf下网络端口是否匹配