Keepalived+Master-Master-Slave组合

参考文章:http://blog.csdn.net/catoop/article/details/41285001

master1和master2公用一个VIP。当某个master宕机后,keepalived能自动将VIP漂移到另一个master上。只需要1~3秒即可切换完成,基本能保证服务不中断。

实验环境:

CentOS6.7X86_64

MariaDB10.0.17

Keepalived1.2.21

各节点配置:

Master1:192.168.2.13

Master2:192.168.2.14

VIP:192.168.2.100

Slave:192.168.2.11

Web服务器:192.168.2.11

主要步骤如下:

构建MySQL双主---> 配置Keepalived ---> 模拟节点故障 ---> MHA基础上构建web站点 ---> MHA基础上添加Slave

一、构建双主MySQL

安装MariaDB过程略过,可以直接使用通用二进制包进行安装。

首先确保2台Master是新安装的(不是新安装的话,执行mysql_install_db重新初始化即可),配置起来更方便些。

配置Master1,修改my.cnf,如下:

[mysqld]

....主要修改如下:....

innodb_file_per_table =ON

skip_name_resolve = ON

log-bin=mysql-bin

binlog_format=mixed

server-id=1

auto-increment-offset = 1

auto-increment-increment = 2

replicate-ignore-db = test

replicate-ignore-db = mysql

配置Master2,修改my.cnf,如下:

[mysqld]

....主要修改如下:....

innodb_file_per_table =ON

skip_name_resolve = ON

log-bin=mysql-bin

binlog_format=mixed

server-id=2

 

auto-increment-offset = 2

auto-increment-increment = 2

replicate-ignore-db = test

replicate-ignore-db = mysql

Master1上建立授权用户

> grant replication slave on*.* to ‘repluser‘@‘192.168.2.%‘ identified by ‘123456‘;

> flush privileges;

> show master status;

Master2上连接到Master1作为自己的主服务器

> change master tomaster_host=‘192.168.2.13‘,

master_user=‘repluser‘,

master_password=‘123456‘,

master_log_file=‘mysql-bin.000003‘,

master_log_pos=22519;

> start slave;

> show slave status\G

Master2上建立授权用户

> grant replication slave on*.* to ‘repluser‘@‘192.168.2.%‘ identified by ‘123456‘;

> flush privileges;

> show master status;

Master1上连接到Master2作为自己的主服务器

> change master to master_host=‘192.168.2.14‘,

master_user=‘repluser‘,

master_password=‘123456‘,

master_log_file=‘mysql-bin.000003‘,

master_log_pos=643;

> start slave;

> show slave status\G

我们可以随意在master1和master2建立测试数据库,插入数据,可以发现在任意节点插入都能自动同步到另一台上面。

二、安装配置keepalived

编译安装keepalived

在2个节点都如下操作:

tar xf keepalived-1.2.21.tar.gz

cd keepalived-1.2.21

./configure

make && make install

cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d

cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf/etc/keepalived

cp /usr/local/sbin/keepalived/usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

master1(192.168.2.13)上,修改keepalived的配置文件,如下:

! Configuration File forkeepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id MySQL_HA

}

vrrp_instance VI_1 {

state BACKUP      # 两台配置此处均是BACKUP

interface eth0    # 网卡,可使用ifconfig查看

virtual_router_id 51

priority 90       # 优先级,另一台改为90,默认VIP飘在在这个节点上

advert_int 1

nopreempt         # 不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.100 dev eth0 label eth0:1

}

}

virtual_server 192.168.2.100 3306{

delay_loop 2      # 每个2秒检查一次real_server状态

lb_algo rr    # 轮询算法

lb_kind DR    # LVS算法,直接转发

persistence_timeout 50   # 会话保持时间

protocol TCP

real_server 192.168.2.13 3306 {     # 只配置一个real_server,且为本机地址

weight 3

notify_down/home/scripts/MySQL.sh # 检测到服务down后执行的脚本

TCP_CHECK{

connect_timeout10       # 连接超时时间

nb_get_retry 3           # 重连次数

delay_before_retry 3 # 重连间隔时间

connect_port 3306    # 健康检查端口

}

}

}

master2(192.168.2.14)上,修改keepalived的配置文件,如下:

! Configuration File forkeepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id MySQL_HA

}

vrrp_instance VI_1 {

state BACKUP      # 两台配置此处均是BACKUP

interface eth0    # 网卡,可使用ifconfig查看

virtual_router_id 51

priority 90       # 优先级,master2这台改为90,当master1无响应,则VIP会飘到这个节点上

advert_int 1

#nopreempt          # 不抢占,只在优先级高的机器上设置即可,优先级低的机器设不设置都无所谓

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.2.100 dev eth0 label eth0:1

}

}

virtual_server 192.168.2.100 3306{

delay_loop 2      # 每个2秒检查一次real_server状态

lb_algo rr    # 轮询算法

lb_kind DR    # LVS算法,直接转发

persistence_timeout 50   # 会话保持时间

protocol TCP

real_server 192.168.2.14 3306 {     # 只配置一个real_server,且为本机地址

weight 3

notify_down/home/scripts/MySQL.sh # 检测到服务down后执行的脚本

TCP_CHECK{

connect_timeout10       # 连接超时时间

nb_get_retry 3           # 重连次数

delay_before_retry 3 # 重连间隔时间

connect_port 3306    # 健康检查端口

}

}

}

编写检测服务down后所要执行的脚本

在master1和master2都执行下面的操作:

mkdir /home/scripts/

vim /home/scripts/MySQL.sh 内容如下:

#!/bin/sh

pkill keepalived

注意:

此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkill keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。

另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP。

master1master2上都启动keepalived

/etc/init.d/keepalived start

ps aux|grep keepalived

ifconfig -a

可以看到VIP默认是在master1上的(因为keepalived里配置master1的优先级为100,master2的优先级为90)

三、模拟节点故障

模拟MySQLMaster的写节点故障

我们可以模拟下master的MySQL无法访问了,看看VIP会不会自动漂移。

先查看master1、master2的IP情况,如下图:

可以看到VIP在master1节点上。我们在master1上执行/etc/init.d/mariadb stop 模拟master1的MySQL进程挂掉的情况。

然后,在maste1和master2上执行

ps aux|egrep "keepalived|mariadb"

ip a

可以看到VIP已经漂移到master2上了。此时,master1上的MariaDB进程挂掉后,keepalived进程也一并被kill了。

至此,实验初步算是完成了。但是这个方案的话,mysql一退出, keepalived也退出了,后面再次修复的话要手动启动keepalived进程,不然就达不到高可用的效果了。不过出于数据安全性、完整性考虑,MySQLkeepalived进程还是应该手动恢复的。

这个方案还有个不足,就是mysql端口和进程都在,但是复制线程却不是Yes状态了,这种只能靠zabbix或者其它监控工具来监控了。

四、在MHA的基础上构建个web站点

MySQL高可用实现后,不如继续深入下,再基于上面的环境构建个web站点吧,操作如下:

web节点参数:

IP:192.168.2.11

运行有:Nginx+php-fpm

nginx虚拟主机配置如下:

server {

listen  80;

server_name  192.168.2.11;

location / {

root           /home/wwwroot/default;

index index.php index.html index.htm;

}

location ~ \.php$ {

root           /home/wwwroot/default;

fastcgi_pass  unix:/tmp/php-cgi.sock;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

include        fastcgi_params;

access_log  /home/wwwlogs/access.log  main;

}

}

/etc/init.d/nginx start     启动nginx服务

/etc/init.d/php-fpm start     启动php-fpm服务

master1或者master2上执行下面命令,创建个web网站所需的数据库:

> create database metinfo;

> grant all on metinfo.* to‘met‘@‘192.168.2.%‘ identified by ‘123456‘;

> flush privileges;

在浏览器访问http://192.168.2.11 如下图:

注意数据库主机地址要填:192.168.2.100

安装完成后,如下图所示:

五、在MHA的基础上添加一个Slave

生产环境中,光靠2个Master是顶不住那么大压力的,因此还需要添加几个Slave节点(假设节点IP 192.168.2.11)。

修改Slave的my.cnf 如下

[mysqld]

....主要修改如下:....

innodb_file_per_table =ON

skip_name_resolve = ON

log-bin=mysql-bin

binlog_format=mixed

server-id   = 10

relay_log =mysql-relay.bin

log_slave_updates = 1

read_only = 1

replicate-ignore-db = test

replicate-ignore-db = mysql

在业务低谷时候,先将keepalived进程停掉,防止写入数据。

然后稍等片刻,等2个master之间的数据同步后,在任意master上执行:

> flush tables with read lock;

# mysqldump -uroot -proot -A >all.sql

# scp [email protected]:/root/

在干净的slave上启动MariaDB,并导入all.sql

# mysql -uroot -proot -e ‘sourceall.sql;‘

导入后,在刚才锁表的master上解除锁

> unlock tables;

操作完成后,在master1和master2上启动keepalived进程,让VIP再次出现。

VIP出现后,然后,在slave上执行:

> change master tomaster_host=‘192.168.2.100‘,

master_user=‘repluser‘,

master_password=‘123456‘,

master_log_file=‘mysql-bin.000006‘,

master_log_pos=326;

> start slave;  启动复制

> show slave status\G

这时候,我们可以在master1或者master2上任意创建库、表、插入数据,然后在slave上都可以看到数据已经正常同步过来了。

时间: 2024-08-03 16:58:10

Keepalived+Master-Master-Slave组合的相关文章

zookeeper学习系列:三、构建一个分布式master、slave系统

之前只理解zk可以做命名,配置服务,现在学习下他怎么用作构建master-slave模式的分布式系统. 为什么叫Zoo?“因为要协调的分布式系统是一个动物园”. ZooKeeper是一个中性化的Service,用于管理配置信息.命名.提供分布式同步,还能组合Service.所有这些种类的Service都会在分布式应用程序中使用到.每次编写这些Service都会涉及大量的修bug和竞争情况.正因为这种编写这些Service有一定难度,所以通常都会忽视它们,这就使得在应用程序有变化时变得难以管理应用

MySQL 主主报错: Fatal error: The slave I/O thread stops because master and slave have

Mysql 主主启动错误处理 error 信息: Slave_IO_State: Master_Host: 192.168.6.87 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 106 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 4 Relay_

jenkins使用Git为源码管理(windows master && linux slave)

作为一个不太经常总结的人,工作以来碰到过太多问题!往往解决之后没有有效记录,导致再次碰到需要重新查资料解决.现在改变下习惯,努力搞的了技术. 公司最近提倡开源(以前啥都机密,即使开源也没改变多少),代码从SVN迁移至Git管理,这样导致原来记录项目日志的wiki持续集成job需要重新配置.之前的设置为:每分钟检查SVN变化,有变化就执行编译发布.改为Git后,碰到坑坑洼洼,一并列在下面. jenkins的Git插件安装 git plugin,git client plugin,github pl

mysql5.5.44 主从复制master and slave have equal MySQL server ids

mysql5.5.44  主从复制配置 注:mysql从5.1.7以后开始就不支持my.cnf中"master-host"类似的参数; 必须在slave 的mysql中执行以下命令(最后的pos没有引号): mysql> change master to master_host='192.168.137.100', master_user='tongbu', master_password='123456', master_log_file='mysql-bin.000014',

RocketMQ多Master多Slave模式部署

每个 Master 配置一个 Slave,有多对Master-Slave,HA采用同步双写方式,主备都写成功,向应用返回成功. 优点:数据与服务都无单点,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高.缺点:性能比异步复制模式略低,大约低10%左右,发送单个消息的RT会略高.目前主宕机后,备机不能自动切换为主机,后续会支持自动切换功能. ### 先启动 NameServer### 在机器 A,启动第一个 Master### 在机器 B,启动第二个 Master### 在机器

故障案例:主从同步报错Fatal error: The slave I/O thread stops because master and slave have equal MySQL server

场景一:因为数据量非常,大概有1.4T,需要在原先master1-slave1的情况下再创建一个库slave2,并且挂在slave1下,即master1-slave1-slave2的结构.为了方便,当时停掉从库salve1,show master status记录状态,开启log_slave_updates,并且关闭salve1,然后将这个从库的data文件夹直接copy到新的从库,结果在创建slave2和slave1的主从关系时报错 Fatal error: The slave I/O thr

TI_DSP总线bus - 3(Bridge,总线连接master与slave表)

Table 4-1列出来了master与slave的连接. ? Y  - 表示master与slave之间有连接(是指通过总线连接master与slave,这里的master与slave没有跨越不同速率的总线): ?  -  - 表示master与slave之间没有连接,即不存在该master对slave的访问: ? n -  表示master与slave之间通过Bridge n连接(直接通过Bridge n连接,或者Bridge n连接了master与slave所连接的两个不同速率的总线).

Jenkins : 安装 master 和 slave

目录 安装 master 安装 slave 设置 master 与 slave 的通信方式 添加 slave 配置 在 salve 上安装 jre 安装并配置 Jenkins salve Jenkins 是一个可扩展的持续集成引擎.主要用于持续.自动地构建.测试软件项目.本文介绍在 windows 平台上安装 Jenkins master 和 slave. 安装 master 请从 Jenkins 的官网下载安装包,直接运行,一路 "next" 就可以了.安装包执行完成后会启动你机器上

MySQL.master and slave 一主多从,切库传输

两个参数,一个是binlog_ignore_db.另一个是replicate-ignore-db  区别: binlog_ignore_db 参数是设置在主库上的,例如:binlog-ignore_db=test,那么针对test库下的所有操作(增删改)都会记录下来,这样slave在接受主库的binlog文件时文件量就会减少. 优点:减少网络I/O,减少slave端I/O线程的I/O量,优化复制性能 Replicate-ignore-db参数是设置在从库上的,例如:replicate-ignor

mysql主从复制 master和slave配置的参数大全

master所有参数1 log-bin=mysql-bin 1.控制master的是否开启binlog记录功能: 2.二进制文件最好放在单独的目录下,这不但方便优化.更方便维护. 3.重新命名二进制日志很简单,只需要修改[mysqld]里的log_bin选项,如下例子:要重新调整logbin的路径为"/home/mysql/binlog"[mysqld]log_bin=/home/mysql/binlog/binlog.log ll /home/mysql/binlog-rw-rw--