keepalived利用VRRP Script进行资源监控

一 VRRP Script设置

1.1 定义vrrp script脚本

vrrp script:自定义资源监控脚本,vrrp实例根据脚本返回值,公共定义,可被多个实例调用,定义在vrrp实例之外的独立配置模块。一般放在global_def设置块之后,是和global_defs平级的语句块。

通常此脚本用于监控指定应用的状态,一旦发现应用的状态异常,则触发对master节点的权重见至低于slave节点,从而实现VIP切换到slave节点。

vrrp_script <SCRIPT_NAME> {          #定义一个检测脚本,在global_defs之外配置
	script <string>|<quoted-string>  #shell命令或脚本路径
    interval <integer>               #间隔时间,单位为妙,默认1s
    timeout <integer>                #超时时间
	weiht <integer:-254..254>        #默认为0,如果设置此值为负数,当上面脚本返回值为非0时,会将此值与本节点权重相加可以降低节点权重,即fall.ruguo为正数,当脚本返回值为0,会将此值与本节点权重相加可以提高节点权重,即标识rise,通常使用负值。
    fall <integer>                   #执行脚本连续几次都失败,则转换为失败,建议设置2以上
    rise <integer>                   #执行脚本连续几次都成功,把服务器从失败标记为成功
    user USERNAME [GROUPNAME]        #执行检测脚本的用户或组
    init_fail                        #设置默认标记为失败状态,检测成功后再转换为成功状态
}

1.2 调用vrrp script

vrrp_instance VI_1 {
   ....
   track_script {
       <SCRIPT_NAME>
   }

}

二 利用脚本实现主从切换

2.1 修改keepalived配置文件

2.1.1 设置node-01配置文件

点击查看代码
root@node-01:~# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
1304005320@qq.com
}
notification_email_from 1304005320@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node-01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script chk_down {
script "/etc/keepalived/check_down.sh" #返回非0,触发权重-30
interval 1
weight -30
fall 3
rise 2
timeout 2
}

vrrp_instance VI_1 {
state MASTER #node-02 为BACKUP
interface ens33
virtual_router_id 101
priority 100 #node-02 为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.20
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
track_script {
chk_down
}
}

2.2 创建check_down.sh

root@node-01:~# cat /etc/keepalived/check_down.sh
#!/bin/bash

[ ! -f /etc/keepalived/down ]

2.3 验证主从切换

2.3.1 查看node-01状态

root@node-01:~# hostname -I
192.168.174.120 192.168.174.20 

2.3.2 创建down文件

root@node-01:~# touch /etc/keepalived/down

2.3.3 验证node-01状态

root@node-01:~# hostname -I
192.168.174.120 

2.3.4 查看日志

root@node-01:~# tail -f /var/log/syslog |grep  Keepalived
Nov  9 20:48:48 node-01 Keepalived_vrrp[7438]: VRRP_Script(chk_down) failed (exited with status 1)
Nov  9 20:48:48 node-01 Keepalived_vrrp[7438]: (VI_1) Changing effective priority from 100 to 70
Nov  9 20:48:51 node-01 Keepalived_vrrp[7438]: (VI_1) Master received advert from 192.168.174.121 with higher priority 80, ours 70
Nov  9 20:48:51 node-01 Keepalived_vrrp[7438]: (VI_1) Entering BACKUP STATE
Nov  9 20:48:51 node-01 Keepalived_vrrp[7438]: (VI_1) removing VIPs.

2.3.5 删除down文件

root@node-01:~# rm -rf /etc/keepalived/down

2.3.6 查看vip状态

root@node-01:~# hostname -I
192.168.174.120 192.168.174.20

2.3.7 查看VIP切换日志

root@node-01:~# tail -f /var/log/syslog |grep  Keepalived
Nov  9 20:53:10 node-01 Keepalived_vrrp[7438]: Script `chk_down` now returning 0
Nov  9 20:53:11 node-01 Keepalived_vrrp[7438]: VRRP_Script(chk_down) succeeded
Nov  9 20:53:11 node-01 Keepalived_vrrp[7438]: (VI_1) Changing effective priority from 70 to 100
Nov  9 20:53:11 node-01 Keepalived_vrrp[7438]: (VI_1) received lower priority (80) advert from 192.168.174.121 - discarding
Nov  9 20:53:13 node-01 Keepalived_vrrp[7438]: message repeated 2 times: [ (VI_1) received lower priority (80) advert from 192.168.174.121 - discarding]
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: (VI_1) Receive advertisement timeout
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: (VI_1) Entering MASTER STATE
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: (VI_1) setting VIPs.
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.174.20
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: Sending gratuitous ARP on ens33 for 192.168.174.20
Nov  9 20:53:14 node-01 Keepalived_vrrp[7438]: message repeated 4 times: [ Sending gratuitous ARP on ens33 for 192.168.174.20]
Nov  9 20:53:19 node-01 Keepalived_vrrp[7438]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.174.20
Nov  9 20:53:19 node-01 Keepalived_vrrp[7438]: Sending gratuitous ARP on ens33 for 192.168.174.20
Nov  9 20:53:19 node-01 Keepalived_vrrp[7438]: message repeated 4 times: [ Sending gratuitous ARP on ens33 for 192.168.174.20]
posted @ 2021-11-09 20:55  小吉猫  阅读(853)  评论(0编辑  收藏  举报