linux下搭建HA高可用集群
HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务。 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived,其中keepalived有负载均衡的功能。
下面我们使用heartbeat来做HA集群,并且把nginx服务作为HA对应的服务。
试验准备工作:
两台机器,都是centos6.6,各增加一块网卡eth1,使用主机模式连接vmnet1,并设定为192.168.11.0网段;eth0为NAT模式,网段为192.168.20.0网段;
开机启动后,拷贝ifcfg-eth0 到 ifcfg-eth1,修改DEVICE为eth1,ip地址设为:192.168.11.20
# cd /etc/sysconfig/network-scripts/ # cp ifcfg-eth0 ifcfg-eth1 # cat ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.11.20
设置完成后,重启网卡,ifconfig查看eth0、eth1的ip地址;
# /etc/init.d/network restart #ifconfig
主机的配置:
hostname hello
eth0:192.168.20.20
eth1:192.168.11.20
从机的配置:
hostname web
eth0:192.168.20.30
eth1:192.168.11.30
1、设置hostname,分别为hello和 web;
2、主从都需要关闭防火墙;关闭selinux;
# iptables -F # setenforce 0 setenforce: SELinux is disabled
3、 vi /etc/hosts //主从都需要增加内容如下:
192.168.20.20 hello 192.168.20.30 web
4、安装epel扩展源
# yum install -y epel-release
5、两个机器都安装heartbeat、libnet
#yum install -y heartbeat* libnet nginx
nginx做代理服务;
6、主上(hello)配置
Heartbeat软件的安装目录为/etc/ha.d目录;
拷贝配置文件的模板到/etc/ha.d目录;
cd /usr/share/doc/heartbeat-3.0.4/ cp authkeys ha.cf haresources /etc/ha.d/ cd /etc/ha.d
vi authkeys //去掉前面的#号,或加入下面的内容:
auth 3 3 md5 Hello!
更改权限为600
chmod 600 authkeys
vi haresources //加入
hello 192.168.11.100/24/eth1:0 nginx
192.168.11.100 ip地址是流动ip,虚拟ip;用来绑定服务的;子网掩码为24位;使用eth1:0绑定虚拟ip,并运行nginx进行测试;
注意:两台主机的haresources文件设置必须完全一致。
vi 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 eth1 192.168.11.30 auto_failback on node hello node web ping 192.168.11.1 respawn hacluster /usr/lib/heartbeat/ipfail
7、把主上的三个配置拷贝到从上:
#cd /etc/ha.d/ #scp authkeys ha.cf haresources web:/etc/ha.d/
8、从上(web) 只需要编辑ha.cf
vi /etc/ha.d/ha.cf //只需要更改一个地方
ucast eth1 192.168.11.30 改为 ucast eth1 192.168.11.20
9、启动heartbeat
先主,后从
#service heartbeat start
10、检查测试
ifconfig 查看是否有 eth1:0 出现则说明主已经开始工作;
[root@yong ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:43:3D:32 inet addr:192.168.20.20 Bcast:192.168.20.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe43:3d32/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:433 errors:0 dropped:0 overruns:0 frame:0 TX packets:429 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:37095 (36.2 KiB) TX bytes:75381 (73.6 KiB) Interrupt:18 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 00:0C:29:43:3D:3C inet addr:192.168.11.20 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe43:3d3c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:396 errors:0 dropped:0 overruns:0 frame:0 TX packets:399 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:83641 (81.6 KiB) TX bytes:89725 (87.6 KiB) Interrupt:18 Base address:0x2080 eth1:0 Link encap:Ethernet HWaddr 00:0C:29:43:3D:3C inet addr:192.168.11.100 Bcast:192.168.11.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2080 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:65536 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)
查看是否有nginx进程、heartbeat进程;
[root@yong ha.d]# ps aux |grep nginx root 3731 0.0 0.1 5000 632 ? Ss 10:52 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 3732 0.0 0.1 5200 984 ? S 10:52 0:00 nginx: worker process
[root@yong ha.d]# ps aux |grep heartbeat root 2765 0.3 1.3 6684 6676 ? SLs 10:44 0:01 heartbeat: master control process root 2770 0.0 1.2 6488 6480 ? SL 10:44 0:00 heartbeat: FIFO reader root 2771 0.0 1.2 6484 6476 ? SL 10:44 0:00 heartbeat: write: ucast eth1 root 2772 0.0 1.2 6484 6476 ? SL 10:44 0:00 heartbeat: read: ucast eth1 root 2773 0.0 1.2 6484 6476 ? SL 10:44 0:00 heartbeat: write: ping 192.168.11.1 root 2774 0.0 1.2 6484 6476 ? SL 10:44 0:00 heartbeat: read: ping 192.168.11.1 498 2787 0.0 0.2 5380 1488 ? S 10:45 0:00 /usr/lib/heartbeat/ipfail
11、测试1:主上故意禁ping
#iptables -I INPUT -p icmp -j DROP
windows客户端一直ping 192.168.11.100 ,一直通,禁止ping之后中间会中断一会,之后一直通;
在从上ifconfig查看出现eth1:0,查看nginx进程也有,说明转换到从机器上运行;
[root@web ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:97:C3:EC inet addr:192.168.20.30 Bcast:192.168.20.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe97:c3ec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:460 errors:0 dropped:0 overruns:0 frame:0 TX packets:469 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:37104 (36.2 KiB) TX bytes:101866 (99.4 KiB) Interrupt:19 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 00:0C:29:97:C3:F6 inet addr:192.168.11.30 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe97:c3f6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1022 errors:0 dropped:0 overruns:0 frame:0 TX packets:1035 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:191275 (186.7 KiB) TX bytes:182393 (178.1 KiB) Interrupt:18 Base address:0x2080 eth1:0 Link encap:Ethernet HWaddr 00:0C:29:97:C3:F6 inet addr:192.168.11.100 Bcast:192.168.11.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2080
12、测试2:主上停止heartbeat服务
#service heartbeat stop
windows客户端一直ping 192.168.11.100 ,一直通,停止心跳服务后中间会中断一会,之后一直通;
在从上ifconfig查看出现eth1:0,查看nginx进程也有,说明转换到从机器上运行;
13、测试3:测试脑裂,主和从都停掉eth1网卡;
eth1网卡禁用,ifdown eth1 心跳线断了之后,会发生脑裂;主从之间连接断了,从在死亡时间没收到主的心跳,则认为主死亡。
主死了,从机器接管服务,从机器使用虚拟ip地址,主和从都在运行服务;
主机器没有eth1网卡,一直运行nginx;
[root@yong ha.d]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:43:3D:32 inet addr:192.168.20.20 Bcast:192.168.20.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe43:3d32/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11442 errors:0 dropped:0 overruns:0 frame:0 TX packets:15376 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:842833 (823.0 KiB) TX bytes:10226838 (9.7 MiB) Interrupt:18 Base address:0x2000 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:65536 Metric:1 RX packets:1163 errors:0 dropped:0 overruns:0 frame:0 TX packets:1163 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:150095 (146.5 KiB) TX bytes:150095 (146.5 KiB)
[root@yong ha.d]# ps aux |grep nginx root 11686 0.0 0.1 5000 636 ? Ss 14:27 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11687 0.0 0.1 5200 988 ? S 14:27 0:00 nginx: worker process
从机器查看地址,eth1:0出现
[root@localhost ha.d]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:97:C3:EC inet addr:192.168.20.30 Bcast:192.168.20.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe97:c3ec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:7901 errors:0 dropped:0 overruns:0 frame:0 TX packets:5891 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:603592 (589.4 KiB) TX bytes:2094606 (1.9 MiB) Interrupt:19 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 00:0C:29:97:C3:F6 inet6 addr: fe80::20c:29ff:fe97:c3f6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20774 errors:0 dropped:0 overruns:0 frame:0 TX packets:59144 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3513861 (3.3 MiB) TX bytes:12998603 (12.3 MiB) Interrupt:18 Base address:0x2080 eth1:0 Link encap:Ethernet HWaddr 00:0C:29:97:C3:F6 inet addr:192.168.11.100 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2080
[root@localhost ha.d]# ps aux |grep nginx root 8938 0.0 0.0 3684 584 ? Ss 14:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 8940 0.0 0.1 4936 2008 ? S 14:37 0:00 nginx: worker process nobody 8941 0.0 0.2 4936 2064 ? S 14:37 0:00 nginx: worker process
14、测试4:设置auto_failback off
主从都需要配置,启动heartbeat,主上禁止ping之后,跳转到从机器,主恢复ping之后,也不会跳转到主,一直是从机器提供服务;
HA高可用搭建完毕;
本文出自 “模范生的学习博客” 博客,请务必保留此出处http://8802265.blog.51cto.com/8792265/1658552
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。