1.环境准备:
系统版本 Centos7
IP:10.10.11.79 master
IP:10.10.11.80 backup
虚拟ip:10.10.11.82
Docker环境:
centos7离线安装docker,docker-compose
keepalived安装包:
https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
2.配置两台 MySQL 主主同步
制作Mysql容器
使用dokcer快速启动mysql容器,注意端口号有没有被占用,两台服务器都要启动,两台服务器端口号要一致,防火墙也看一下,对外能不能访问端口。
docker run -p 3308:3306 --privileged=true --name keepalivedMysql -v /home/mysql/conf:/etc/mysql/mysql.conf.d -v /home/mysql/logs:/var/log/mysql -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d nanlist/mysql5.7:v1.1
配置MySQL
编辑mysqld.cnf,可以挂载目录,或者从容器中cp出来,编辑好以后再cp进容器内部.
把配置文件拷贝出来
docker cp keepalivedMysql:/etc/mysql/mysql.conf.d/mysqld.cnf /home/keepalivedMysql/mysqld.cnf
编辑完成再拷贝进去
docker cp /home/keepalivedMysql/mysqld.cnf keepalivedMysql:/etc/mysql/mysql.conf.d/mysqld.cnf
MASTER:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names = 1
log-bin=/var/lib/mysql/mysql-bin
server-id=1 #backup这台设置2
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
BACKUP:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
lower_case_table_names = 1
log-bin=/var/lib/mysql/mysql-bin
server-id=2 #backup这台设置2
binlog-ignore-db = mysql,information_schema #忽略写入binlog日志的库
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 2 #初始字段ID为2
slave-skip-errors = all #忽略所有复制产生的错误
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
注:master和backup只有server-id不同和 auto-increment-offset不同。
修改好以后,重启mysql容器
docker restart keepalivedMysql
查看下log bin日志和pos值位置
进入mysql docker容器内部,登录mysql环境:
mysql> show master status;
配置主从
主主模式就是配置两个主从,先配置10.10.11.79(主1)->10.10.11.80(主2)的主从,然后再反过来配置10.10.11.80(主2)->10.10.11.79(主1)的主从,这样主主的模式就配置好了。
配置 master:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.10.11.%' IDENTIFIED BY 'replication';
mysql> flush privileges;
mysql> change master to master_host='10.10.11.80',master_port=3308, master_user='replication', master_password='replication',master_log_file='mysql-bin.000004', master_log_pos=615; #对端状态显示的值
mysql> start slave; #启动同步
master_log_file='mysql-bin.000004'
master_log_pos=154;
这两个参数根据自己的服务器填对应的值,不要填错了。
配置 backup:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.10.11.%' IDENTIFIED BY 'replication';
mysql> flush privileges;
mysql> change master to master_host='10.10.11.79',master_port=3308, master_user='replication', master_password='replication', master_log_file='mysql-bin.000004', master_log_pos=615;
mysql> start slave;
3.测试主主同步
主主同步配置完毕,查看同步状态mysql> show slave status\G
Slave_IO和Slave_SQL是YES说明主主同步成功。
可以使用工具链接一下试试,这一步略过。
建一张表,两个数据库里面分别插入数据,看看是否同步。
4.安装 keepalived
在两台宿主机器上分别安装 keepalived (注意:keepalived安装在实体机上,不是安装到Docker容器中)
1.联网下载到/usr/local目录下并解压有可能会提示连接不成功,加上它提示的命令再下载就好了
如果下载不下来用浏览器下载下来再传到服务上面也可以
两台服务器分别执行下:
主:
[root@Master html]# cd /usr/local
[root@Master local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Master local]# tar -zxvf keepalived-1.4.2.tar.gz
备:
[root@Slave html]# cd /usr/local
[root@Slave local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Slave local]# tar -zxvf keepalived-1.4.2.tar.gz
2.安装相关依赖,有不用下载了
主:
[root@Master local]# yum install -y gcc openssl-devel popt-devel
备:
[root@Slave local]# yum install -y gcc openssl-devel popt-devel
3.编译安装:
主:
[root@Master local]# cd keepalived-1.4.2
[root@Master keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Master keepalived-1.4.2]# make
[root@Master keepalived-1.4.2]# make install
备:
[root@Slave local]# cd keepalived-1.4.2
[root@Slave keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Slave keepalived-1.4.2]# make
[root@Slave keepalived-1.4.2]# make install
4.相关配置,按命令执行
主:
[root@Master keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Master keepalived-1.4.2]# mkdir /etc/keepalived
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
备:
[root@Slave keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Slave keepalived-1.4.2]# mkdir /etc/keepalived
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Slave keepalived-1.4.2]#
安装之后,在 /etc/keepalived目录下有个 keepalived.conf 配置文件:
编辑master服务器上的keepalived.conf:
! Configuration File forkeepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 #主备相同
priority 100 #优先级,backup设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 指定VIP地址
10.10.11.82
}
}
virtual_server 10.10.11.82 3308 { #配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.10.11.79 3308 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /etc/keepalived/chk_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
}
编辑backup服务器上的keepalived.conf:
! Configuration File forkeepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #标识,双主相同
}
vrrp_instance VI_1 {
state BACKUP #两台都设置BACKUP
interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 #主备相同
priority 90 #优先级,backup设置90
advert_int 1
nopreempt #不主动抢占资源,只在master这台优先级高的设置,backup不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 指定VIP地址
10.10.11.82
}
}
virtual_server 10.10.11.82 3308 { #配置virtual_server ip为上面配置的虚拟vip地址 端口为mysql的端口
delay_loop 2
#lb_algo rr #LVS算法,用不到,我们就关闭了
#lb_kind DR #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50 #同一IP的连接60秒内被分配到同一台真实服务器
protocol TCP
real_server 10.10.11.80 3308 { #检测本地mysql,backup也要写检测本地mysql
weight 3
notify_down /etc/keepalived/chk_mysql.sh #当mysq服down时,执行此脚本,杀死keepalived实现切换
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔时间
}
}
}
backup服务器只修改priority为90、nopreempt不设置、real_server设置本地IP。
5.编写检测服务down后所要执行的脚本
vim /etc/keepalived/chk_mysql.sh
跟keepalived.conf配置文件放同一个目录下面
内容如下:
#!/bin/bash
#version 0.0.1
#当mysql进程不存在时,会自动重启mysql容器;
A=`ps -ef|grep mysqld|grep -v grep|grep -v bash|wc -l`
if [ $A -eq 0 ];then
docker restart keepalivedMysql #重启mysql容器
sleep 2
if [ `ps -ef|grep mysqld|grep -v grep|grep -v bash|wc -l` -eq 0 ];then
systemctl stop keepalived #停掉keepalived服务
fi
fi
chk_mysql.sh脚本:目的是检测mysqld进程是否存活,如果mysql进程挂掉了,就杀掉keepalived服务,从而实现了MySQL故障自动转移。
查看脚本是否有运行的权限
如果你是root登陆的话(不是的话,切换到root用户,对*.sh 赋可执行的权限)
两台服务器这一步授权一定要执行不然脚本会失效:
cd /etc/keepalived
chmod +x chk_mysql.sh
脚本上传以后重启keepalived。
6.keepalived常用命令
#启动:
service keepalived start
#停止:
service keepalived stop
#查看状态:
service keepalived status
重启以后用那个虚拟ip链接一下,试试:
7.验证
1:如果验证主从是否可以自动切换实现热备效果可以把其中一台机器的keepalived服务stop掉,然后观察虚拟ip看是否实现了故障转移。
2:如果验证健康脚本是否生效,可以把mysql容器stop掉,过两秒再次查看一下mysql容器是否会自动启动。