keepalived + nginx high-availability load balancing Troubleshooting Problem Solution

When using nginx + keepalived website to achieve high availability load balancing scheme, the issue split brain problem and the primary server failure but did not release the resources often occur.
: 1. For the first question, the script can be used to monitor backup server, backup server if it is found appear vip address, sends an alarm immediately
# / bin / bash!
"192.168.22.3"> / dev / null grep | ip eth0 AS
IF [? $ -eq 0]
the then
echo "keepalived failed" | mail -s [email protected] abnormal alarm -keepalived
fi

the script was added to the regular tasks, executed once every 2 minutes:
* / 2 * * * * / usr / bin / sh /server/scripts/monitoring_keep.sh
Note: vip address appears on the backup server, a case split brain problem, another situation is the primary server fails, vip switch to the normal standby server, but both what kind of reason, were indicative of a problem clusters, we should be alert.

2. For the second question, is a common cause, nginx failure, but keepalived work, the main occupation of the vip address server resources, but it can not provide normal services, the solution is real-time monitoring primary server nginx service, if it is found nginx fails, the immediate release vip.
1) write a script nginx monitoring services:
vim /server/scripts/monitoring_web.sh
# / bin / bash!
NUM = ps -ef|grep -c nginx
IF [$ NUM -lt 2]
the then
Exit 1
the else
Exit 0
fi

2) add to the script executable permissions:
+ X /server/scripts/monitoring_web.sh the chmod
. 3) and LB01 lb02 mutual backup, keepalived configuration file as follows:
LB01 configuration file:
[the root LB01 @ ~] # Vim /etc/keepalived/keepalived.conf
! the configuration file keepalived for
global_defs {
the router_id of the LB01
}
vrrp_script check_web {
Script "/server/scripts/monitoring_web.sh"
interval The 2
weight -60
}
vrrp_instance one {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.22.3/24 dev eth0 label eth0:3
}
track_script {
check_web
}
}
vrrp_instance two {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.22.4/24 dev eth0 label eth0:4
}
}

lb02配置文件:
[root@lb02 scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_script monitoring_web {
script “/server/scripts/monitoring_web.sh”
interval 2
weight -60
}
vrrp_instance one {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.22.3/24 dev eth0 label eth0:3
}
}
vrrp_instance two {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.22.4/24 dev eth0 label eth0:4
}
track_script {
monitoring_web
}
}

Weight configuration file commands are described briefly:
when the weight is negative:
① If the script execution result is true, + weight priority;
② If the script execution result is false, priority unchanged;
when the weight is negative when:
① If the script execution result is true, the priority change;
② If the script execution result is false, priority + weight (weight minus the absolute value).
Since I put the main configuration file weight configured to -60, it is negative, so when nginx fails, ps -ef | grep -c nginx get the results must be less than 2, will return 1 (exit 1), then the monitoring service to keepalived the result is false, it will subtract the value of the priority value of the weight, get 90, more than 100 small backup server, the initiative to release vip resources. Similarly, when the nginx returned to normal, ps -ef | grep -c nginx results obtained must be greater than 2, return 0 (exit 0), then keepalived service to monitor the result is true, priority value is unchanged, 150 still remain, standby server than the value of large, snatch vip resources.

Published 15 original articles · won praise 10 · views 1240

Guess you like

Origin blog.csdn.net/ygh3110001606/article/details/104675480