Cron的特殊性,在双机热备集群中,只有一台是提供服务的。当CronJobA故障时,停掉cron服务,CronJobB启动cron并接替CronJobA继续服务。CronJobA在恢复后,Keepalived不应该切回去。
所以我们要在配置文件加入 nopreempt 非抢占,但是这个参数只能用于state 为backup,故我们在用Keepalived的时候最好master 和backup的state都设置成backup 让其通过priority来竞争。
keepalived在线安装:
yum -y install keepalived ipvsadm
在Master(CronJobA)配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id cronha
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 101
advert_int 1
#防止抢占资源
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
#指定当切换到master时,执行的脚本
notify_master /etc/keepalived/scripts/cronstart
#指定当切换到backup时,执行的脚本
notify_backup /etc/keepalived/scripts/cronstop
virtual_ipaddress {
172.88.0.254
}
}
在Backup(CronJobB)配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id cronha
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
notify_master /etc/keepalived/scripts/cronstart
notify_backup /etc/keepalived/scripts/cronstop
virtual_ipaddress {
172.88.0.254
}
}
- 做为Master和Backup时需要的脚本
#创建目录
mkdir /etc/keepalived/scripts
#启动cron脚本
vim /etc/keepalived/scripts/cronstart
#!/bin/bash
/etc/init.d/crond start
#微信告知切换
/etc/keepalived/scripts/weixin
#添加执行权限
chmod +x /etc/keepalived/scripts/cronstart
#关闭cron脚本
vim /etc/keepalived/scripts/cronstop
#!/bin/bash
/etc/init.d/crond stop
#添加执行权限
chmod +x /etc/keepalived/scripts/cronstop
在两台CronJob上配置
- 关闭cron自启动服务
chkconfig crond off
- 启动Keepalived并加入开机启动:
/etc/init.d/keepalived restart
chkconfig keepalived on