Keepalived集群软件高级使用(工作原理和状态通知)
1、介绍
Keeaplived主要有两种应用场景,一个是通过配置keepalived结合ipvs做到负载均衡(LVS+Keepalived),有此需求者可参考以往博文:http://lizhenliang.blog.51cto.com/7876557/1343734。另一个是通过自身健康检查、资源接管功能做高可用(双机热备),实现故障转移。
以下内容主要针对Keepalived+MySQL双主实现双机热备为根据,主要讲解keepalived的状态转换通知功能,利用此功能可有效加强对MySQL数据库监控。此文不再讲述Keepalived+MySQL双主部署过程,有需求者可参考以往博文:http://lizhenliang.blog.51cto.com/7876557/1362313
2、keepalived主要作用
keepalived采用VRRP(virtual router redundancy protocol),虚拟路由冗余协议,以软件的形式实现服务器热备功能。通常情况下是将两台linux服务器组成一个热备组(master-backup),同一时间热备组内只有一台主服务器(master)提供服务,同时master会虚拟出一个共用IP地址(VIP),这个VIP只存在master上并对外提供服务。如果keepalived检测到master宕机或服务故障,备服务器(backup)会自动接管VIP成为master,keepalived并将master从热备组移除,当master恢复后,会自动加入到热备组,默认再抢占成为master,起到故障转移功能。
3、工作在三层、四层和七层原理
Layer3:工作在三层时,keepalived会定期向热备组中的服务器发送一个ICMP数据包,来判断某台服务器是否故障,如果故障则将这台服务器从热备组移除。
Layer4:工作在四层时,keepalived以TCP端口的状态判断服务器是否故障,比如检测mysql 3306端口,如果故障则将这台服务器从热备组移除。
示例:
! Configuration File for keepalived
global_defs {
notification_email {
example@.com
}
notification_email_from example@example.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id
nopreempt #当主down时,备接管,主恢复,不自动接管
priority
advert_int
authentication {
auth_type PASS
ahth_pass
}
virtual_ipaddress {
192.168.1.200 #虚拟IP地址
}
}
virtual_server 192.168.1.200 {
delay_loop
# lb_algo rr
# lb_kind NAT
persistence_timeout
protocol TCP
real_server 192.168.1.201 { #监控本机3306端口
weight
notify_down /etc/keepalived/kill_keepalived.sh #检测3306端口为down状态就执行此脚本(只有keepalived关闭,VIP才漂移 )
TCP_CHECK { #健康状态检测方式,可针对业务需求调整(TTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK)
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
Layer7:工作在七层时,keepalived根据用户设定的策略判断服务器上的程序是否正常运行,如果故障则将这台服务器从热备组移除。
示例:
! Configuration File for keepalived
global_defs {
notification_email {
example@.com
}
notification_email_from example@example.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id MYSQL_HA
}
vrrp_script check_nginx {
script /etc/keepalived/check_nginx.sh #检测脚本
interval #执行间隔时间
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id
nopreempt #当主down时,备接管,主恢复,不自动接管
priority
advert_int
authentication {
auth_type PASS
ahth_pass
}
virtual_ipaddress {
192.168.1.200 #虚拟IP地址
}
track_script { #在实例中引用脚本
check_nginx
}
}
脚本内容如下:
# cat /etc/keepalived/check_nginx.sh
Count1=`netstat -antp |grep -v grep |grep nginx |wc -l`
if [ $Count1 -eq ]; then
/usr/local/nginx/sbin/nginx
sleep
Count2=`netstat -antp |grep -v grep |grep nginx |wc -l`
if [ $Count2 -eq ]; then
service keepalived stop
else
exit
fi
else
exit
fi
4、健康状态检测方式
4.1 HTTP服务状态检测
HTTP_GET或SSL_GET {
url {
path /index.html #检测url,可写多个
digest 24326582a86bee478bac72d5af25089e #检测效验码
#digest效验码获取方法:genhash -s IP -p -u http://IP/index.html
status_code #检测返回http状态码
}
connect_port #连接端口
connect_timeout #连接超时时间
nb_get_retry #重试次数
delay_before_retry #连接间隔时间
}
4.2 TCP端口状态检测(使用TCP端口服务基本上都可以使用)
TCP_CHECK {
connect_port #健康检测端口,默认为real_server后跟端口
connect_timeout
nb_get_retry
delay_before_retry
}
4.3 邮件服务器SMTP检测
SMTP_CHECK { #健康检测邮件服务器smtp
host {
connect_ip
connect_port
}
connect_timeout
retry
delay_before_retry
hello_name "mail.domain.com"
}
4.4 用户自定义脚本检测real_server服务状态
MISC_CHECK {
misc_path /script.sh #指定外部程序或脚本位置
misc_timeout #执行脚本超时时间
!misc_dynamic #不动态调整服务器权重(weight),如果启用将通过退出状态码动态调整real_server权重值
}
5、状态转换通知功能
keepalived主配置邮件通知功能,默认当real_server宕机或者恢复时才会发出邮件。有时我们更想知道keepalived的主服务器故障切换后,VIP是否顺利漂移到备服务器,MySQL服务器是否正常?那写个监控脚本吧,可以,但没必要,因为keepalived具备状态检测功能,所以我们直接使用就行了。
主配置默认邮件通知配置模板如下:
global_defs # Block id
{
notification_email # To:
{
admin@example1.com
...
}
# From: from address that will be in header
notification_email_from admin@example.com
smtp_server 127.0.0.1 # IP
smtp_connect_timeout # integer, seconds
router_id my_hostname # string identifying the machine,
# (doesn't have to be hostname).
enable_traps # enable SNMP traps
}
5.1 实例状态通知
a) notify_master :节点变为master时执行
b) notify_backup : 节点变为backup时执行
c) notify_fault : 节点变为故障时执行
5.2 虚拟服务器检测通知
a) notify_up : 虚拟服务器up时执行
b) notify_down : 虚拟服务器down时执行
示例:
! Configuration File for keepalived
global_defs {
notification_email {
example@.com
}
notification_email_from example@example.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id MYSQL_HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id
nopreempt #当主down时,备接管,主恢复,不自动接管
priority
advert_int
authentication {
auth_type PASS
ahth_pass
}
virtual_ipaddress {
192.168.1.200
}
notify_master /etc/keepalived/to_master.sh
notify_backup /etc/keepalived/to_backup.sh
notify_fault /etc/keepalived/to_fault.sh
}
virtual_server 192.168.1.200 {
delay_loop
persistence_timeout
protocol TCP
real_server 192.168.1.201 {
weight
notify_up /etc/keepalived/mysql_up.sh
notify_down /etc/keepalived/mysql_down.sh
TCP_CHECK {
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
状态参数后可以是bash命令,也可以是shell脚本,内容根据自己需求定义,以上示例中所涉及状态脚本如下:
1) 当服务器改变为主时执行此脚本
# cat to_master.sh
#!/bin/bash
Date=$(date +%F" "%T)
IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}')
Mail="baojingtongzhi@163.com"
echo "$Date $IP change to master." |mail -s "Master-Backup Change Status" $Mail
2) 当服务器改变为备时执行此脚本
# cat to_backup.sh
#!/bin/bash
Date=$(date +%F" "%T)
IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}')
Mail="baojingtongzhi@163.com"
echo "$Date $IP change to backup." |mail -s "Master-Backup Change Status" $Mail
3) 当服务器改变为故障时执行此脚本
# cat to_fault.sh
#!/bin/bash
Date=$(date +%F" "%T)
IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}')
Mail="baojingtongzhi@163.com"
echo "$Date $IP change to fault." |mail -s "Master-Backup Change Status" $Mail
4) 当检测TCP端口3306为不可用时,执行此脚本,杀死keepalived,实现切换
# cat mysql_down.sh
#!/bin/bash
Date=$(date +%F" "%T)
IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}')
Mail="baojingtongzhi@163.com"
pkill keepalived
echo "$Date $IP The mysql service failure,kill keepalived." |mail -s "Master-Backup MySQL Monitor" $Mail
5) 当检测TCP端口3306可用时,执行此脚本
# cat mysql_up.sh
#!/bin/bash
Date=$(date +%F" "%T)
IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}')
Mail="baojingtongzhi@163.com"
echo "$Date $IP The mysql service is recovery." |mail -s "Master-Backup MySQL Monitor" $Mail
Keepalived集群软件高级使用(工作原理和状态通知)的更多相关文章
- CentOS 6.5环境下heartbeat高可用集群的实现及工作原理详解
Linux HA Cluster高可用服务器集群,所谓的高可用不是主机的高可用,而是服务的高可用. 什么叫高可用:一个服务器down掉的可能性多种多样,任何一个可能坏了都有可能带来风险,而服务器离线通 ...
- 高性能集群软件keepalived
Keepalived介绍 以下是keepalive官网上的介绍.官方站点为http://www.keepalived.org. Keepalived is a routing sof ...
- Nginx+Keepalived 集群方案
1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,kee ...
- Linux实战教学笔记33:lvs+keepalived集群架构服务
一,LVS功能详解 1.1 LVS(Linux Virtual Server)介绍 LVS是Linux Virtual Server 的简写(也叫做IPVS),意即Linux虚拟服务器,是一个虚拟的服 ...
- Keepalived 集群在Linux下的搭建
[概述]:Keepalived 是一个免费开源的,用C编写.主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务 ...
- lvs+keepalived集群架构服务
一,LVS功能详解 1.1 LVS(Linux Virtual Server)介绍 LVS是Linux Virtual Server 的简写(也叫做IPVS),意即Linux虚拟服务器,是一个虚拟的服 ...
- Centos7+nginx+keepalived集群及双主架构案例
目录简介 一.简介 二.部署nginx+keepalived 集群 三.部署nginx+keepalived双主架构 四.高可用之调用辅助脚本进行资源监控,并根据监控的结果状态实现动态调整 一.简介 ...
- MHA+keepalived集群环境搭建
整个MHA+keepalived集群环境搭建 1.1. 环境简介1.1.1.vmvare虚拟机,系统版本CentOS6.5 x86_64位最小化安装,mysql的版本5.7.21,1.1.2.虚拟机器 ...
- dubbo源码解析五 --- 集群容错架构设计与原理分析
欢迎来我的 Star Followers 后期后继续更新Dubbo别的文章 Dubbo 源码分析系列之一环境搭建 博客园 Dubbo 入门之二 --- 项目结构解析 博客园 Dubbo 源码分析系列之 ...
随机推荐
- HDU2482-Transit search(地图还原+SPFA)
Henry decides to develop a web site, which will provide the service of transit search. But he can on ...
- 使用Spring Boot和AspectJ实现方法跟踪基础结构
了解如何使用Spring Boot和AspectJ实现方法跟踪基础结构!最近在优锐课学习收获颇多,记录下来大家一起进步! 在我们的应用程序中,获取方法的堆栈跟踪信息可能会节省很多时间.具有输入输出参数 ...
- Django 09
目录 多对多三种创建方式 全自动 纯手撸 半自动(推荐) form组件 校验数据 渲染标签 展示错误信息 validators校验器 钩子函数 补充 多对多三种创建方式 全自动 ManyToManyF ...
- Jmeter查看结果树之查看响应的13种方法[详解]
查看结果树查看响应有哪几种方法,可通过左侧面板底部的下拉框选择 1.Text 查看结果树中请求的默认格式为text,会显示请求的取样器结果.请求.响应数据3个部分内容. 取样器结果: 默认Raw视图, ...
- Ambari 自定义服务集成原理介绍
之前,在 github 上开源了 ambari-Kylin 项目,可离线部署,支持 hdp 2.6+ 及 hdp 3.0+ .github 地址为:https://github.com/8418090 ...
- SSM-配置tkmybatis
引言 Mybatis 与 Hibernate的一个很大的区别就是Mybatis所有的数据库操作语句都需要自己写,对于简单的单表操作来说是比较烦琐的.因此有人就开发了tk.mybatis插件,通过这个插 ...
- java—将数据库读取的list转tree
一.引言 有时候我们从数据库中读取出了一个表的数据,比如存储的是中国的省市县的ID.名称与父节点ID,读出来的数据并不是前台想要的,这个时候我们要想法处理一下都出来的list,将它变为一个树. 比如直 ...
- ubifs开销测试
问题 在板子上观察到56M的ubi卷,挂载上ubifs之后,df -h显示可用空间约为50M. 如此计算开销超过了10%,那么这个开销随容量如何变化呢,是固定为10%吗还是有其他规律? 理论计算 简单 ...
- PHP7.3安装event扩展
安装支持库libevent wget https://github.com/libevent/libevent/releases/download/release-2.1.11-stable/libe ...
- 分布式事务之解决方案(XA和2PC)
3. 分布式事务解决方案之2PC(两阶段提交) 针对不同的分布式场景业界常见的解决方案有2PC.TCC.可靠消息最终一致性.最大努力通知这几种. 3.1. 什么是2PC 2PC即两阶段提交协议,是将整 ...