1、在与其他服务一起运行下做高可用时,比如NGINX,需要在NGINX断开的时候先尝试启动一次NGINX,如果启动不行,漂移keepalived到备机,所以需要在配置下加检测脚本。
2、配置:keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@163.com
} notification_email_from abc@163.com
smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/data/sh/check_nginx.sh"
interval 2
weight 20
} vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_typePASS
auth_pass 1111
} virtual_ipaddress {
192.168.2.129
} track_script {
chk_nginx
}
}
3、检测脚本:chk_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
4、配置参数详解:
global_defs模块:
notification_email 当切换的时候发送通知对象,一般不需要
notification_email_from 发件人,一般不需要
smtp_server smtp服务地址,一般不需要
smtp_connect_timeout smtp连接超时时间,一般不需要
router_id 运行keepalived的标识,一般为主机名
vrrp_script模块:
script 脚本路径
interval 检测时间间隔
weight 权重
vrrp_instance模块:
state 主机状态,包括MASTER和BACKUP
virtual_router 路由id,主备要一致
priority 优先级,值越高,主机状态就是MASTER
advert_int 检查间隔
nopreempt 不抢占
authentication 设置认证
auth_type 认证方式
auth_pass 认证密码
virtual_ipaddress 设置VIP
track_script 脚本监控,名称为vrrp_script设置
5、查看本机IP信息:ip addr list