keepalived主动切换vip,邮件通知
来源:互联网 发布:app录像软件 编辑:程序博客网 时间:2024/06/06 15:02
1.主动切换vip
通过vrrp_script判断,调节权重进行切换
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from kanotify@muuzz.com smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL } vrrp_script chk_mantaince_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功 # commadn1 && command2 只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行 # commadn1 || command2 只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。 interval 1 weight -2 #只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换}vrrp_instance VI_1 { interface eth0 state MASTER # BACKUP for slave routers priority 101 # 100 for BACKUP virtual_router_id 51 garp_master_delay 1 authentication { auth_type PASS auth_pass password } track_interface { eth0 } virtual_ipaddress { 192.168.255.100/24 } track_script { chk_mantaince_down } }
2.在状态切换时进行通知
# notify scripts and alerts are optional
#
# filenames of scripts to run on transitions
# can be unquoted (if just filename)
# or quoted (if has parameters)
# to MASTER transition
notify_master /path/to_master.sh
# to BACKUP transition
notify_backup /path/to_backup.sh
# FAULT transition
notify_fault “/path/fault.sh VG_1”
1.使用:
notify_master
notify_backup
notify_fault
可以使用在vrrp_sync_group和vrrp_instance中,较多使用在vrrp_instance中
如果脚本带有参数,需要用”“扩起:notify_fault “/path/fault.sh VG_1”
当发生状态切换时,会根据切换的状态分配调用MASTER BACKUP FAULT指定的脚本
2.使用:
notify进行通知,notify可以使用在vrrp_sync_group和vrrp_instance中
此时脚本需要接受三个参数:
$3 “MASTER”|”BACKUP”|”FAULT”
# for ANY state transition.
# “notify” script is called AFTER the
# notify_* script(s) and is executed
# with 3 arguments provided by keepalived
# (ie don’t include parameters in the notify line).
# arguments
# $1 = “GROUP”|”INSTANCE”
# $2 = name of group or instance
# $3 = target state of transition
# (“MASTER”|”BACKUP”|”FAULT”)
notify /path/notify.sh
还是notify_master, notify_backup, notify_fault 这种好用
下面是一个notify.sh脚本的简单示例:
#!/bin/bash# Author: MageEdu <linuxedu@foxmail.com># description: An example of notify script# vip=172.16.100.1contact='root@localhost'notify() { mailsubject="`hostname` to be $1: $vip floating" mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1" echo $mailbody | mail -s "$mailsubject" $contact}case "$1" in master) notify master /etc/rc.d/init.d/haproxy start exit 0 ;; backup) notify backup /etc/rc.d/init.d/haproxy stop exit 0 ;; fault) notify fault /etc/rc.d/init.d/haproxy stop exit 0 ;; *) echo 'Usage: `basename $0` {master|backup|fault}' exit 1 ;;esac
#MASTER:
##!/bin/bash
#vip=192.168.255.100
#concat=’root@localhost’
#thisis=ifconfig eno16777736|awk '/inet /{print $2}'
#
#notify() {
# mailbody=”vrrp transaction,
# subject=”
# echo
#}
#
#notify
于是配置文件变为
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from kanotify@muuzz.com smtp_connect_timeout 3 smtp_server 127.0.0.1 router_id LVS_DEVEL } vrrp_script chk_mantaince_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功 # commadn1 && command2 只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行 # commadn1 || command2 只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。 interval 1 weight -2 #只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换}vrrp_instance VI_1 { interface eth0 state MASTER # BACKUP for slave routers priority 101 # 100 for BACKUP virtual_router_id 51 garp_master_delay 1 authentication { auth_type PASS auth_pass password } track_interface { eth0 } virtual_ipaddress { 192.168.255.100/24 } track_script { chk_mantaince_down } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" #在脚本中配置邮件,或其他操作就可以了 }
- keepalived主动切换vip,邮件通知
- NLog邮件通知,让系统管理员工作更主动和高效
- keepalived双机热备实现故障时发送邮件通知
- keepalived 两边都出现VIP
- centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)
- MHA VIP切换脚本
- LVS+Keepalived 多个VIP 测试总结
- MySQL学习笔记-Keepalived VIP配置使用
- Keepalived无法绑定VIP故障排查经历
- keepalived的vip设置为公网IP
- keepalived+lvs无法访问vip或访问超时
- Heartbeat和Keepalived创建VIP的区别
- 数据库主动式通知编程
- Mysql+keepalived主从切换
- keepalived 双击切换
- keepalived+Mysql切换
- 关于会员VIP的一些通知
- 06-线程的主动切换
- 文件操作,输入输出流(stream, writer)
- 开通博客第一天
- 程序员30岁以后的发展迷途
- js弹出框、对话框、提示框、弹窗总结
- php的op_array与execute_data的关系
- keepalived主动切换vip,邮件通知
- 静态链表和循环链表
- Mysql字符集
- java 归并排序(稳定)
- POSIX信号量与互斥锁(生产者,消费者)
- replicate-rewrite-db
- 常用复制管理命令
- Ubuntu server中iptables基本配置
- java学习日志(6)回调、模板方法模式、内部类、数组及初始化