HA集群(nginx )

本文介绍了如何构建高可用HA集群,重点是Nginx的配置。首先讲解了HA集群的基本原理,然后详细阐述了环境准备、安装包、配置文件的复制与修改,包括主节点和从节点的配置。接着,文章描述了安装测试Nginx主程序的步骤,以及启动heartbeat的顺序。在测试环节,通过访问虚拟IP和使用Python脚本进行不间断测试,确保集群的稳定性。当主节点关闭ICMP后,从节点能自动接管服务。
摘要由CSDN通过智能技术生成

HA==high available

原理

mark

环境准备

header 1header 2
关闭iptablesiptables -F
关闭selinuxdisabled
/etc/hostshostname 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

结果
mark

关闭主节点的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))

mark

如果将测试频率增减到0.5秒一次,有可能出现异常
mark

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值