In the first two articles, our team keepalived did the relevant introduction, the environment is still the same as before, this time we mainly introduce the Vrrp_script module, in the last introduction of keepalived basic ha function used vrrp_script this module, This module is specifically designed to monitor the service itself in the cluster, with the Track_script module, which can refer to monitoring scripts, command combinations, shell statements, and so on to enable monitoring of the service ports in many ways, Track_ Script module is mainly used to call the Vrrp_script module to enable keepalived to perform the inspection of Cluster service resources, the following is a brief introduction of the Vrrp_script module manufacturers of several detection mechanisms, as to which monitoring aspects, depending on the actual application of the environment
1. Probe service running status through Killall command
This way of monitoring the Cluster service is mainly implemented through the Killall command, Killall will send a signal to the running process of the specified command, killall the available signal names are many, such as 9 signal to force interrupt a program to run, the signal used here is 0, A signal code of 0 does not mean to close a program, but to monitor the running state of the program (process), if a process shutdown or other exception is found, will return status code 1, conversely, if the process is found to be healthy, will return a status code 0
Vrrp_script module is the use of this feature of the Killall command, in disguise to realize the service running state monitoring, in our previous configuration file
Vrrp_script CHECK_HTTPD {
Script "killall-0 httpd"
Interval 2
}
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/21/wKiom1eav4PQ-IpEAAAOs_IjTic332.png "title=" 1.png " alt= "Wkiom1eav4pq-ipeaaaos_ijtic332.png"/>
Here through echo $? The method shows the return status code of the above command, the HTTPD service is running normally, so the status return code is 0, at this time the CHECK_HTTPD module will return the normal prompt for service detection, then the HTTPD service is closed, and the result is as follows
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/21/wKiom1eawBmyaa4NAAAjXmYcM7o476.png "title=" 2.png " alt= "Wkiom1eawbmyaa4naaajxmycm7o476.png"/>
Because the httpd service is turned off, the status return code is 1, and the CHECK_HTTPD module returns a hint that the service failed to detect
From this process, we can see that the Vrrp_script module is not really concerned about how the monitoring script or monitoring command is implemented, it only through the monitoring script return status code to identify whether the Cluster service is normal, if the status return code is 0, then the service is considered normal, if the status return code is 1, it is considered a service barrier, Once you understand this principle, you can write a custom monitoring script just by following this principle.
2, the detection port operation status
Script "</DEV/TCP/127.0.0.1/80"
3. Monitoring through Shell statements
Script "If [-f/var/run/httpd/httpd.pid];then exit 0;else exit 1;fi"
4, through the script for service status monitoring
This is the most common way of monitoring, here we still have to look at the previous configuration file
[[email protected] keepalived]# cat keepalived.confglobal_defs { notification_email { [email protected] [ email protected] [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id http_master}vrrp_script check_httpd { script "/etc/keepalived/check_httpd.sh" interval 2}vrrp_instance HA_1 { state master interface eth0 virtual_router_id 80 priority 100 advert_int 2 authentication { auth_type pass auth_pass 1111 }notify_master "/etc/keepalived/master.sh" notify_backup " /etc/keepalived/backup.sh "notify_fault "/etc/keepalived/fault.sh "Track_script { check_ httpd} virtual_ipaddress { 172.16.80.100 }}
/etc/keepalived/check_httpd.sh script content is as follows [[email protected] keepalived]# Cat check_httpd.sh script requires execute permission #!/bin/baship= ' Ifconfig|grep Bcast|head-1|awk ' {print $} ' |awk-f: ' {print $ {} ' http_status= ' curl-i-S $IP |head-1|wc-l ' if [$HTTP _sta TUS-GT 0];then httpd=0else httpd=1fiecho $HTTPDexit $HTTPD
So let's take the actual test and stop the primary node httpd service to see if the script can monitor and automatically switch.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/22/wKioL1eayfKTzKBfAAAPJvTjzNc116.png "title=" 1.png " alt= "Wkiol1eayfktzkbfaaapjvtjznc116.png"/>
Master Node
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/22/wKiom1eaygngYgMkAAA1KSytqug368.png "title=" 2.png " alt= "Wkiom1eaygngygmkaaa1ksytqug368.png"/>
Alternate node
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/22/wKioL1eayiaDCDAIAABNkfVBJP4607.png "title=" 3.png " alt= "Wkiol1eayiadcdaiaabnkfvbjp4607.png"/>
Turn on the primary node httpd service again
Master Node
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/22/wKioL1eayk6hbd-nAABhueQsENY909.png "title=" 4.png " alt= "Wkiol1eayk6hbd-naabhueqseny909.png"/>
Alternate node
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/22/wKiom1eaymHQLNy2AAArhZk5AoY755.png "title=" 5.png " alt= "Wkiom1eaymhqlny2aaarhzk5aoy755.png"/>
You can see that everything fits the expected effect.
This article from "Thick tak" blog, declined reprint!
HA cluster software keepalived detailed 3