Keepalived installed and configured to monitor MySQL dual master on CentOS 7

keepalived installation

Please see here for MySQL dual-master configuration: https://tongyao.blog.csdn.net/article/details/132016200?spm=1001.2014.3001.5502

The installation steps for the two servers 128 and 129 are the same, but the configuration files are different, which are introduced below.

1. Install relevant dependency packages, download the keepalived installation package, decompress, configure, and compile

[root@128 ~]# cd /opt
[root@128 opt]# yum -y install gcc openssl-devel popt-devel psmisc
[root@128 opt]# wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz
[root@128 opt]# tar -zxvf keepalived-2.2.7.tar.gz
[root@128 opt]# cd keepalived-2.2.7
[root@128 keepalived-2.2.7]# ./configure --prefix=/opt/keepalived-2.2.7
[root@128 keepalived-2.2.7]# make && make install

2. Copy the file to the corresponding directory

[root@128 keepalived-2.2.7]# mkdir /etc/keepalived
[root@128 keepalived-2.2.7]# cp keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@128 keepalived-2.2.7]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@128 keepalived-2.2.7]# cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@128 keepalived-2.2.7]# cp bin/keepalived /usr/sbin/

3. Create a new /etc/keepalived/shutdown.sh file with the following content:

#!/bin/bash
#该脚本是在mysql服务出现异常时,将keepalived应用停止,从而使虚拟vip主机自动连接到另一台mysql上
killall keepalived

Enforce authorization, otherwise keepalived will not be closed when a MySQL exception occurs:

chmod 777 /etc/keepalived/shutdown.sh

4. Use the ifconfig command to check the network card name. The local network card name is ens33.
Insert image description here
5. Modify the 128 server/etc/keepalived/keepalived.conf configuration file as follows:

! Configuration File for keepalived
 
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state MASTER
    #interface为刚才查到的本机网卡名称
    interface ens33
    #同一网段中同一组virtual_router_id值相同。不同组virtual_router_id值唯一。
    #如server-1、server-2为一组,virtual_router_id=51
    #server-3、server-4为一组,则virtual_router_id不能为51
    virtual_router_id 51
    #字数越大,优先级越高,master必须大于backup
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       #该ip为虚拟出来的vip地址
       192.168.222.130
    }
}
 
#配置virtual_server  ip为上面配置的虚拟vip地址  端口为mysql的端口
virtual_server 192.168.222.130 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    #real_server 该配置为实际物理机ip地址 以及实际物理机mysql端口
    real_server 192.168.222.128 3306 {
        #当该ip 端口连接异常时,执行该脚本
        notify_down /etc/keepalived/shutdown.sh
        TCP_CHECK {
            #实际物理机ip地址
            connect_ip 192.168.222.128
            #实际物理机port端口
            connect_port 3306
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

6. Modify the 129 server/etc/keepalived/keepalived.conf configuration file. The difference from the configuration of 128 is the two configurations of real_server and connect_ip. The corresponding actual host IP needs to be configured. The details are as follows:

! Configuration File for keepalived
 
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state BACKUP
    #interface为刚才查到的本机网卡名称
    interface ens33
    #同一网段中同一组virtual_router_id值相同。不同组virtual_router_id值唯一。
    #如server-1、server-2为一组,virtual_router_id=51
    #server-3、server-4为一组,则virtual_router_id不能为51
    virtual_router_id 51
    #字数越大,优先级越高,master必须大于backup
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       #该ip为虚拟出来的vip地址
       192.168.222.130
    }
}
 
#配置virtual_server  ip为上面配置的虚拟vip地址  端口为mysql的端口
virtual_server 192.168.222.130 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    #real_server 该配置为实际物理机ip地址 以及实际物理机mysql端口
    real_server 192.168.222.129 3306 {
        #当该ip 端口连接异常时,执行该脚本
        notify_down /etc/keepalived/shutdown.sh
        TCP_CHECK {
            #实际物理机ip地址
            connect_ip 192.168.222.129
            #实际物理机port端口
            connect_port 3306
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

For detailed configuration, please refer to "Keepalived Installation and Configuration Detailed Explanation"

7. Add servers 128 and 129 keepalived to start automatically at boot and start the service

[root@128 keepalived-2.2.7]# systemctl enable keepalived
[root@128 keepalived-2.2.7]# systemctl start keepalived

8. After startup, if the keepalived status is: active (running), it is normal.

Insert image description here
9. After startup, it is equivalent to virtualizing a VIP 192.168.222.130. You can use the remote connection tool to connect to the server. After connecting, use ifconfig to check that the actual physical server used by the virtual VIP is the 129 server.

Insert image description here
10. Stop the keepalived application of the 129 server and check the 130 service ifconfig again. You can see that the 130 server automatically drifts the physical machine IP to the 128 server. At this point, the keepalived deployment is completed
Insert image description here
Insert image description here
.

mysql dual master dual active + keepalived high availability overall test

1. First start all mysql and keepalived applications on the two servers 128 and 129, and then use the mysql connection tool to connect to the 192.168.222.130 server virtualized by keepalived.

Insert image description here
2. Insert id=3, name='wangwu' into the t_test table of the 130 database test_db library

Insert image description here
3. At this time, you can view the 128 and 129 databases, and the data has been synchronized.

Insert image description here
4. Check that the actual physical machine used by ifconfig of server 130 is 128, so server mysql of 128 is the main database.

Insert image description here
5. At this time, manually stop the 128 server mysql. Keepalived detects that the 128 service port 3306 connection fails, and will execute the /etc/keepalived/shutdown.sh script to end the 128 server keepalived application.

Insert image description here
6. At this time, connect to the 130 service again, check ifconfig, and find that the physical machine has actually been transferred from 128 to the 129 server.

Insert image description here
7. Then use the mysql connection tool to connect to the mysql of 130, insert a piece of data id=4, name='zhaoliu', and test whether the data is stored in the mysql of the 129 server.

Insert image description here
8. Check the 129 server mysql data. The data has been synchronized, indicating that keepalived has successfully established high availability. When a problem occurs with the 128 server mysql, keepalived automatically drifts the IP to the physical machine 129 server, so that the 129 server mysql serves as the main database.

Insert image description here
9. At this time, start the 128 server mysql and keepalived applications.

Insert image description here
10. Check the t_test table data of the 128 database. The data has been synchronized successfully.

Insert image description here
11. If the drift fails, it may be caused by the duplication of virtual_router_id in the same network segment. Executable command to check if other IPs are used

[root@128 keepalived-2.2.7]# tcpdump -i 网卡名 vrrp -n |grep virtual_router_id值
#例如:
[root@128 keepalived-2.2.7]# tcpdump -i eno16777736 vrrp -n |grep 51

At this point, the high-availability deployment and testing of mysql dual-master dual-active + keepalived is completed.

Article reference: https://blog.csdn.net/a360284634/article/details/89892028 , with slight modifications.

Some other configuration details: https://www.cnblogs.com/cyleon/p/10338822.html

Guess you like

Origin blog.csdn.net/u014641168/article/details/131982020