[Detailed to Crying Series] keepalived configuration to switch zabbix backup

Deployment phase and problem notes: https://www.cnblogs.com/l-hh/category/1400262.html

Both machines have keepalived installed

[root@zabbix-master ~]# yum install keepalived.x86_64 -y

keepalived configuration

Zabbix-master profile:

! Configuration File for keepalived

global_defs {
   router_id zabbix-master    #router_id Machine Identification
}
vrrp_script chk_zabbix {
  script "/etc/keepalived/check.sh zabbix_server"
  interval 1          #Detect the running state of a service every second
  weight 30      #Range of Priority Change
  fall 2             #Successful attempts were made twice
  rise 2            #Failed after two attempts
}
vrrp_script chk_mysql {
  script "/etc/keepalived/check.sh mysqld"
  interval 1
  weight 20
  fall 2      
  rise 2       
}
vrrp_instance VI_1 {             #vrrp instance definition section
    state MASTER            #Set lvs status, MASTER and BACKUP, must be capitalized 
    interface eth0          #Setting up interfaces for external services
    virtual_router_id 100       #Set the virtual route flag, which is a number, and use the unique flag for the same vrrp instance 
    priority 100               #Define priority, the higher the number, the higher the priority. In a vrrp - instance, master must have a higher priority than backup 
    advert_int 1            #Set the time interval, in seconds, for synchronization checks between master and backup load balancers
    authentication {             #Set authentication type and password
        auth_type PASS      #There are two main types: PASS and AH.
        auth_pass 1111      #Verify password, MASTER and BACKUP passwords must be the same under the same vrrp_instance
    }
    virtual_ipaddress {         #Set virtual ip address, you can set multiple, one per line
        192.168.115.200  
    }
 track_script {
        chk_zabbix
    }
    track_script {
        chk_mysql
    }
notify_master "/etc/keepalived/zabbix.sh start"             #Specifies the script to execute when switching to master
notify_backup "/etc/keepalived/zabbix.sh stop"          #Specifies the script to execute when switching to backup
} 

As follows:

Zabbix-slave profile:

! Configuration File for keepalived

global_defs {
   router_id zabbix-slave
}
vrrp_script chk_zabbix {
  script "/etc/keepalived/check.sh zabbix_server"
  interval 1
  weight 5 
  fall 2    
  rise 2     
}
vrrp_script chk_mysql {
  script "/etc/keepalived/check.sh mysqld"
  interval 1
  weight 45 
  fall 2      
  rise 2       
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0   
    virtual_router_id 100
    priority 90
    advert_int 1 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {   
        192.168.115.200  
    }
 track_script {
        chk_zabbix
    }
    track_script {
        chk_mysql
    }
notify_master "/etc/keepalived/zabbix.sh start" 
notify_backup "/etc/keepalived/zabbix.sh stop" 
} 

As follows:

Note: The weight values of chk_zabbix and chk_mysql in the main note.The purpose of this setting is to ensure that keepalived switches normally when the host's ZABBIX or MySQL service is abnormal, and that keepalived can backcut when the host's ZABBIX and MySQL services are restarted normally.

Script in check.sh (same primary and standby):

Note: To determine the status of zabbix, mysql, and http services, the script stops keepalived and VIP switches to zabbix-slave when a service has an exception.

#!/bin/bash

zabbix_server=`ps -C zabbix_server --no-header | wc -l`
mysqld=`ps -C mysqld --no-header | wc -l`

case $1 in
    zabbix_server)
        if [ $zabbix_server -gt 0 ];then
            exit 0 
        else
            exit 1
        fi
    ;;
    mysqld)
        if [ $mysqld -gt 0 ];then
            exit 0
        else
            exit 1 
        fi
    ;;
Esac

Execute on the server:

[root@zabbix-master keepalived]# chmod +x check.sh

Zabbix.sh script (same primary and standby):

#!/bin/bash
case $1 in
    start)
        systemctl start zabbix-server
    ;;
    stop)
        systemctl stop zabbix-server
    ;;
Esac

Note: This script is designed to ensure that only one machine will be serviced externally by keeping the VIP on the machine with the zabbix service and the other without the zabbix service when the primary and standby switch or backcut occurs.(If both servers provide zabbix service, each event triggers two alerts)

The primary and standby machines restart the keepalived service and join the startup service

[root@zabbix-master ~]# systemctl restart keepalived
[root@zabbix-master ~]# systemctl enable keepalived 

Check all related services, and then test:

High Availability Testing

Zabbix-master View VIP information:

Failure of ZABBIX or mariadb service simulating zabbix-master:

Zabbix-slave then looks at the network card information:

Zabbix-web pages are also normally accessible:

Next, we simulate the zabbix-master recovery to test whether the VIP backcuts:

Note: High availability of zabbix has been achieved by now.

Note: If you do not want the unnecessary primary-standby switching of VIP, resulting in VIP preemption again after the recovery of zabbix-master, understand "Resolve VIP preemption problem" by yourself.

Tags: Linux Zabbix MySQL yum MariaDB

Posted on Thu, 19 Mar 2020 21:57:01 -0700 by phpdoodler