通过keepalived监控多个应用。通过配置监控两个应用,配置如下。
203配置
! Configuration File for keepalived
global_defs {
notification_email { ######定义接受邮件的邮箱
18507371564@163.com
}
notification_email_from 18507371564@163.com ######定义发送邮件的邮箱
smtp_server mail.tuge.com
smtp_connect_timeout 10
}
vrrp_script check_mysql { ######定义监控mysql的脚本
script “/root/check_mysql.sh”
interval 2 ######监控时间间隔
weight 2 ######负载参数
}
vrrp_instance vrrptest { ######定义vrrptest实例
state BACKUP ######服务器状态
interface eth0 ######使用的接口
virtual_router_id 51 ######虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
priority 150 ######服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS ######认证类型
auth_pass ufsoft ######认证密码,一组lvs 服务器的认证密码必须一致
}
track_script { ######执行监控mysql进程的脚本
check_mysql
}
virtual_ipaddress { ######虚拟IP地址
10.230.124.197
}
}
vrrp_script chk_redis {
script “/etc/keepalived/script/redis_check.sh”
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
chk_redis
}
virtual_ipaddress {
10.230.124.197
}
}
virtual_server 10.230.124.197 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
protocol TCP
real_server 10.230.124.203
{
weight 1
}
}
unicast_src_ip 10.230.124.203
unicast_peer {
10.230.124.202
}
202配置
! Configuration File for keepalived
global_defs {
notification_email { ######定义接受邮件的邮箱
18507371564@163.com
}
notification_email_from 18507371564@163.com ######定义发送邮件的邮箱
smtp_server mail.tuge.com
smtp_connect_timeout 10
}
vrrp_script check_mysql { ######定义监控mysql的脚本
script “/root/check_mysql.sh”
interval 2 ######监控时间间隔
weight 2 ######负载参数
}
vrrp_instance vrrptest { ######定义vrrptest实例
state BACKUP ######服务器状态
interface eth0 ######使用的接口
virtual_router_id 51 ######虚拟路由的标志,一组lvs的虚拟路由标识必须相同,这样才能切换
priority 150 ######服务启动优先级,值越大,优先级越高,BACKUP 不能大于MASTER
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS ######认证类型
auth_pass ufsoft ######认证密码,一组lvs 服务器的认证密码必须一致
}
track_script { ######执行监控mysql进程的脚本
check_mysql
}
virtual_ipaddress { ######虚拟IP地址
10.230.124.197
}
}
vrrp_script chk_redis {
script “/etc/keepalived/script/redis_check.sh”
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
chk_redis
}
virtual_ipaddress {
10.230.124.197
}
}
virtual_server 10.230.124.197 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
protocol TCP
real_server 10.230.124.202
{
weight 1
}
}
unicast_src_ip 10.230.124.202
unicast_peer {
10.230.124.203
}
监控配置:
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=root
$MYSQL -h $MYSQL_HOST -u
M
Y
S
Q
L
U
S
E
R
−
p
MYSQL_USER -p
MYSQLUSER−pMYSQL_PASSWORD -e “show status;” >/dev/null 2>&1
#
m
y
s
q
l
c
l
i
e
n
t
−
−
h
o
s
t
=
mysqlclient --host=
mysqlclient−−host=host --port=
p
o
r
t
−
−
u
s
e
r
=
port --user=
port−−user=user --password=$password -e “show databases;” > /dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
#echo " $host mysql login faild"
/etc/init.d/keepalived stop
exit 2
fi