Keepalived实现Nginx高可用
Keepalived的安装
下载keepalived官网:http://keepalived.org
Keepalived配置
keepalived相当于设置两台nginx主机抢占同一个ip,需要设置一个虚拟ip,这个虚拟ip代表客户端要访问的ip。
修改配置文件: /etc/keepalived/keepalived.conf
分别启动两台机器上的keepalived
测试:
杀掉master上的keepalived进程,你会发现,在slave机器上的eth0网卡多了一个ip地址
查看ip地址的命令: ip addr
原理:
Keepalived并不跟nginx耦合,它俩完全不是一家人
但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了
|
|
添加切换通知脚本
vi /usr/local/keepalived/sbin/notify.sh
添加执行权限
chmod +x /usr/local/keepalived/sbin/notify.sh
在第二台机器上添加notify.sh脚本
分别在两台机器上启动keepalived
service keepalived start
chkconfig keepalived on