【redis】keepalived实现redis双主备份(keepalived + redis高可用方案搭建)
- 方案设计
主机:
172.17.209.25 (简称25)
172.17.209.26 (简称26)
拓扑图:
软件版本:
keepalived-1.2.19.tar.gz
redis-2.8.1.tar.gz
- 安装Redis
先在25机器上安装然后拷贝到26机器上。
-
- 安装
1 |
| tar -xvf redis-2.8.1.tar.gz |
-
- 配置成系统服务
vi /etc/init.d/redis
1 |
| #!/bin/bash |
-
- 配置redis
vi /usr/local/redis/redis.conf
1 |
| daemonize yes |
-
- 配置自启动
chkconfig nginx on
-
- 相关命令
启动:service redis start
关闭:service redis stop
-
- 查看服务
ps -ef | grep redis
-
- 拷贝redis到另一台器机(26机器上)
直接用SSH传输到另外一台机器上
scp -r /usr/local/redis/ root@172.17.209.26:/usr/local/
scp -r /etc/init.d/redis root@172.17.209.26:/etc/init.d/
并在另一台机器也做相同的自启动
- 安装keepalived
- 安装
1 |
| tar -zxvf keepalived-1.2.19.tar.gz |
-
- 配置keepalived.conf
配置详细说明:https://blog.csdn.net/u010391029/article/details/48311699
说明:涉及到4个脚本,分别是:
- redis_master.sh:表示当切换到master状态时,要执行的脚本
- redis_backup.sh:表示当切换到backup状态时,要执行的脚本
- redis_fault.sh当keepalived进入fault状态时,会执行notify_fault ;
- redis_stop.sh当keepalived进入stop状态时,会执行notify_stop;
vi /etc/keepalived/keepalived.conf
1 |
| vrrp_script chk_redis { |
备注:拷贝到26后,要改一下notify_master和notify_backup参数中的172.17.209.25为172.17.209.26;
配置项说明:
State:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定,里如果这里设置为master,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为master
interface:实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
virtual router id:这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
auth type:认证方式,可以是PASS或AH两种认证方式
auth pass:认证密码
virtual ipaddress:这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
version:版本,4位,在RFC3768中定义为2;
Type:类型,4位,目前只定义一种类类型:通告数据,取值为1;
Virtual Rtr ID:虚拟路由器ID,8位
Priority:优先级,8位,具备冗余IP地址的设备的优先级为255;
Count IP Addrs:VRRP包中的IP地址数量,8位;
Auth Type:认证类型,8位,RFC3768中认证功能已经取消,此字段值定义0(不认证),为1,2只作为对老版本的兼容;
Adver Int:通告包的发送间隔时间,8位,单位是秒,缺省是1秒;
Checksum:校验和,16位,校验数据范围只是VRRP数据,即从VRRP的版本字段开始的数据,不包括IP头;
IP Address(es):和虚拟路由器相关的IP地址,数量由Count IP Addrs决定
Authentication Data:RFC3768中定义该字段只是为了和老版本兼容,必须置0。
-
- (4)配置自启动
chkconfig keepalived on
-
- (5)相关命令
启动:service keepalived start
关闭:service keepalived stop
-
- (6)查看服务
ps -ef | grep keepalived
-
- 编写keepalived相关redis检查脚本
- redis_master.sh
- 编写keepalived相关redis检查脚本
1 |
| #!/bin/bash |
-
-
- redis_backup.sh
-
1 |
| #!/bin/bash |
-
-
- redis_fault.sh
-
1 |
| #!/bin/bash |
-
-
- redis_stop.sh
-
1 |
| #!/bin/bash |
-
-
- redis_check.sh
-
1 |
| #!/bin/bash |
将以上shell拷贝到目录:/usr/local/keepalived/scripts/redis/,
然后赋权:chmod 755 /usr/local/keepalived/scripts/redis/*.sh
-
- 拷贝keepalived到另一台器机9.26
在26机器上也创建目录:/etc/keepalived,/usr/local/keepalived/var/log,/usr/local/keepalived/scripts/redis
scp -r /usr/sbin/keepalived root@172.17.209.26:/usr/sbin/
scp -r /etc/sysconfig/keepalived root@172.17.209.26:/etc/sysconfig/
scp -r /etc/init.d/keepalived root@172.17.209.26:/etc/init.d/
scp -r /usr/local/keepalived/scripts/redis/* root@172.17.209.26:/usr/local/keepalived/scripts/redis
- 三、测试方案
查看日志命令:tailf -500 /data/keepalived/log/keepalived-redis-state.log
查看redis主从角色命令:/usr/local/redis/redis-cli -p 6379 info replication
链接:https://www.jianshu.com/p/2c3c6d320bf5
实际经验告诉我主机备机都有vip的情况就是防火墙或路由的问题了
两台机器都不能互相访问的话,就会有两个vip,且不能访问,检查防火墙,centos 7 以后是firewall
- 遇到的错误:
- Configuration file '/etc/keepalived/keepalived.conf' is not a regular non-executable file
解决方式
查看keeplived权限
ll /etc/keepalived/
total 8
-rwxrwxr-x 1 root root 600 Dec 25 14:38 check_mysql.sh
-rwxrwxr-x 1 root root 645 Dec 25 14:46 keepalived.conf
更改为644权限即可
chmod 644 keepalived.conf
ll keepalived.conf
-rw-r--r-- 1 root root 645 Dec 25 15:02 keepalived.conf
执行成功
-
- auth.log 频繁出现Cron: pam_unix (cron:session): session opened/closed for user root by (uid=0)
http://languor.us/cron-pam-unix-cron-session-session-opened-closed-user-root-uid0
https://blog.csdn.net/u010433704/article/details/103079928
原文:
现在的生产环境基本上都采用主备方式,而如何实现redis的主备呢?一般情况下,redis实现主从复制比较简单,只需要在从服务器的配置文件里配置 "slaveof "即可。但这样存在一个问题:主服务器挂掉时,可临时将业务地址转移到从服务器,但从服务器无法进行写操作。
为解决这个问题,在网上查找了一些资料。主要有以下几种方案
1、使用keepalived+监控脚本实现主备切换
2、使用redis-cluster实现双主切换,原理就是模仿MySQL的bin-log机制。
本文介绍一下第一种方案
设计思路
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby(后备);
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
环境介绍
Master:172.17.129.251
Slave: 172.17.129.253
VIP: 172.17.129.250
基于如下的拓扑图:
实施步骤
1.两台主机上分别安装keepalived服务
#yum -y install keepalived
2.修改keepalived配置文件keepalived.conf
一般在:/etc/keepalived/keepalived.conf
修改Master上的
Master
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh"interval 2 } vrrp_instance VI_1 { state MASTER interface em1 virtual_router_id 51 priority 101 authentication { auth_type PASS auth_pass redis } track_script { chk_redis } virtual_ipaddress { 192.168.3.188 } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } |
修改slave上的
Slave
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" interval 2 } vrrp_instance VI_1 { state BACKUP interface em1 virtual_router_id 51 # mcast_src_ip 192.168.3.186 priority 100 authentication { auth_type PASS auth_pass redis } track_script { chk_redis } virtual_ipaddress { 192.168.3.188 } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh } |
3.编写脚本
首先在两台服务器分别创建监控redis的脚本
#!/bin/bash netstat -ntlp |grep 6379 if [ "$?" == "0" ]; then echo $? exit 0 else echo $? exit 1 fi |
然后是以下这些
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
当keepalived进入Master状态时,会执行notify_master;
当keepalived进入Backup状态时,会执行notify_backup;
当keepalived进入fault状态时,会执行notify_fault ;
当keepalived进入stop状态时,会执行notify_stop;
在主服务器上编写以下两个脚本
redis_master.sh
#!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.186 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
redis_backup.sh
#!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 sleep 15 # 延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.186 6379 >> $LOGFILE 2>&1 |
在从服务器上编写以下两个脚本
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE2>&1 echo "Run SLAVEOF cmd ..." >>$LOGFILE $REDISCLI SLAVEOF 192.168.3.185 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
redis_backup.sh
#!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 192.168.3.185 6379 >> $LOGFILE 2>&1 |
主从共同编写以下两个脚本
redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]">> $LOGFILE
date >> $LOGFILE
添加执行权限
#chmod +x *.sh
4.启动并验证
a.启动redis-server
b.启动keepalived
使用命令ip a查看VIP
c.访问VIP,并写入数据
# /usr/local/redis/bin/redis-cli -h 192.168.3.188 set hello 1
OK
d.模拟主服务down掉,访问VIP并写入数据
主服务哭关闭redis进程
#pkill -9 redis
查看VIP
访问VIP
# /usr/local/redis/bin/redis-cli -h 192.168.3.188 get hello
"1"
写入数据
# /usr/local/redis/bin/redis-cli -h 192.168.3.188 set hello2 2
OK
e恢复主服务并查看写添加数据
# /usr/local/redis/bin/redis-cli -h 192.168.3.188 get hello2
"2"
至此,双主备份成功实现。