标签归档:master

Keepalived单网络主备单主配置模式(实战)

前言:

以下阐述在Keepalived中配置最简单的主备模式,后面我将一直讲述从简单的单网络单主主备模式,到双网络双主双同步的故障漂移模式。

关于Keepalived介绍,这里就不再叙述,可参阅前文:

keepalived 源代码编译安装及配置文件讲解》。

架构示意图:

多播IP是:224.0.0.111。
NAT网络可根据自己的实际情况配置。

                        +------+
			|Client|
			+------+
                           /\
		       +--------+
                       |Internet|
		       +--------+
                           /\
		       +--------+
                       |NAT 网络|
		       +--------+
                           /\
		+-----------------------+
                | 公网VIP1:172.16.8.100 |
		+-----------------------+
                   /                \
+-----------------------+      +-----------------------+
| KA+Lvs/Nginx/HAProxy  |      | KA+Lvs/Nginx/HAProxy  |
| VIP1:Master    (eth1) |多播IP| VIP1:BACKUP    (eth1) |
| IP1:172.16.8.10(eth1) |      | IP1:172.16.8.11(eth1) |
+-----------------------+      +-----------------------+
                   \                /
                +-----------------------+
                | 公网VIP1:172.16.8.100 |
		+-----------------------+
		           \/
		        +------+
			|资源池|
			+------+

环境:

MASTER:172.16.8.10
BACKUP:172.16.8.11
VIP:172.16.8.100
OS:CentOS Linux release 7.4.1708 (Core)

先决条件:

  • 时间同步。
  • 设置SELinux和防火墙。
  • 互相之间/etc/hosts文件添加对方主机名(可选)。
  • 确认接口支持多播(组播)新网卡默认支持。

keepalived 源代码编译安装及配置文件讲解》文中已完成以上必备条件。

1.单网络主备配置文件

MASTER 配置文件:

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from ka@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka67
   vrrp_mcast_group4 224.0.0.111
}
vrrp_instance VG_1 {
    state MASTER
    interface eth0
    virtual_router_id 103
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass renwole0
    }
    virtual_ipaddress {
        172.16.8.100
    }
    notify_master "/usr/local/keepalived/etc/keepalived/notify.sh master"
    notify_backup "/usr/local/keepalived/etc/keepalived/notify.sh backup"
    notify_fault "/usr/local/keepalived/etc/keepalived/notify.sh fault"          
}

BACKUP 配置文件:

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from ka68@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka68
   vrrp_mcast_group4 224.0.0.111
}
vrrp_instance VG_1 {
    state BACKUP
    interface eth0
    virtual_router_id 103
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass renwole0
    }
    virtual_ipaddress {
        172.16.8.100
    }
    notify_master "/usr/local/keepalived/etc/keepalived/notify.sh master"
    notify_backup "/usr/local/keepalived/etc/keepalived/notify.sh backup"
    notify_fault "/usr/local/keepalived/etc/keepalived/notify.sh fault"             
}

2.通用脚本

以下内容是notfiy.sh通用检测脚本:

$ cat /usr/local/keepalived/etc/keepalived/notify.sh
#!/bin/bash
contact='root@localhost'
                
notify() {
    local mailsubject="$(hostname) to be $1, vip floating"
    local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    echo "$mailbody" | mail -s "$mailsubject" $contact
}
                
case $1 in
master)
    notify master   
    ;;
backup)
    notify backup   
    ;;
fault)
    notify fault    
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac

3.主备测试

测试MASTER

启动keepalived之前,查看网卡信息:

[root@ka67 keepalived]# ip a
...
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
   link/ether 00:15:5d:ae:02:78 brd ff:ff:ff:ff:ff:ff
   inet 172.16.8.10/24 brd 172.16.8.255 scope global eth0
      valid_lft forever preferred_lft forever
   inet6 fe80::436e:b837:43b:797c/64 scope link
      valid_lft forever preferred_lft forever

启动keepalived后,再次查看网卡信息:

[root@ka67 keepalived]# ip a
...
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
   link/ether 00:15:5d:ae:02:78 brd ff:ff:ff:ff:ff:ff
   inet 172.16.8.10/24 brd 172.16.8.255 scope global eth0
      valid_lft forever preferred_lft forever
   inet 172.16.8.100/32 scope global eth0
      valid_lft forever preferred_lft forever
   inet6 fe80::436e:b837:43b:797c/64 scope link
      valid_lft forever preferred_lft forever

已经成功添加VIP 172.16.8.100。

测试MASTER

启动keepalived:

[root@ka68 keepalived]# systemctl start keepalived

现在停止 MASTER,看会不会漂移到BACKUP:

[root@ka67 keepalived]# systemctl stop keepalived

查看BACKUP运行日志:

[root@ka68 keepalived]# cat /cat /var/log/messages
...
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) Transition to MASTER STATE
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) Entering MASTER STATE
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) setting protocol VIPs.
Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
...

已经成功漂移到BACKUP 主机。

再次启动MASTER:

[root@ka67 keepalived]# systemctl start keepalived

查看BACKUP Keepalived服务状态:

[root@ka68 keepalived]# systemctl status keepalived
keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-03-02 22:13:14 EST; 15min ago
  Process: 1448 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1449 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1449 /usr/local/keepalived/sbin/keepalived -D
           ├─1450 /usr/local/keepalived/sbin/keepalived -D
           └─1451 /usr/local/keepalived/sbin/keepalived -D

Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) Sending/queueing gratuitous ARPs on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: Sending gratuitous ARP on eth0 for 172.16.8.100
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) Entering BACKUP STATE
Keepalived_vrrp[1451]: VRRP_Instance(VG_1) removing protocol VIPs.
Keepalived_vrrp[1451]: Opening script file /usr/local/keepalived/etc/keepalived/notify.sh

以上状态表明,当MASTER恢复服务后,BACKUP的Keepalived会自动漂移到MASTER上。因为MASTER的权重值比BACKUP高。以上是BACKUP的漂移到MASTER的状态。

Linux Centos7 MariaDB(MySQL)10.2 主从半同步复制模式安装配置

实验环境情况;

操作系统:CentOS Linux release 7.3.1611 (Core)
数据库:MariaDB-10.2.6-linux-glibc_214-x86_64

1.实现半同步复制功能需要在主从服务器上安装google提供的插件。插件在MySQL二进制/usr/local/mysql/lib/plugin目录下,分别是;

主插件: semisync_master.so
从插件: sosemisync_slave.so

2.分别在主从节点上安装插件

3.主服务器安装插件:

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; //安装插件 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled = 1; //启动插件模块 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout = 1000; //设置超时时间

4.从服务器安装插件;

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; //安装插件 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled = 1; //启动插件模块
MariaDB [(none)]> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD; //重启进程使插件模块生效

5.在主Master和从Slave的my.cnf中编辑添加以下:

[mysqld]
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000

[mysqld]
rpl_semi_sync_slave_enabled = 1

6.我们至此已经实现了mysql数据库复制的半同步方式的架构。在主服务器的数据库中创建一个数据库或表,在从服务器停止后,查看主服务器是否初次延迟,二次创建数据库则降为异步同步,当从服务器启动后如果同步成功,那说明配置已经没有问题。

Linux MariaDB(MySQL)10.2 数据库基于GTID主从同步复制安装配置

原创作品,转载请注明本文地址

操作系统:CentOS Linux release 7.3.1611 (Core)
数据库:MariaDB-10.2.6-linux-glibc_214-x86_64

主服务器:10.10.10.56
从服务器:10.10.10.163

1.今天要做的事,单向主从同步

关于MariaDB(MySQL)数据库安装请参阅《Centos7.3 x64位二进制安装(MySQL)MariaDB 10.1.20数据库之生产篇》下面就开始MariaDB(MySQL)服务器集群的配置过程。

2.在MariaDB(MySQL)配置文件中修改或添加以下信息

vim /etc/my.cnf

主从通用配置

binlog-format = mixed #二进制日志记录的模式(高版本默认开启)
binlog-checksum = CRC32 #可使主机为写入二进制日志的事件写入校验(高版本默认开启)
sync-master-info = 1 #MariaDB依靠操作系统将master.info文件刷新到磁盘。
sync_relay_log_info = 1 #MariaDB依靠操作系统将relay-log.info文件刷新到磁盘。
expire_logs_days = 7 #日志文件过期天数,默认是 0,表示不过期 
master-verify-checksum = 1 #主服务器效验
slave-sql-verify-checksum = 1 #从服务器效验

3.主服务器Master除了通用配置外,还需要加入以下代码

server-id = 56 #MySQL服务器ID,不重复
log-bin = mysql-bin #二进制日志(默认开启)
sync-binlog = 1 #主服务器进行设置,用于事务安全
log-bin-index = mysql-bin

4.从服务器Slave除了通用配置外,还需要加入以下代码

server-id = 163
relay-log = relay-bin #中继日志
slave-parallel-threads = 2 #设定从服务器的SQL线程数
#replicate-do-db = renwoleblogdb#复制指定的数据库,多个写多行
replicate-ignore-db = mysql #不备份的数据库,多个写多行
relay_log_recovery = 1 #从站崩溃后可以使用,防止损坏的中继日志处理。
log-slave-updates = 1 #slave将复制事件写进自己的二进制日志
relay-log-index = relay-bin

此外Mysql从服务器没有必要开启二进制日志,但是在一些情况下,必须设置,例如;如果slave为其它slave的master,必须设置bin_log。我这里就默认开启。

5.以上只是简单的介绍了每个参数的作用,这些参数的具体设置还需根据用户的实际情况进行相关调整,具体可到官方了解

《复制和二进制日志服务器系统变量》
//mariadb.com/kb/en/mariadb/replication-and-binary-log-server-system-variables/

关于系统变量的兼容性,可参阅官方

《MariaDB与MySQL兼容性》
//mariadb.com/kb/en/mariadb/mariadb-vs-mysql-compatibility/

6.主服务器Master授权配置

主MariaDB服务器上创建专用账号并授权数据库权限,以及从服务器IP的远程访问

# mysql -uroot -p
Enter password:【输入你的MySQL密码回车】
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'renwoleuseracc'@'%' IDENTIFIED BY 'renwoleuserpass'; //创建Slave专用备份账号
MariaDB [(none)]> flush privileges; //刷新MySQL权限
MariaDB [(none)]> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; //查看授权情况
MariaDB [(none)]> flush tables with read lock; //锁定数据库防止master值变化
MariaDB [(none)]> show master status; //获取master状态值
+-----------------+----------+------------+-----------------+
| File |Position |Binlog_Do_DB|Binlog_Ignore_DB |
+-----------------+----------+------------+-----------------+
| mysql-bin.000006| 627 | | |
+-----------------+----------+------------+-----------------+
1 row in set (0.00 sec)

7.一旦获取了备份时正确的Binlog位点(文件名和偏移量),那么就可以用BINLOG_GTID_POS()函数来计算GTID

MariaDB [(none)]> SELECT BINLOG_GTID_POS("mysql-bin.000006", 627);
+------------------------------------------+
| BINLOG_GTID_POS('mysql-bin.000006', 627) |
+------------------------------------------+
| 0-56-4 |
+------------------------------------------+
1 row in set (0.01 sec)

8.从服务器Slave配置

正如官方所说从MariaDB 10.0.13版本开始,mysqldump会自动完成这个工作,并且把GTID的写在导出文件中,只要设置 –master-data 或 –dump-slave 的同时设置 –gtid 即可。

这样的话新的SLAVE就可以通过设置 @@gtid_slave_pos 的值来设定复制的起始位置,用 CHANGE MASTER 把这个值传给主库,然后开始复制:

# mysql -uroot -p
Enter password:【输入你的MySQL密码】
MariaDB [(none)]> SET GLOBAL gtid_slave_pos = "0-56-4";
MariaDB [(none)]> change master to master_host='10.10.10.56',MASTER_PORT = 3306,master_user='renwoleuseracc',master_password='renwoleuserpass',master_use_gtid=slave_pos; //进行主从授权
MariaDB [(none)]> START SLAVE; //启动Slave
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 10.10.10.56
 Master_User: renwoleuseracc
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000006
 Read_Master_Log_Pos: 627
 Relay_Log_File: relay.000035
 Relay_Log_Pos: 537
 Relay_Master_Log_File: mysql-bin.000006
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
 ...
 ...
 ...
 Using_Gtid: Slave_pos
 Gtid_IO_Pos: 0-56-4

9.如果 Slave_IO_Running 与 Slave_SQL_Running 都为YES,表明从服务已经运行,Using_Gtid列判断GTID值是否一致。
说明:

master_host 表示master授权地址
MASTER_PORT MySQL端口
master_user 表示master授权账号
master_password 表示密码
master_use_gtid GTID变量值

10.接下来解锁主服务器数据库表

MariaDB [(none)]> unlock tables; //解锁数据表
MariaDB [(none)]> show slave hosts; //查看从服务器连接状态
MariaDB [(none)]> show global status like "rpl%"; //查看客户端

11.从服务器Slave查看relay的所有相关参数

MariaDB [(none)]> show variables like '%relay%';

12.主从已经配置完成。现在无论在主服务器上增、改、删、查,都会同步到从服务器,根据自己的需求进行相关测试即可。
关于master slave重置语法

重置master的核心语法

RESET MASTER; 意思是执行 RESET MASTER 将删除所有二进制日志文件,并创建一个空白的二进制日志文件,数字后缀为.000001,RESET MASTER 不会影响SLAVE服务器上的工作状态,所以执行这个命令会导致Slave找不到Master的binlog,从而造成同步失败。

重置slave的核心语法

RESET SLAVE; 含义是;RESET SLAVE 将清除slave上的同步位置并删除所有旧的同步中继日志文件,但重置前必须先停止slave服务(STOP SLAVE)

以后有时间,我将介绍基于GTID的半同步主从的文章,这也是在生产中所需要记录下来,以备以后使用。

更多介绍://mysql.taobao.org/monthly/2016/02/08/