HA==high available
原理
环境准备
header 1 | header 2 |
---|---|
关闭iptables | iptables -F |
关闭selinux | disabled |
/etc/hosts | hostname ip |
安装包
yum install -y epel
yum install -y libnet heartbeat
复制配置文件样例到/etc/ha.d/下
# cd /usr/share/doc/heartbeat-3.0.4/
# cp authkeys ha.cf haresources /etc/ha.d/
主节点配置
配置验证文件及修改权限为当前用户600
vim authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
# 从上到下安全级别越来越高,采用第几级,auth后面就写数字几
chmod 600 authkeys
配置虚拟IP(与HA无关)
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-eth0\:1
# eth0:1配置
DEVICE=eth0:1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.139.199
NETMASK=255.255.255.0
service network restart
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:60:22:B6
inet addr:192.168.139.199 Bcast:192.168.139.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
配置 vim haresources
node1 10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
主节点主机名 虚拟流动IP 启动资源
centos6-1 192.168.139.189/24/eth0:0 nginx
配置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 192.168.139.129
auto_failback on
node centos6-1
node centos6-2
ping 192.168.139.2
respawn hacluster /usr/lib64/heartbeat/ipfail
从节点配置(主要修改ha.cf中的对方ip地址)
配置验证文件及修改权限为当前用户600
vim authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
# 从上到下安全级别越来越高,采用第几级,auth后面就写数字几
chmod 600 authkeys
配置 vim haresources
node1 10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
主节点主机名 虚拟流动IP 启动资源
centos6-1 192.168.139.189/24/eth0:0 nginx
配置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 192.168.139.145
auto_failback on
node centos6-1
node centos6-2
ping 192.168.139.2
respawn hacluster /usr/lib64/heartbeat/ipfail
安装测试主程序Nginx
启动顺序
先启动主heartbeat,再启动从heartbeat
在主节点上启动heartbeat
/etc/init.d/heartbeat start
从节点上启动heartbeat
/etc/init.d/heartbeat start
测试
访问虚拟ip
[root@centos6-1 ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:60:22:B6
inet addr:192.168.139.144 Bcast:192.168.139.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe60:22b6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:87246 errors:0 dropped:0 overruns:0 frame:0
TX packets:26970 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:114528452 (109.2 MiB) TX bytes:2487660 (2.3 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:60:22:B6
inet addr:192.168.139.189 Bcast:192.168.139.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
ps aux|grep nginx
root 7038 0.0 0.1 108916 2188 ? Ss 18:14 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 7039 0.0 0.1 109340 2860 ? S 18:14 0:00 nginx: worker process
nginx 7040 0.0 0.1 109340 2932 ? S 18:14 0:00 nginx: worker process
nginx 7041 0.0 0.1 109340 2932 ? S 18:14 0:00 nginx: worker process
nginx 7043 0.0 0.1 109340 2912 ? S 18:14 0:00 nginx: worker process
root 7046 0.0 0.0 103312 880 pts/1 S+ 18:14 0:00 grep nginx
echo "1111111cetos6-1" > /usr/share/nginx/html/index.html
结果
关闭主节点的icmp
iptables -A INPUT -p icmp -j DROP
此时从节点的服务启动
[root@centos6-2 ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:8F:D3:D2
inet addr:192.168.139.129 Bcast:192.168.139.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8f:d3d2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:47256 errors:0 dropped:0 overruns:0 frame:0
TX packets:22151 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:57900661 (55.2 MiB) TX bytes:2136758 (2.0 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:8F:D3:D2
inet addr:192.168.139.189 Bcast:192.168.139.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@centos6-2 ha.d]# ps aux |grep nginx
root 3619 0.0 0.1 108920 2184 ? Ss 14:49 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 3620 0.0 0.1 109344 2856 ? S 14:49 0:00 nginx: worker process
nginx 3621 0.0 0.1 109344 2928 ? S 14:49 0:00 nginx: worker process
nginx 3623 0.0 0.1 109344 2928 ? S 14:49 0:00 nginx: worker process
nginx 3624 0.0 0.1 109344 2908 ? S 14:49 0:00 nginx: worker process
root 3627 0.0 0.0 103312 884 pts/0 R+ 14:49 0:00 grep nginx
echo "222222CENTOS6-2" > /usr/share/nginx/html/index.html
使用python脚本进行不间断测试
# -*- coding: utf-8 -*-
import requests
import time
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
#return r.text
return r.text
except:
return '产生异常'
if __name__ == "__main__":
url = "http://192.168.139.189/"
for i in range(100):
time.sleep(2)
print(getHTMLText(url))
如果将测试频率增减到0.5秒一次,有可能出现异常