keepalived调用脚本进行资源监控

keepalived调用脚本进行资源监控
keepalived调用外部的辅助脚本进行资源监控,并根据监控的结果状态能实现优先动态调整
vrrp_script:自定义资源监控脚本,vrrp实例根据脚本返回值,公共定义,可被多个实例调用,定义在vrrp实例之外的独立配置块,一般放在global_defs设置块之后。
通常此脚本用于监控指定应用的状态。一旦发现应用的状态异常,则触发对MASTER节点的权重减至低于SLAVE节点,从而实现 VIP 切换到 SLAVE 节点

分两步:(1) 先定义一个脚本;(2) 调用此脚本
vrrp_script SCRIPT_NAME {  # 定义用于实例执行的脚本内容,比如可以在线降低优先级,用于强制切换,定义在与vrrp_instance平级。
		script <STRING>|<QUOTED-STRING> 	# shell命令或脚本路径,此脚本返回值为非0时(假),会触发下面OPTIONS执行
		interval <INTEGER> 					# 间隔时间,单位为秒,默认1秒
		timeout <INTEGER> 					# 超时时间
		weight <INTEGER:-254..254> 			# 权重,监测失败后会执行权重相加,权重可以为负数即相加后降低本机权重
		fall <INTEGER> 						# 脚本几次失败转换为失败
		rise <INTEGER> 						# 脚本连续监测成功后,把服务器从失败标记为成功的次数
		user USERNAME [GROUPNAME]			# 执行监测的用户或组
		init_fail 							# 设置默认标记为失败状态,监测成功之后再转换为成功状态
   }			
track_script {           #追踪脚本,通常用于去执行上面的vrrp_script定义的脚本内容,定义在vrrp_instance里。
	SCRIPT_NAME 
	SCRIPT_NAME
}     


 实验架构接上一个实验的:https://www.cnblogs.com/Yuanbangchen/p/16550016.html

[root@localhost7A ~]# cat  /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
     348987564@qq.com   
   }
   notification_email_from root@localhost  
   smtp_server 127.0.0.1  
   smtp_connect_timeout 30
   router_id localhost7A
   vrrp_iptables
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.100.100
}
vrrp_script chk_down {   #定义脚本       说明:两台KA都需要/etc/keepalived/down,建议使用网络挂载方式这个文件夹,也叫仲裁盘。  
	script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0" # down存在时返回非0,触发权重-30
	interval 1
    weight -30
    fall 3
    rise 2
    timeout 2
}
vrrp_script chk_nginx { #定义脚本
	script “/usr/bin/killall -0 nginx "  #0表示检查进程是否运行。
	interval 1
	weight -20  #减后要小于backup中priority的值。
	fall 2 
	rise 1 
}
vrrp_instance zzhz {
    state MASTER
    interface eth0
    virtual_router_id 88 
    priority 88        
    advert_int 2         
    authentication {
        auth_type PASS
        auth_pass centos 
    }
    virtual_ipaddress {
        192.168.80.222/24 dev eth0 label eth0:1
    }
    track_script {  
	chk_down
	chk_nginx  #调用此脚本
	}
}
virtual_server 192.168.80.222  80 {
	delay_loop 6
        lb_algo rr
        lb_kind DR
        protocol TCP
        sorry_server 127.0.0.1 80

	real_server 192.168.80.120 80 {
        	weight 1
        	HTTP_GET {
        		url {
                		path /
                		status_code 200
		 	 }
       		connect_timeout 3
        	nb_get_retry 3
        	delay_before_retry 3
   		}		
	}	
	real_server 192.168.80.130 80 {
        	weight 1
        	HTTP_CHECK {
			connect_ip 192.168.80.130
            connect_port 80
            bindto 192.168.80.100
            bind_port 7777
        	connect_timeout 3
       		 nb_get_retry 3
        	delay_before_retry 3
  		 }
	 }
}	

  

测试:
创建文件/etc/keepalived/down

16:23:04.720866 IP 192.168.80.100 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 65, authtype simple, intvl 2s, length 20
16:23:04.721430 IP 192.168.80.110 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 85, authtype simple, intvl 2s, length 20

tail -f /var/log/messages
Aug 4 16:39:54 localhost7A avahi-daemon[934]: Received response from host 192.168.80.1 with invalid source port 64517 on interface 'eth0.0'
Aug 4 16:39:54 localhost7A Keepalived_vrrp[30583]: /bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0 exited with status 1
Aug 4 16:39:55 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) Changing effective priority from 95 to 65
Aug 4 16:39:55 localhost7A Keepalived_vrrp[30583]: /bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0 exited with status 1
Aug 4 16:39:56 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) Received advert with higher priority 85, ours 65
Aug 4 16:39:56 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) Entering BACKUP STATE
Aug 4 16:39:56 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) removing protocol VIPs.
Aug 4 16:39:56 localhost7A avahi-daemon[934]: Withdrawing address record for 192.168.80.222 on eth0.
Aug 4 16:39:56 localhost7A Keepalived_vrrp[30583]: /bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0 exited with status 1
Aug 4 16:39:57 localhost7A Keepalived_vrrp[30583]: /bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0 exited with status 1
Aug 4 16:40:01 localhost7A systemd: Started Session 155 of user root.
Aug 4 16:40:01 localhost7A systemd: Starting Session 155 of user root.
Aug 4 16:40:02 localhost7A Keepalived_vrrp[30583]: VRRP_Script(chk_down) succeeded
Aug 4 16:40:03 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) Changing effective priority from 65 to 95
Aug 4 16:40:04 localhost7A Keepalived_vrrp[30583]: VRRP_Instance(zzhz) forcing a new MASTER election

  

  

posted @ 2022-08-04 16:32  yuanbangchen  阅读(733)  评论(0编辑  收藏  举报