Raspberry Pi keepalived+mysql dual M architecture high availability configuration

1. Environment

       Raspberry Pi 3B uses network card: wlan0 ip: 192.168.31.209

       Raspberry Pi 3B uses network card: wlan0 ip: 192.168.31.194

Two, mysql installation and configuration

       1. Install a mysql service on each Raspberry Pi, configure bind_addr to 0.0.0.0, bind all the ip addresses on the Raspberry Pi

       2. The mysql on the two Raspberry Pis is configured as a dual M architecture, which is the master of each other. There are many examples on the Internet.

Three, keepalived installation and configuration

       1. apt-get install keepalived install keepalived service

       2. Raspberry Pi (192.168.31.209) add configuration file /etc/keepalived/keepalived.conf

global_defs{
        notification_email{
                [email protected]
                [email protected]
        }
        notification_email_from [email protected]
        smtp_server 127.0.0.1
        stmp_connect_timeout 30
        router_id lnmp_node1
}

vrrp_instance lnmp {
        state MASTER
        interface wlan0
        virtual_router_id 100
        priority 200
        advert_int 5
        authentication {
                auth_type PASS
                auth_pass 123456
        }
        virtual_ipaddress {
                192.168.31.110
        }
}

       3. Raspberry Pi (192.168.31.194) add configuration file /etc/keepalived/keepalived.conf

global_defs{
        notification_email{
                [email protected]
                [email protected]
        }
        notification_email_from [email protected]
        smtp_server 127.0.0.1
        stmp_connect_timeout 30
        router_id lnmp_node1
}

vrrp_instance lnmp {
        state BACKUP
        interface wlan0
        virtual_router_id 100
        priority 150
        advert_int 5
        authentication {
                auth_type PASS
                auth_pass 123456
        }
        virtual_ipaddress {
                192.168.31.110
        }
}

      4. Start the keepalived service on the two Raspberry Pis: keepalived -f /etc/keepalived/keepalived.conf The log started is printed in the system log file

            In /var/log/messages, after the startup is completed, a virtual router cluster is generated. The VIP is 192.169.31.110 in the configuration file. The client uses this IP for database access and interaction

     5. You can shut down a Raspberry Pi to test whether the configuration is normal and whether the IP can be switched normally. Use the ip addr command to see that the ip of 192.169.31.110 is switched between the two Raspberry Pis.

Three, health check script

       The previous steps can only solve the problem caused by server downtime and power failure; it cannot solve the problem that the server is normal and the mysql service is abnormal. You need to use a script to perform a health check. Run the following scripts on the two raspberry platoons respectively. The scripts need to be configured to boot up

       nohup sh /etc/keepalived/mysqlHealthCheck.sh  >/dev/null 2>log &

#!/bin/sh
while :
do

mysql -uroot -proot -e "select version();"
if [ $? -ne 0 ]; then
    #echo "mysql error!!!"
    service keepalived stop
else
    keepalivedcheck=`ps -C keepalived --no-header | wc -l`
    if [ $keepalivedcheck -eq 0 ]; then
              service keepalived start
         else
              #echo "keepalived is running"
              :
    fi
    #echo "mysql running!!!"
fi
sleep 5
done

   Note: Switching in this way is to switch without waiting for the data to be synchronized by the slave database, and the problem of inconsistent master-slave data may occur. If you want to ensure that the data in the master and slave databases are consistent, you need to wait for the data synchronization to be completed by the slave database, and you need to develop a script to detect the completion of the data synchronization. This is another problem, please refer to it yourself

  Test: service mysql stop Stop a mysql to check whether the client database access and interaction are normal

Guess you like

Origin blog.csdn.net/weixin_42226343/article/details/106547133