MariaDB cluster environment construction

MariaDB Master-Master dual master + Lvs + Keepalived configure the load balancing and high availability environment of DB services
1 . Architecture introduction
  Architecture introduction: Use Keepalived to build a high-availability MariaDB-HA to ensure the consistency of the two MariaDB data, and then use Keepalived to implement virtual IP, and use Keepalived's own service monitoring function to realize automatic switchover when MariaDB fails. Keepalived is designed to build a highly available LVS load balancing cluster. You can call the ipvsadm tool to create virtual servers and manage server pools, not just for hot backup. Using keepalived to build an LVS cluster is easier and easier to use. The main advantages are as follows: 1. Implement hot standby switching for the LVS load balancing scheduler to improve availability. 2. Perform a health check on the nodes in the server pool, automatically remove the failed node, and rejoin it after recovery. The LVS cluster structure implemented based on LVS+Keepalived includes at least two hot-standby load schedulers and more than two node servers. In this example, based on the LVS cluster in DR mode, a load scheduler is added, and Keepalived is used to realize the hot backup of the scheduler, so as to build an LVS website cluster platform with two capabilities of load balancing and high availability. Keepalived can achieve high availability or hot backup to prevent the problem of single point of failure, and the core of Keepalived is VRRP protocol. VRRP protocol mainly realizes redundancy at routers or Layer 3 switches. Keepalived uses VRRP protocol to achieve high available. The LVS cluster constructed by LVS+Keepalived, LVS load balances user requests to the back-end MariaDB server. The function of Keepalived is to detect the status of the MariaDB server. If a MariaDB server goes down or the work fails, Keepalived will detect it and send The faulty MariaDB server is removed from the system. When the MariaDB server works normally, Keepalived automatically adds the MariaDB server to the server group. All these tasks are completed automatically without manual intervention. All that is required is to repair the faulty MariaDB server.
  Hardware topology:
  VIP: 172.19.0.8
  MariaDB1: 172.19.0.32
  MariaDB2: 172.19.0.33
 
Two. Install software:
2.1 Install CentOS 6.6_64 system.

2.2 Open the iptables service:
vim /etc/sysconfig/iptables
add a line:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT/sbin/iptables -I INPUT -p vrrp -j ACCEPT
and then restart the service: service iptables restart

2.3 Turn off the selinux function and set the kernel parameters:
vim /etc/sysconfig/selinux
set SELINUX=disabled
Setenforce 0 Temporarily turn off selinux
and use getenforce to check the status of selinux.

echo "1" >/proc/sys/net/ipv4/ip_forward
/sbin/sysctl -p

2.4 Uninstall the mysql package.
rpm –e mysql-libs –nodeps

2.5 Set the source for MariaDB.
cd /etc/yum.repos.d/
Add a new file with the following content:
vim /etc/yum.repos.d/MariaDB.repo
[mariadb]
name=MariaDB
baseurl=
http://yum.mariadb.org/10.0/centos6-amd64 gpgkey=https://yum.mariadb.org/RPM- GPG-KEY-MariaDB
gpgcheck=1

2.6 installs the MariaDB package.
yum -y install MariaDB-client MariaDB-server MariaDB-devel
service mysql on
chkconfig mysql on

2.7 Set the MariaDB root password for security hardening.
/usr/bin/mysql_secure_installation

2.8 MariaDB sets dual-master, and the process of setting dual-master is the same as that of mysql master-slave. Dual masters are masters and slaves of each other. You need to authorize the user first, and then refresh the permissions.
Execute the statement on MariaDB1:
grant all privileges on *.* to sa@'%' identified by 'sa';
flush privileges;
Execute the statement on MariaDB2:
grant all privileges on *.* to sa@'%' identified by ' pass$123';
flush privileges;

2.9 MariaDB sets the dual-master configuration file.
Configure the auto-increment ID on MariaDB1 to be an odd number, and the auto-increment to be 2.
[root@mariadb1 yum.repos.d]# vim /etc/my.cnf
[client-server]
!includedir /etc/my.cnf.d
[mysqld ]
log_slave_updates
log-bin = mysql-bin
server-id = 1
binlog-ignore-db = mysql
auto-increment-increment = 2
auto-increment-offset = 1

Configure auto-increment ID to be even and auto-increment to 2 on MariaDB2.
[root@mariadb2 ~]# more /etc/my.cnf|grep -v '#'|grep -v '^$'
[client-server]
!includedir /etc/my.cnf.d
[mysqld]
log_slave_updates
log- bin=mysql-bin
server-id=2
binlog-ignore-db=mysql
auto-increment-increment = 2
auto-increment-offset = 2
2.10 Set MariaDB master and slave, and then set dual master.
First, mariadb1 is the master, and mariadb2 is the slave.
Log in to MariaDB locally on Mariadb1 through mysql –uroot –p.
Enter:
show master status; record the file name and location of mysql-bin.
Log in to MariaDB locally on Mariadb2 via mysql –uroot –p.
Execute stop slave;
change master to master_host='172.19.2.35', master_user='sa', master_password='sa', master_log_file='mysql-bin.0000XX', master_log_pos=XXX;
execute start slave;
check the status of the slave library: show slave status\G;

again, mariadb2 is the master, and mariadb1 is the slave.
Log in to MariaDB locally on Mariadb2 via mysql –uroot –p.
Enter:
show master status; record the file name and location of mysql-bin.
Log in to MariaDB locally on Mariadb1 through mysql –uroot –p.
Execute stop slave;
change master to master_host='10.10.10.245',master_user=' abc222',master_password=' eayun123.com ',master_log_file='mysql-bin.0000XX',master_log_pos=XXX;
execute start slave;
view slave status: show slave status\G;

2.11 Install the Keepalived package and write the MariaDB status detection script.
yum –y install keepalived
Set the state automatic detection script of MariaDB and configure Keepalived on mariadb1:
[root@mariadb1 yum.repos.d]# more /root/check_mysql.sh |grep -v '#'|grep -v '^$ '
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=1001 #Set to the previously set password
CHECK_TIME=3
MYSQL_OK=1
function check_mysql_helth (){ 
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >
        if [ $? = 0 ] ;then 
                MYSQL_OK=1
        else 
                MYSQL_OK=0
        fi 
        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 
          /etc/init.d/keepalived stop 
     exit 1  
     fi 
     sleep 1 
done

[root@mariadb1 yum.repos.d]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_SLAVE
}
vrrp_script check_mysql {
    script "/root/check_mysql.sh"
    interval 3
}
vrrp_sync_group VG1 {
     group {
        VI_1
     }
}
vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 51
     priority 100 #The priority must be higher than High
     advert_int 1
     nopreempt #Set not to preempt
     authentication {
         auth_type PASS
         auth_pass kinda22
     }
     track_script {
         check_mysql
     }
     virtual_ipaddress {
         172.19.0.8
     }
}
virtual_server 172.19.0.8 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    protocol TCP
    real_server 172.19.0.32 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 172.19.0.33 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
Set the MariaDB state automatic detection script and configure Keepalived on Mariadb2:
[root@mariadb2 ~]# more /root/check_mysql.sh |grep -v '#'|grep -v '^$'
MYSQL =/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=1001 #Set to the previously set password
CHECK_TIME=3
MYSQL_OK=1
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 
        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 
          /etc/init.d/keepalived stop 
     exit 1  
     fi 
     sleep 1 
done 

[root@mariadb2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_SLAVE
}
vrrp_script check_mysql {
    script "/root/check_mysql.sh"
    interval 3
}
vrrp_sync_group VG1 {
     group {
        VI_1
     }
}
vrrp_instance VI_1 {
     state BACKUP
     interface eth0
     virtual_router_id 51
     priority 99 #The priority must be lower than the master node
     advert_int 1
     nopreempt #Set not to preempt
     authentication {
         auth_type PASS
         auth_pass kinda22
     }
     track_script {
         check_mysql
     }
     virtual_ipaddress {
         172.19.0.8
     }
}
virtual_server 172.19.0.8 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    protocol TCP
    real_server 172.19.0.32 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
   real_server 172.19.0.33 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

2.14 部署LVS负载均衡。
yum install ipvsadm
Mariadb1上的配置与mariadb2上的配置一样。
[root@mariadb1 yum.repos.d]# vim /root/ipvsadm_conf.sh
#!/bin/bash
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t 172.19.0.8:3306 -s wrr
/sbin/ipvsadm -a -t 172.19.0.8:3306 -r 172.19.0.32:3306 -g -w 1
/sbin/ipvsadm -a -t 172.19.0.8:3306 -r 172.19.0.33:3306 -g -w 1
service ipvsadm save

chmod 755 /root/ipvsadm_conf.sh
./root/ipvsadm_conf.sh start

[root@mariadb1 yum.repos.d]# vim /etc/init.d/lvsdr.sh
#!/bin/bash
VIP=10.10.10.246                 
. /etc/rc.d/init.d/functions                    
case "$1" in
start)
    /sbin/ifconfig lo down 
    /sbin/ifconfig lo up       
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore   
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce   
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore   
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce   
    /sbin/sysctl -p >/dev/null 2>&1
    /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up
    /sbin/route add -host $VIP dev lo:0 
    echo "LVS-DR real server starts successfully."                   
    ;;   
stop)   
    /sbin/ifconfig lo:0 down   
/sbin/route del $VIP >/dev/null 2>&1
    echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore   
    echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce   
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore   
    echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce   
    echo "LVS-DR real server stopped."
    ;;
status)
    isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"`
    isRoOn=`/bin/netstat -rn | grep "$VIP"`
    if [ "$isLoOn" == "" -a "$isRoOn" == "" ]; then
       echo "LVS-DR real server has to run yet."
    else
       echo "LVS-DR real server is running."
    fi
    exit 3
    ;;
*)   
    echo "Usage: $0 {start|stop|status}"
    exit 1   
esac                    
exit 0

chmod 755 /etc/init.d/lvsdr.sh
/etc/init.d/lvsdr.sh start

三.业务验证:
1.使用SQLyog工具连接172.19.0.8
create database mysqltest;
CREATE TABLE `ttt` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) NOT NULL,
  PRIMARY KEY (`id`)
);
insert into ttt values('','aaa1');
insert into ttt values('','aaa2');
insert into ttt values('','aaa3');
exit
mysql –h 10.10.10.246 –uroot –peayun\)\(\*2015
insert into ttt values('','bbb1');
insert into ttt values('','bbb2');
insert into ttt values('','bbb3');
exit

select * from ttt; You can see that when the parameter auto-increment-offset = 1 in the LVS configuration file /etc/my.cnf distributed to mariadb, the ID in the ttt table is an odd number. When the parameter auto-increment-offset = 2 in /etc/my.cnf, the ID in the ttt table is an even number.

3. Stop the keepalived service on one of the business systems, and the keepalived VIP will drift to another system.
service keepalived restart Then check the VM where the VIP is located through ip a, and check the protocol status of VRRP and the status switch of Keepalived Master/Backup through more /var/log/messages.
4. Stop the mariadb service of one of the business systems for testing. Through the ipvsadm -Ln command from another business system, it is found that the business system has undergone the Keepalived health check, is removed from the cluster, and the Keepalived service has stopped.
5. It is important to note that the mariadb service is a database service. For security, there is no need to configure the public network IP address, and the connection to the DB is done through the intranet.



We go to the /etc/my.cnf.d folder to find server.cnf and add lower_case_table_names=1 max_connections = 200 under
[mysqlld]



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646303&siteId=291194637