Call the script parameter meaning
vrrp_script <SCRIPT_NAME> {# define a detection script, disposed outside global_defs Script <STRING> | <the QUOTED-STRING> # the shell command or script path interval <INTEGER> # interval, in seconds, 1 second default timeout <INTEGER > # timeout weight <INTEGER: -254..254> # weights will monitor the implementation of the weights after the failure of the + operator fall <INTEGER> # script failed to convert several failures rise <INTEGER> # after the results of continuous detection script, the server the number of failures for a successful tag from user USERNAME [GROUPNAME] # monitoring execution of the user or group init_fail # set the default state marked as failed, and then converted to successfully monitor after successful status }
Real one: to achieve high availability ping the gateway address detection
1, the host A configuration keepalived call ping script.
Writing a script, ping the IP address of the gateway host, if the ping fails, start the following keepalived configuration file contents.
vim /etc/keepalived/ping.sh
#!/bin/bash ping -c 192.168.37.2 &> /dev/null if [ $? -eq 0 ];then exit 0 else exit 2 fi
Plus execute permissions: chmod + x ping.sh
Keepalived configuration file, when more than ping script fails, the following configuration file is executed, then the weight will be minus 50, it will reduce the priority, then it will drift to the VIP address from the server.
vim /etc/keepalived/keepalived.conf
{global_defs notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 the router_id of the KA2 vrrp_skip_check_adv_addr vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script linux_ping named {scripts are invoked script /etc/keepalived/ping.sh call script path 2 second interval time interval 2 weight -50 weight minus 50 Fall 3 into three consecutive failures failure after successful detection of Rise 5 consecutive 5 times, successfully labeled timeout 2 2 seconds timeout } vrrp_instance VIP_1 { State the MASTER interface ens33 50 virtual_router_id priority 100 unicast_src_ip 192.168.37.7 unicast_peer { 192.168.37.17 } advert_int 2 authentication { AUTH_TYPE the PASS AUTH_PASS 123456 } virtual_ipaddress { 192.168.37.100 dev ens33 label ens33:. 1 } track_script { linux_ping examination script, the above named after call } }
2, the host configuration B keepalived
Writing a script, ping the IP address of the gateway host, if the ping fails, start the following keepalived configuration file contents.
vim /etc/keepalived/ping.sh
#!/bin/bash ping -c 192.168.37.2 &> /dev/null if [ $? -eq 0 ];then exit 0 else exit 2 fi
Plus execute permissions: chmod + x ping.sh
vim /etc/keepalived/keepalived.conf
{global_defs notification_email { the root @ localhost } notification_email_from the root @ localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 the router_id of the KA1 vrrp_skip_check_adv_addr vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script linux_ping { Script /etc/keepalived/ping.sh ping scripts also call, when the barrier from the gateway server Ping when the weight reduction is also 50, the priority becomes low at this time, when the primary server, it will drift to the VIP address of the primary server. 2 interval The weight -50 Fall. 3 Rise. 5 timeout 2 } vrrp_instance VIP_1 { State the BACKUP interface ens33 virtual_router_id 50 priority 80 unicast_src_ip 192.168.37.17 unicast_peer { 192.168.37.7 } advert_int 2 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.37.100 dev ens33 label ens33:1 } track_script { linux_ping } }
3, test results:
The gateway address ping.sh script changes the IP address does not exist.
vim /etc/keepalived/ping.sh
#!/bin/bash #ping -c 192.168.37.2 &> /dev/null ping -c 192.168.77.2 &> /dev/null if [ $? -eq 0 ];then exit 0 else exit 2 fi
Restart the master-slave keepalived service: systemctl reload keepalived
At this point you can see has drifted to the VIP address from the server.
When the IP address of the primary gateway server correctly modified, VIP address it will float back to the primary server.
Combat II: HAProxy achieve high availability detection
1, the configuration in keepalived A host and write a script to detect haproxy
vim /etc/keepalived/chk_haproxy.sh
#!/bin/bash # #******************************************************************** #Author: liu #QQ: 29308620 #Date: 2019-12-26 #FileName: /etc/keepalived/chk_haproxy.sh #URL: http://www.struggle.com #Description: The test script #Copyright (C): 2019 All rights reserved #******************************************************************** killall -0 haproxy
Keepalived modify the configuration file, call detection haproxy script.
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka2 vrrp_skip_check_adv_addr vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_haproxy { script /etc/keepalived/chk_haproxy.sh 调用chk_haproxy.sh脚本 interval 2 weight -50 fall 3 rise 5 timeout 2 } vrrp_instance VIP_1 { state MASTER interface ens33 virtual_router_id 50 100 priority unicast_src_ip 192.168.37.7 unicast_peer { 192.168.37.17 } advert_int 2 authentication { AUTH_TYPE the PASS AUTH_PASS 123456 } virtual_ipaddress { 192.168.37.100 dev ens33 label ens33:. 1 } track_script { chk_haproxy above calling script call name } }
A host configuration file haproxy
global maxconn 100000 chroot /usr/local/haproxy stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2 user haproxy group haproxy daemon nbproc 2 cpu-map 1 0 cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats bind :9527 stats enable stats hide-version stats uri /haproxy-status stats realm HAPorxy\Stats\Page stats auth haadmin:123456 stats auth admin:123456 stats refresh 30s stats admin if TRUE listen web_port bind 0.0.0.0:80 mode http log global server web1 127.0.0.1:80 check inter 3000 fall 2 rise 5
2, B-host configuration keepalived, and write a script to detect haproxy
vim /etc/keepalived/chk_haproxy.sh
#! / bin / bash # # ****************************************** ************************** #Author: liu # QQ: 29.30862 million #Date: 2019-12-26 #filename: / etc / keepalived / chk_haproxy.sh #URL: http://www.struggle.com #description: the Test Script at The #Copyright (C): 2019 All Rights Reserved # ****************** ************************************************** killall -0 haproxy signal to haproxy, determining status information on the haproxy.
Keepalived modify the configuration file, call detection haproxy script.
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka2 vrrp_skip_check_adv_addr vrrp_iptables vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_haproxy { script /etc/keepalived/chk_haproxy.sh 调用chk_haproxy.sh脚本 interval 2 weight -50 fall 3 rise 5 timeout 2 } vrrp_instance VIP_1 { state BACKUP interface ens33 virtual_router_id 50 100 priority unicast_src_ip 192.168.37.17 unicast_peer { 192.168.37.7 } advert_int 2 authentication { AUTH_TYPE the PASS AUTH_PASS 123456 } virtual_ipaddress { 192.168.37.100 dev ens33 label ens33:. 1 } track_script { chk_haproxy above calling script call name } }
B host configuration file haproxy
global maxconn 100000 chroot /usr/local/haproxy stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2 user haproxy group haproxy daemon nbproc 2 cpu-map 1 0 cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats bind :9527 stats enable stats hide-version stats uri /haproxy-status stats realm HAPorxy\Stats\Page stats auth haadmin:123456 stats auth admin:123456 stats refresh 30s stats admin if TRUE listen web_port bind 0.0.0.0:80 mode http log global server web1 127.0.0.1:80 check inter 3000 fall 2 rise 5
3, test results:
A server will host haproxy stopped, then it will drift to the VIP address from the server.
When starting haproxy server, VIP address it will drift back to the primary server
If you want to achieve high availability nginx testing, only need to read the script : killall -0 nginx;
This script can then call keepalived profile.