mysql keepalived

Two servers, has already done the master-master replication, keepalived now and then to ensure high availability.

Without further ado, the first two servers are installed keepalived:

yum install keepalived -y

Successful installation.

keepalived use the default configuration file: /etc/keepalived/keepalived.conf

There will be a default after installed, we can back up what, in fact, does not back up all right

keepalived.conf use the following configuration:

[Master1 the root @ ~] # CP /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak 
[Master1 the root @ ~] # # Vim /etc/keepalived/keepalived.conf emptied default content, using the following direct configuration: 
! the configuration File for keepalived 
       
global_defs {  
  router_id mysql1 
} 
       
vrrp_script chk_mysql_port {# detect mysql service is running. There are many ways, such as process, script testing, etc. 
    script "/opt/chk_mysql.sh" # script here, by monitoring the 
    interval 2 # script execution interval, tested once every 2s 
    weight -5 # script priority change result of the detection failure (non-script returns 0), the priority -5 
    Fall # 2 detects two consecutive failures are considered to determine the true failure. It will reduce the priority (1-255) with a weight 
    Rise 1 # 1 Detection successful even if successful. But does not modify the priority 
} 
       
vrrp_instance VI_1 { 
    State the BACKUP     
    interface enp0s3 # ip of the specified virtual network interface
    virtual_router_id 100 # router identification, the MASTER must be consistent and BACKUP 
    101 # define the priority, the greater the priority number, the higher the priority, under the same vrrp_instance, MASTER priority must be higher than the priority of BACKUP. MASTER recovery after such a failure, you can once again win back the VIP resource 
    advert_int 1          
    authentication {    
        AUTH_TYPE PASS 
        AUTH_PASS 1111      
    } 
    virtual_ipaddress {     
        192.168.1.222 # Set the virtual IP 
    } 
      
    track_script {                
       chk_mysql_port              
    } 
}
global_defs global, mainly used mysql on this machine mail notification after hanging up, but not here first mail notification mechanism mainly to configure is router_id, in fact, a server's identity id, two servers the value to be different
vrrp_script mysql is configured to check whether the normal script, the script is given below
vrrp_instance is an example, points to note: 
State formulated BACKUP represents binding nopreemt provided non-preemptive, mysql is hung on the A, B to the vip will float up, etc. A better on the mysql, still vip on the B, B to wait until mysql hung up on, it will float to the vip a up. and thought it more reasonable, nothing good to grab well, all for customer service well, grab what.
interface to be dubbed the current real ip card that use of the machine, such as sometimes eth0, but I saw on my server is enp0s3:
virtual_router_id
. router identification, MASTER and BACKUP must be consistent
nopreempt set to a non-preemptive mode, or do not use this good, explained below.

given second machine keepalived following configuration:
[root@localhost opt]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
    router_id mysql2
}

vrrp_script chk_mysql_port {
    script "/opt/chk_mysql.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 100
    priority 97
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.222
    }
    track_script {               
        chk_mysql_port             
    }
}

It can be seen that only two machines router_id and priority different. And both are set to become BACKUP, no nopreempt.

/opt/chk_mysql.sh script:
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=c1551
MYSQL_PASSWORD=1qaz@WSX3edc
CHECK_TIME=3
  
#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
  
MYSQL_OK=1
  
function check_mysql_helth (){
    $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
    if [ $? = 0 ] ;then
    MYSQL_OK=1
    else
    MYSQL_OK=0
    fi
    echo $MYSQL_OK
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi
if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    echo 'killing keepalived...'
    pkill keepalived
    exit 1
else
    echo 'not killing keepalived...'
fi
sleep 1
done
For preemptive and non-preemptive, someone says so:
keepalive is vrrp protocol daemon ways to achieve health checks according to the configuration file on the host linux based. 
VRRP is a protocol, it can be the responsibility of a virtual router dynamically allocated to one of the VRRP routers on the LAN. 
Control VRRP virtual router IP address of the router is called the master router that forwards packets sent to these IP addresses. 
Once the master fails, this selection process provides a dynamic failover mechanism, which allows a virtual router's IP address as a default host terminal first hop router. 
Preparation keepalive elected multicast, unicast, etc. (custom), the master keepalive achieved. And preemption mode of operation is divided into non-preemptive (controlled parameter nopreempt). 
1 ) preemptive mode: 
the main service is working, virtual IP will be on the primary and backup service is not available, when the main service of lower priority than equipment, equipment will automatically seize the virtual IP, then, is not the main provision of services, equipment provided service. 
In other words, work in preemptive mode, regardless of standby, just priority. 
As configured, regardless of keepalived . Conf in the state configured master or backup, just to see who the high-priority priority (in general, the state is a higher priority than MASTER BACKUP). 
high priority that a priority after the recovery, VIP resource will automatically seize back again! ! 
2 ) non-preemptive mode:  
in this way the parameter nopreempt (generally disposed below the line that advert_int) controlled. Priority regardless of priority, as long as the MASTER machine fails, VIP resources will be switched to BACKUP.
and when the MASTER machine back, will not go back to seize the VIP resource, until when BACKUP machine failure, can automatically switch back. 
Be careful:
nopreempt this parameter can be used to state the case for the backup, so take the time to configure the master state and backup are set to backup, this will achieve a non-preemptive mode keepalived! 

In other words: 
A) When the state status as a master, one of the backup, plus without nopreempt this parameter is the same effect. That is according to priority priority to determine who seize vip resources, it is preemptive mode! 
b) When the state status is set to backup, if not nopreempt configuration parameters, then the priority is to see the priority to decide who seize vip resources, that is preemption mode. 
c) When the state status is set to Backup, if the configuration parameters nopreempt, then you will not to consider the priority priority, non-preemptive mode! That is, only vip currently resides machine fails, another machine to take over the vip. Even after the high priority that the machine will not take the initiative to restore snatch vip, can only wait until the other party fails, vip will be cut back.

However, I found that if I set both BACKUP and enable nopreempt, health checks and use the above script, then, if mysql on a machine hung up, although health checks have not seen, keepalived of lower priority on this machine , but, vip still on this machine, because said above, do not take the initiative to grab plus vip, only keepalived on this machine or directly linked to the machine hung up, vip will drift to another the machine !!! WTF !!! that mysql on this machine though hanging, but keepalived on the machine or on one side and other mysql up, it is also taking up the vip this is obviously not what I want !! the so, or not configured nopreempt well, let's priority to reduce the machine automatically release vip.

the above.

 
 
 

Guess you like

Origin www.cnblogs.com/lihan829/p/11427162.html