这里使用heartbeat来做HA集群,并且把nginx服务作为HA对应的服务。
- 编辑修改hosts文件,将两台服务器的IP添加上
vim /etc/hosts
192.168.2.89 liuke2
192.168.2.91 liuke3
- 清空防火墙
iptables -F
- 关闭selinux:
setenforce 0
- 更换yum源,安装epel-release
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
yum install -y epel-release
- 安装heartbeat、libnet
yum install -y libnet heartbeat
————————————————上面5点是主从一样的操作——————————
6. 拷贝配置文件
cp /usr/share/doc/heartbeat-3.0.4/authkeys /usr/share/doc/heartbeat-3.0.4/haresources /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d/
- 修改配置文件
7.1
vim /etc/ha.d/authkeys
auth 3 #取消注释
#1 crc
#2 sha1 HI!
3 md5 Hello!#取消注释
chmod 600 /etc/ha.d/authkeys
7.2
vim /etc/ha.d/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.2.91 #从的ip
auto_failback on
node liuke2 #主的地址
node liuke3 #从的地址
ping 192.168.2.254 #仲裁地址,一般用路由器或者交换机
respawn hacluster /usr/lib64/heartbeat/ipfail
7.3
vim /etc/ha.d/haresources
加上下面行:
liuke2 192.168.2.100/24/eth0:0 nginx #主的主机名、虚拟IP地址、掩码和要集群的服务
————————————-6-7点是在主上操作——————————————
8. 配置从
scp /usr/share/doc/heartbeat-3.0.4/authkeys /usr/share/doc/heartbeat-3.0.4/haresources /usr/share/doc/heartbeat-3.0.4/ha.cf 192.168.2.91:/etc/ha.d/
vim /etc/ha.d/ha.cf
修改为主的ip
ucast eth0 192.168.2.89 //对方的IP地址
————————————–8是在从的操作————————————————
测试:
先后启动主和从的heartbeat
service heartbeat start
在主和从上的网页中写入不同的内容,以便区分
在主上:
echo "master" >/usr/share/nginx/html/index.html
ip add
inet 192.168.2.100/24 brd 192.168.2.255 scope global secondary eth0:0#看到漂移IP
ps aux | grep nginx
看到nginx已经启动
在从上:
echo "slave" >/usr/share/nginx/html/index.html
ip add
并没有漂移IP
ps aux | grep nginx
nginx没有启动
输入漂移ip访问nginx服务
192.168.2.100/1.html
master #显示的是主上的资源
在主上模拟障碍,让主挂掉
iptables -A INPUT -p icmp -j DROP
在次按上述方法查看nginx和漂移,发现主上的漂移IP转移到了从上,且从上的nginx服务已经启动,在此输入漂移ip访问nginx服务
192.168.2.100/1.html
slave #显示的是从上的资源
至此,主从正常切换