nginx的HA集群及配置

一、服务器环境

master服务器:

ip:192.168.11.155

系统环境:CentOS release 6.5 (Final)

内核版本:2.6.32-431.el6.x86_64

防火墙和selinux已关闭

slave从服务器:

ip:192.168.11.156

系统环境:Red Hat Enterprise Linux Server release 6.5 (Santiago)内核版本:2.6.32-431.el6.x86_64

防火墙和selinux已关闭

二、配置主机名

master服务器:master

# hostname master

slave服务器:slave

# hostname slave

编辑/etc/hosts文件,加入如下内容

192.168.11.155 master

192.168.11.156 slave

三、配置扩展源,安装heartbeat

# wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm

# rpm -ivh epel-release-6-8_64.noarch.rpm

# yum -y install heartbeat

# yum -y install libnet

四、增加虚拟网卡,配置虚拟ip

# cd /etc/sysconfig/network-scripts/

# cp ifcfg-eth1 ifcfg-eth1:1

# vim ifcfg-eth1:1修改成如下内容

DEVICE=eth1:1

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.11.160

NETMASK=255.255.255.0

重启网卡服务:

# /etc/init.d/network restart

五、heartbeat配置

先将配置文件copy到/etc/ha.d/目录下

# cp authkeys ha.cf haresources /etc/ha.d/

编辑验证配置文件authkeys选择加密方式

auth 3

3 md5 Hello!

修改authkeys文件的权限

# chmod 600 authkeys

编辑配置文件haresources

# vim haresources添加如下内容或者修改备注是的node行

master  192.168.11.161/24/eth1:1 nginx

# 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    192.168.11.156

auto_failback   on 

node    master

node    slave

ping 192.168.11.1

respawn hacluster   /usr/lib64/heartbeat/ipfail

六、将主上的heartbeat配置文件复制到从上

# scp authkeys haresources ha.cf slave:/etc/ha.d/

只需要修改从服务器的ha.cf配置文件

ucast   eth0    192.168.11.156

改成:

ucast   eth1    192.168.11.155

七、启动heartbeat服务(先主后从)

# /etc/init.d/heartbeat start

测试高可用是否生效

将主服务器上的icmp包DROP掉,看从服务器的nginx服务否启动

原文地址:https://www.cnblogs.com/fansik/p/5361226.html