change_state.sh当检测到服务器的角色转换时,这个脚本就会调用钉钉报警,并且调整业务上的一些操作 。如 sysctl.conf 配置或者 iptables 上的规则,甚至可以配合 LVS 做一些负载均衡的部署 。
#!/bin/bashHOME="/etc/keepalived/"LIP=`/sbin/ip addr | awk '/192.168./{gsub("/.*","");if($2!=""){print $2}}'|sort -u|head -n1`VIP=$(awk '/virtual_ipaddress/{getline; print $1}' $HOME/keepalived.conf)URL="https://oapi.dingtalk.com/robot/send?access_token=07xxxxxxxxxxxxx"[ -z $LIP ] && LIP=$VIP############################################################################dingding(){curl $URL --connect-timeout 10 -H 'Content-Type: application/json' \-d '{"msgtype": "markdown","markdown": {"title": "数据中心报警","text": "* 报警类别: '"$1"'\n* 报警机器: '"$2"'\n* 报警服务: '"$3"'\n* 报警内容: '"$4"'\n* 报警时间: '"$(date "+%Y-%m-%d %T")"'\n"}}'}ENDSTATE=$3NAME=$2TYPE=$1dingding Keepalived $LIP Change_state "$ENDSTATE"case $ENDSTATE in"BACKUP") # Perform action for transition to BACKUP stateecho "--- I am$ENDSTATE #`date`" >> /tmp/keepalived.logsed -r -i '/state/s#MASTER#BACKUP#g' $HOME/keepalived.confsysctl -w \net.ipv4.conf.all.arp_accept=1 \net.ipv4.conf.all.arp_ignore=0 \net.ipv4.conf.all.arp_announce=0\net.ipv4.ip_nonlocal_bind=1#$HOME/tunl startexit 0;;"FAULT")# Perform action for transition to FAULT stateexit 0;;"MASTER") # Perform action for transition to MASTER stateecho "+++ I am$ENDSTATE #`date`" >> /tmp/keepalived.logsed -r -i '/state/s#BACKUP#MASTER#g' $HOME/keepalived.confsysctl -w \net.ipv4.conf.all.arp_ignore=1 \net.ipv4.conf.all.arp_accept=1 \net.ipv4.conf.all.arp_announce=1\net.ipv4.ip_nonlocal_bind=1iptables -L -vn | grep -iqE "vrrp|112"[ $? = 0 ] || iptables -I INPUT -p vrrp -j ACCEPTiptables -L -vn | grep -iq "accept .*$VIP"[ $? = 0 ] || iptables -I INPUT -d $VIP -j ACCEPTexit 0;;*)echo "Unknown state ${ENDSTATE} for VRRP ${TYPE} ${NAME}"exit 1;;esac
今天的分享就到这了,大家可以扫描下方二维码加又小拍好友,期望与你有更多交流 。
推荐阅读详解 SSL(三):SSL 证书该如何选择?
红利风口下,企业出海如何强势突围?
【又拍云之 Keepalived 高可用部署】
推荐阅读
- 许褚怎么牺牲的(三国名将许褚之死)
- 我的Vue之旅 10 Gin重写后端、实现页面详情页 Mysql + Golang + Gin
- 无期迷途噩兆之种怎么玩
- 红警尤里怎么玩(红警2共和国之辉尤里怎么玩)
- 1 java安全之CC1浅学
- zk系列三:zookeeper实战之分布式锁实现
- 云顶之弈8月11日12.15版本更新维护公告
- 王者荣耀荣耀之战荣耀值获取方法介绍
- 王者荣耀2022荣耀之战活动皮肤抽奖概率一览
- 10 ClickHouseClickHouse合并树MergeTree家族表引擎之ReplacingMergeTree详细解析