1、相关拓扑以及前期规划

keepalived 端口穿透 keepalived 通信端口_keepalived 端口穿透


如上图所示,本架构搭建的思路主要分为以下几个步骤:

1、两套MariaDB配置为互为主从(即,数据库A和数据库B之间配置两个相反方向的主从复制);

2、两台数据库服务器上安装配置Keepalived,使得两台数据库服务器能够互相发送VRRP报文,形成服务器高可用;

3、在Keepalived配置文件中进行相关配置,让Keepalived可以分别实时监听两台数据库服务器真实IP地址的3306端口(3306端口为MariaDB/MySQL的默认端口)。

2、安装MariaDB

分别在服务器上安装MariaDB数据库,在线安装命令如下:

sudo apt-get install mariadb-server -y

3、配置主从复制

3.1、配置数据库A为主,数据库B为从

(1)在数据库A输入命令“sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf”进行相关参数的修改,修改的参数如下所示:

bind-address            = 0.0.0.0  #该项表示允许连接服务端的客户端IP地址,0.0.0.0表示为所有
server-id              = 1         #注:主数据库和从数据库此数值不能相同
log_bin                = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10

(2)在数据库B输入命令“sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf”进行相关参数的修改,修改的参数如下所示:

bind-address            = 0.0.0.0   #该项表示允许连接服务端的客户端IP地址,0.0.0.0表示为所有
server-id              = 2          #注:主数据库和从数据库此数值不能相同
log_bin                = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10

(3)配置文件修改完成后分别在主数据库(数据库A)和从数据库(数据库B)上通过以下命令重启MariaDB:

systemctl restart mariadb.service

(4)分别在数据库A和数据库B上配置root用户的密码,相关命令如下:

sudo mysqladmin -u root password 'yanmuhuan@123'

(5)输入命令“sudo mysql_secure_installation”分别在数据库A和数据库B上进行数据库初始化,如下所示(下列配置为本实验的选项,仅供参考,根据实际需要选择不同选项):

yanmuhuan@ubuntuserver:~$ sudo mysql_secure_installation
[sudo] password for yanmuhuan:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   #输入事先设置的数据库root账户密码
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n   #是否改变root用户密码?
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n   #是否删除匿名用户?
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n   #是否不允许root用户直接登录?
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n   #是否删除测试数据库?
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y   #是否重新加载权限信息?
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

(6)使用root用户进入数据库A,并创建一个名为yanmuhuan的用户(该账户用作数据库A到数据库B之间的数据实时复制),相关命令如下:

mysql -u root -p   #使用root用户并密码登录到数据库A

keepalived 端口穿透 keepalived 通信端口_数据库_02

create user 'yanmuhuan'@'%' identified by 'yanmuhuan@123';   #创建一个名为yanmuhuan的用户,允许此用户在所有主机上登录,并设置密码为“yanmuhuan@123”

keepalived 端口穿透 keepalived 通信端口_数据库_03


(7)对备份用户yanmuhuan赋予replication slave权限,相关命令如下:

grant replication slave on *.* to 'yanmuhuan'@'%' identified by 'yanmuhuan@123';

keepalived 端口穿透 keepalived 通信端口_mysql_04


(9)在数据库A上输入命令“show master status;”记录下“File”和“Position”两个参数,这两个参数会在配置数据库B(从数据库时使用)

keepalived 端口穿透 keepalived 通信端口_数据库_05


(10)登录到数据库B上,输入以下命令,连接到数据库A,形成数据库A和数据库B之间的主从复制:

change master to  master_host = '192.168.2.133', master_user = 'yanmuhuan', master_password = 'yanmuhuan@123', master_log_file = 'mysql-bin.000001', master_log_pos = 1011;

keepalived 端口穿透 keepalived 通信端口_mysql_06


(11)重启数据库B并登录数据库B,然后检查数据库A和数据库B之间的主从复制状态,命令如下:

systemctl restart mariadb.service  #重启数据库B
sudo mysql -u root -p   #登录数据库B
MariaDB [(none)]> show slave status\G;  #检查主从复制状态

keepalived 端口穿透 keepalived 通信端口_keepalived 端口穿透_07


keepalived 端口穿透 keepalived 通信端口_mariadb_08


(12)对数据库A进行SQL语句操作,然后查看数据库B,验证主从复制运转正常,此处的SQL操作为,创建一个数据库“yanmuhuan_test”,SQL语句如下:

create database yanmuhuan_test;

keepalived 端口穿透 keepalived 通信端口_mysql_09


keepalived 端口穿透 keepalived 通信端口_keepalived 端口穿透_10

3.2、配置数据库B为主,数据库A为从

【注】本小节配置方法和2.2.1基本一样,但2.2.1小节中的步骤(1)~(5)对数据库A和数据库B都进行了初始化操作,故本节不再对这几个步骤进行重复操作,可以直接从步骤(6)为参考开始

(1)使用root用户登录数据库B,在数据库B上创建一个名为“yanmuhuan”的账户,密码为“yanmuhuan@123”,相关命令如下:

mysql -u root -p   #使用root用户并密码登录到数据库B
create user 'yanmuhuan'@'%' identified by 'yanmuhuan@123';  #创建用户“yanmuhuan”

keepalived 端口穿透 keepalived 通信端口_主从复制_11


(2)对备份用户yanmuhuan赋予replication slave权限,相关命令如下:

grant replication slave on *.* to 'yanmuhuan'@'%' identified by 'yanmuhuan@123';

keepalived 端口穿透 keepalived 通信端口_keepalived 端口穿透_12


(3)在数据库B上输入命令“show master status;”记录下“File”和“Position”两个参数,这两个参数会在配置数据库A(从数据库时使用)

keepalived 端口穿透 keepalived 通信端口_mysql_13


(4)登录到数据库A上,输入以下命令,连接到数据库B,形成数据库B和数据库A之间的主从复制:

change master to  master_host = '192.168.2.199', master_user = 'yanmuhuan', master_password = 'yanmuhuan@123', master_log_file = 'mysql-bin.000002', master_log_pos = 704;

keepalived 端口穿透 keepalived 通信端口_mysql_14


(5)重启数据库A并登录数据库A,然后检查数据库B和数据库A之间的主从复制状态,命令如下:

systemctl restart mariadb.service  #重启数据库A
sudo mysql -u root -p   #登录数据库A
MariaDB [(none)]> show slave status\G;  #检查主从复制状态

keepalived 端口穿透 keepalived 通信端口_keepalived 端口穿透_15


keepalived 端口穿透 keepalived 通信端口_数据库_16


(6)对数据库B进行SQL语句操作,然后查看数据库A,验证主从复制运转正常,此处的SQL操作为,创建一个数据库“yanmuhuan_test2”,SQL语句如下:

create database yanmuhuan_test2;

keepalived 端口穿透 keepalived 通信端口_mariadb_17

3、在两台数据库服务器上安装Keepalived,并进行相应配置,以形成基于VRRP协议的高可用数据库结构

(1)分别在数据库A和数据库B上安装Keepalived,相关命令如下:

sudo apt-get install keepalived -y

(2)目前基于debian系统软件源上的Keepalived默认没有keepalived.conf这个配置文件,因此这个配置文件需要手动创建,并放置在/etc/keepalived目录下,位于数据库A服务器上的配置文件keepalived.conf的内容如下:

global_defs {
   router_id mysql-ha
}

vrrp_instance VI_1 {
    state MASTER     #VRRP节点角色
    interface ens33  #设置绑定的网卡
    virtual_router_id 201
    priority 100     #VRRP优先级
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS     #VRRP验证方式,此处为密码方式
        auth_pass 123456   #VRRP验证密码,主备必须一致,否则验证不通过
    }
    virtual_ipaddress {
        192.168.2.200      #用于主备漂移的虚拟IP地址
    }
}

#以下内容为VRRP监听真实IP地址的相关信息之配置
virtual_server 192.168.2.200 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 192.168.2.133 3306 {
        weight 1
        notify_down /etc/keepalived/mysql.sh  #当MariaDB在某时刻停止服务时(监听不到真实IP地址的3306端口)执行的脚本
        TCP_CHECK {
            connect_port 3306
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
        }
    }
}

位于数据库B上的keepalived.conf配置文件内容如下:

global_defs {
   router_id mysql-ha
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 201
    priority 50
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.2.200
    }
}

virtual_server 192.168.2.200 3306 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 192.168.2.199 3306 {
        weight 1
        notify_down /etc/keepalived/mysql.sh
        TCP_CHECK {
            connect_port 3306
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
        }
    }
}

需要注意的是,在keepalived.conf文件中,real_server结构中包含notify_down参数,相应的理解应该为——当keepalived运行时,keepalived进程会实时监测服务器真实IP地址的特定端口(此处为数据库服务器真实IP地址的3306端口,因为3306是MariaDB或MySQL的默认端口),当该特定端口由监听状态转为未使用状态或关闭状态,则会立即执行notify_down参数所指向的shell脚本,本案例中,相应的shell文件是位于/etc/keepalived中的mysql.sh脚本,相应的脚本代码如下:

#!/bin/bash
echo 'yanmuhuan@123' | sudo -s killall -9 keepalived

该脚本表示杀死本服务器的keepalived进程,结合keepalived.conf,可以达到的效果为,当MariaDB发生故障时,真实IP地址的3306由监听状态转为未使用状态,发生此事件时,杀死keepalived进程以保证网络流量可以通过VRRP协议切换到备用数据库上。
如果不杀死本服务器的keepalived进程,在MariaDB发生故障不能正常服务时,流量会因为VRRP没有发生主备切换继续发送至故障数据库服务器,形成黑洞服务器导致业务中断。
以上配置完成后,整个高可用数据库结构就已经完成,就可以进行相应的故障测试了。

4、验证

在数据库A服务器(主节点)上停止MariaDB后,尝试登录数据库,仍然成功,说明高可用形成。

keepalived 端口穿透 keepalived 通信端口_主从复制_18