1、下载keepalived源码包
去http://www.keepalived.org/software/keepalived-1.4.2.tar.gz下载
2、解压安装
解压 tar -zvxf keepalived-1.4.1.tar.gz
配置 ./configure --prefix=/usr/local/keepalived
编译 make
安装 make install
3、注册keepalived服务
keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件
/etc/rc.d/init.d 目录设置可执行文件 就可以使用service命令操作
所以就需要将几个文件拷贝一下,在etc下创建keepalived文件夹
mkdir /etc/keepalived
复制到相关径下
#keepalived基本配置文件
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#注册启动命令,用于service启动、停止
cp /opt/nginx/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
#keepalived系统配置
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#不复制启动报错
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
4、创建nginx运行状态检测脚本
vim check_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
#监控nginx的脚本
#查看是否有 nginx进程 把值赋给变量A
#统计nginx的进程数 如果大于0 说明nginx启动了
#如果等于0 ,说明nginx没运行,那么启动nginx
#休眠3秒 再次统计nginx进程数 如果等于0 说明没启动成功
#杀掉keepalived, keepalied将虚拟 ip 绑定到 BACKUP
添加可执行权限:chmod +x check_nginx.sh
5、进行Keepalived配置
(1)Master配置(ip:192.168.1.77)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx_master
}
#一定要定义在track_script上面 否则keepalived不执行检查nginx的脚本
vrrp_script check_nginx {
script "/usr/local/keepalived/scripts/check_nginx.sh"
interval 2 ## 2s检测一次
weight -20 #如果条件成立,权重-20
}
vrrp_instance VI_1 {
state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP
interface eth0 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同
virtual_router_id 51 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用,相同的 VRID 为一个组,他将决定多播的MAC地址
priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高
advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s
# nopreempt
## 设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111 #通信口令,建议生产环境设置复杂点
}
## 将 track_script 块加入 instance 配置块
track_script {
check_nginx #脚本监测
}
notify_backup /etc/keepalived/sendmail.pl #运行perl脚本,邮件通知(脚本参见下一篇文章)
# 虚拟 IP 池, 两个节点设置必须一样 ,可以定义多个虚拟ip
virtual_ipaddress {
192.168.1.120
#192.168.1.121
}
}
(2)Backup配置(ip:192.168.1.76)
vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx_backup
}
vrrp_script check_nginx {
script "/usr/local/keepalived/scripts/check_nginx.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
notify_master /etc/keepalived/sendmail.pl
virtual_ipaddress {
192.168.1.120
}
}
6、启动、停止
chkconfig keepalived on #开机自启
service keepalived start #启动
service keepalived stop #停止
service keepalived restart #重启
service keepalived status #查看状态
7、进行测试
分别启动两台机的nginx和keepalived,nginx和keepalived都启动后
在192.168.1.77、192.168.1.76上,ip addr查看 ,可以看到虚拟ip:192.168.1.120绑定在master 192.168.1.77上
停掉192.168.1.77上的keepalived,可以看到,192.168.1.77上的虚拟ip没了,虚拟ip绑定在192.168.1.76上了
再次启动192.168.1.77上的keepalived,可以看到虚拟ip有绑定到了192.168.1.77上,192.168.1.76的虚拟ip不见了
至此完成了nginx主从配置的自动切换,具备了HA性能。
访问地址为http://虚拟ip+端口号:如http://192.168.1.120:80。