By default, the Keepalived software takes over the business only when the other machine is down or keepalived is stopped. But in the actual work, there are business services stopped and keepalived service is still working situation, which will lead to users access to the VIP can not find the corresponding service, then, how to solve the business service outage can be IP drift to the standby node to take over the provision of services?
The first method: You can write a daemon script to handle it. When there is a problem with nginx business, it will stop the local keepalived service and realize the P drift to the peer to continue to provide services. Sample scripts for deployment and development in real-world work are as follows:
[email protected] shell]# cat check_nginx.sh #!/bin/bash#author:mr.ding#created time:2018-10-05 15:51:54#name:check_ Nginx.sh#description:while truedoif [' Netstat-lntup|grep nginx|wc-l '-ne 1];thensystemctl stop KEEPALIVED.SERVICEFISL Eep 5done
The idea of this script is to disable the Keepalived service implementation to release the local VIP if no 80 ports exist. Perform the above script check in the background or join the scheduled task.
[[Email protected] shell]# sh check_nginx.sh &[1] 1428[[email protected] shell]# ps-ef |grep check |grep-v greproot< c0/>1428 1320 0 15:56 pts/0 00:00:00 sh check_nginx.sh
Stop the Nginx service to see if the IP has switched
[Email protected] shell]# systemctl stop nginx.service [[email protected] shell]# Netstat-lntup|grep nginx[[email Protec Ted] shell]# IP add|grep 192.168.200.16 see if the standby node has been taken over [[email protected] ~]# IP add|grep 192.168.200.16 inet 192.168.200.16/24 Scope Global Secondary ENS33
The second method: You can use the Keepalived profile parameter to trigger a well-written service detection script.
[email protected] shell]# cat chk_nginx_proxy.sh #!/bin/bash#author:mr.ding#created time:2018-10-05 16:09:27#name: chk_nginx_proxy.sh#description:if [' Netstat-lntup|grep nginx|wc-l '-ne 1];thensystemctl stop keepalived.servicefi[[ Email protected] shell]# chmod +x chk_nginx_proxy.sh [[email protected] shell]# ls-l chk_nginx_proxy.sh-rwxr-xr-x 1 root Root 192 October 5 16:10 chk_nginx_proxy.sh
In the keepalived.conf file, configure the following:
[[email protected] shell]# cat/etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { notification_email { [email protected] } notification _email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout- router_id lb01}vrrp_script Chk_nginx_proxy {#定义vrrp脚本, detects HTTP port script "/server/scripts/shell/chk_nginx_proxy.sh" #执行脚本, when the Nginx service has a problem, stop Keepalived Service Interval 2 #间隔2秒weight 2}vrrp_instance vi_1 {State MASTER Interface Ens33
virtual_router_id advert_int 1 authentication { Auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.200.16/24 dev ens33 lable ens33:1}track_script {chk_nginx_ Proxy #触发检查}}
Test takeover results
[[email protected] shell]# systemctl stop nginx[[email protected] shell]# IP add|grep 192.168.200.16 view takeover results on the standby server [email p rotected] ~]# IP add|grep 192.168.200.16 inet 192.168.200.16/24 scope Global Secondary ENS33
When the Nginx is stopped, the keepalived will automatically stop in 2 seconds, the VIP is released, by the backup side to take over, so that the service outage will also carry out IP drift, business switching.
Troubleshooting High-availability servers for physical servers only